zan
Drop in replacement for React.PropTypes
:
import { types } from 'zan';
const { string, number } = types;
React.createClass({
propTypes: {
name: string,
age: number.isOptional
},
render() {
// ...
},
});
The primary differences this has with React.PropTypes
is that
zan
exposes anexactShape
type.- checks by default are
isRequired
already and they each expose anisOptional
method zan
exposes acreateCustomChecker
method which can be used as follows:
const urlString = createCustomChecker(value => /^https?:/.test(value) );
This module also exposes a recursive checker so you don't need to use things like arrayOf
and shape
:
import { types, recursive } from 'zan';
const { number, string } = types;
const propTypes = recursive({
myAge: number,
myFavoriteNumbers: [number],
treeNode: {
value: number
},
buddies: [{
name: string,
age: number,
}],
});
// which is equivalent to:
const propTypes = {
myAge: number,
myFavoriteNumbers: arrayOf(number),
treeNode: shape({
value: number,
}),
buddies: arrayOf(shape({
name: string,
age: number,
})),
};
This module also exposes an a checker so you can check types manually without React
Usage
import { check, types, createCustomChecker } from 'zan';
check(types.bool, true); // null
check(types.bool, 123); // returns Error object
check(types.shape({name: types.string}), {name: 'Me'}); // null
check(types.shape({name: types.string}), {}); // returns Error object
check(types.shape({name: types.string}), {name: 'Me', age: 22}); // null
check(types.exactShape({name: types.string}), {name: 'Me', age: 22}); // returns Error object
check
is curry-able so const numberChecker = check(types.number); numberChecker(22);
works
see test.js from more usage