PropType validators that work with Mori.js.
This package was inspired by react-immutable-proptypes, which I had used often. Striving to become more FRP, I have switched to using mori.js for my immutable needs. I could not find a similar package for validating propTypes with mori data structures, so created this package and used react-immutable-proptypes
as a base.
npm install react-mori-proptypes --save
Usage is simple, they work with and like any React.PropType.* validator.
import MoriPropTypes from 'react-mori-proptypes';
import { vector } from 'mori';
class MyComponent extends React.Component {
static propTypes = {
someMap: MoriPropTypes.map.isRequired,
someVec: MoriPropTypes.vec,
};
static defaultProps = {
someVec: vector(1, 2, 3, 4),
};
render() {
...
}
}
React-Mori-PropTypes has:
MoriPropTypes.list // mori.isList
MoriPropTypes.map // mori.isMap
MoriPropTypes.queue // isQueue
MoriPropTypes.range // isRange
MoriPropTypes.set // mori.isSet
MoriPropTypes.sortedMap // isSortedMap
MoriPropTypes.sortedSet // isSortedSet
MoriPropTypes.vec // mori.isVec
MoriPropTypes.listOf // based on React.PropTypes.arrayOf
MoriPropTypes.mapOf // similar to MoriPropTypes.vecOf, but specific to maps
MoriPropTypes.queueOf // similar to MoriPropTypes.vecOf, but specific to queues
MoriPropTypes.setOf // similar to MoriPropTypes.vecOf, but specific to sets
MoriPropTypes.sortedMap // similar to MoriPropTypes.vecOf, but specific to sortedMaps
MoriPropTypes.sortedSet // similar to MoriPropTypes.vecOf, but specific to sortedSets
MoriPropTypes.vecOf // based on React.PropTypes.arrayOf
React-Mori-PropTypes also supports a contains
type, which is analogous to React.PropTypes.shape
type. This propType validator supports defining the shape of a hashMap, I.E. the keys/value types. You should only use a mori.map
or mori.sortedMap
with contains
.
It is used the same way as shape
:
static propTypes = {
person: MoriPropTypes.contains({
firstName: React.PropTypes.string.isRequired,
lastName: React.PropTypes.string.isRequired,
age: React.PropTypes.number,
})
};
static defaultProps = {
person: mori.hashMap('firstName', 'John', 'lastName', 'Smith')
}
Issues, feedback, and PR's welcome. Please follow the linter rules.