deku-prop-types
Prop type validation for Deku components
Install
npm install --save deku-prop-types
Notes
- Check out deku-prop-types-immutable for additional Prop Type validations for Immutable
- This library is compatible with eslint-plugin-react's prop-types rule
Usage
Note: propType validations are not performed in production environments
function-component.jsx
import element from 'deku'import PropTypes validate from 'deku-prop-types' const Counter = props <div>propscount</div>CounterpropTypes = count: PropTypesnumberisRequired Counter
object-component.jsx
import element from 'deku'import PropTypes validate from 'deku-prop-types' const Counter = propTypes: count: PropTypesnumberisRequired { return <div>propscount</div> } Counter
deku-redux-connect
Usage withimport connect from 'deku-redux-connect'import element from 'deku'import PropTypes validate from 'deku-prop-types' const Counter = props <div>propscount</div>CounterpropTypes = count: PropTypesnumberisRequired const mapStateToProps = count count mapStateToProps
Supported types
PropTypes.any
Validate prop is of any type (not undefined)
PropTypes.array
Validate prop is an array
PropTypes.arrayOf
Validate prop is an array consisting of a type
import element from 'deku'import PropTypes validate from 'deku-prop-types' const NamesList = props <div> propsnames</div> NamesListpropTypes = names: PropTypes NamesList
PropTypes.bool
Validate prop is a boolean
PropTypes.func
Validate prop is a function
PropTypes.instanceOf
Validate prop is an instance of a function or class
import element from 'deku'import PropTypes validate from 'deku-prop-types' const ItemList props => <div> propslist</div> ItemListpropTypes = list: PropTypes ItemList
PropTypes.number
Validate prop is a number
PropTypes.object
Validate prop is an object
PropTypes.objectOf
Validate prop has keys all matching an allowed type
import element from 'deku'import PropTypes validate from 'deku-prop-types' const NameCard = props <div>propspersonfirstName + ' ' + propspersonlastName</div> NameCardpropTypes = person: PropTypes NameCard
PropTypes.oneOf
Validate prop is one of the allowed values
import element from 'deku'import PropTypes validate from 'deku-prop-types' const Color = props <div>propscolor</div> ColorpropTypes = color: PropTypes Color
PropTypes.oneOfType
Validate prop is one of the allowed types
import element from 'deku'import PropTypes validate from 'deku-prop-types' const Age = props <div>propsage</div> AgepropTypes = age: PropTypes Age
PropTypes.shape
Validate an object's properties are of a certain type
import element from 'deku'import PropTypes validate from 'deku-prop-types' const ConfigDisplay = props <div>propsconfigport + ' ' + propsconfighost</div> ConfigDisplaypropTypes = config: PropTypes ConfigDisplay
PropTypes.string
Validate prop is a string
Note: all PropTypes can be required by specifying isRequired
like below:
PropTypes.number.isRequired
Custom Validators
A function may be passed instead of a propType.
import element from 'deku'import validate from 'deku-prop-types' const Counter = props <div>propscount</div>CounterpropTypes = { if propspropName < 10 return 'count must be less than 10' } Counter
API
validate(component, [warningLevel])
component
type: function
| object
Validate props passed to component match the specified type. An Error
is thrown if a prop is not valid.
warningLevel
type: number
default: 1
If a missing propType is discovered, validate will by default log a warning. This functionality may be configured.
0
- Do not warn or throw error
1
- Log a warning
2
- Throw an error
License
MIT © Dustin Specker