joi-react
v1.0.1
Published
Convert joi schemas to React PropTypes.
Downloads
5
Maintainers
Readme
Synopsis
joi-react is a tiny wrapper to convert joi schemas into React PropType validators.
Install
Node.js
With NPM
npm install joi-react
From source
git clone https://github.com/foss-haas/joi-react.git
cd joi-react
npm install
npm run test && npm run dist
API
joiToPropType(joiSchema):Function
Takes a joi schema and returns a React PropType.
Example:
var joi = require('joi');
var joiToPropType = require('joi-react');
var React = require('react');
var starsSchema = joi.number().integer().min(1).max(5).required();
var starsPropType = joiToPropType(starsSchema);
var Rating = React.createClass({
displayName: 'Rating',
propTypes: {
stars: starsPropType
},
render: function () {
var stars = [];
for (var i = 0; i < this.props.stars; i++) {
stars.push(React.createElement('img', {src: 'star.png'}));
}
return stars;
};
});
React.renderToString(React.createElement(Rating));
// -> Warning: value is required
React.renderToString(React.createElement(Rating, {stars: 0}));
// -> Warning: value must be larger than or equal to 1
React.renderToString(React.createElement(Rating, {stars: 6}));
// -> Warning: value must be less than or equal to 5
React.renderToString(React.createElement(Rating, {stars: 5}));
// no warnings
License
The MIT/Expat license. For more information, see http://foss-haas.mit-license.org/ or the accompanying LICENSE file.