Last active
April 24, 2018 11:12
-
-
Save glortho/23715563d2b74d0ae5bb to your computer and use it in GitHub Desktop.
Nullable PropType for React
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Usage: | |
* | |
* import React from 'react'; | |
* import nullable from 'prop_nullable'; | |
* | |
* let myClass = React.createClass({ | |
* propTypes: { | |
* myProp: nullable( React.PropTypes.string ).isRequired, | |
* myOtherProp: nullable( [React.PropTypes.string, React.PropTypes.number] ) | |
* } | |
* }); | |
*/ | |
import { PropTypes } from 'react'; | |
let { oneOf, oneOfType } = PropTypes; | |
export default types => oneOfType( [ oneOf( [ null ] ), ...[].concat( types ) ] ); |
This doesn't raise any warning if types
is undefined.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice, I hope something like this is integrated in React. Although I like the Joi way for validations more.
https://github.com/hapijs/joi
You can do something like this:
A "white list" like
allow(whatever)
is more flexible.