├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── joi-to-proptype.js /.gitignore: -------------------------------------------------------------------------------- 1 | npm-debug.log 2 | node_modules 3 | coverage 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | after_script: 5 | - npm run coveralls 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Foss & Haas GmbH 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a 4 | copy of this software and associated documentation files (the "Software"), 5 | to deal in the Software without restriction, including without limitation 6 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 7 | and/or sell copies of the Software, and to permit persons to whom the 8 | Software is furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Synopsis 2 | 3 | **joi-react** is a tiny wrapper to convert [joi](https://github.com/hapijs/joi) schemas into [React](https://github.com/facebook/react) PropType validators. 4 | 5 | [![license - MIT](https://img.shields.io/npm/l/joi-react.svg)](https://foss-haas.mit-license.org) [![Dependencies](https://img.shields.io/david/foss-haas/joi-react.svg)](https://david-dm.org/foss-haas/joi-react) 6 | 7 | [![NPM status](https://nodei.co/npm/joi-react.png?compact=true)](https://npmjs.org/package/joi-react) 8 | 9 | [![Build Status](https://img.shields.io/travis/foss-haas/joi-react.svg)](https://travis-ci.org/foss-haas/joi-react) [![Coverage Status](https://img.shields.io/coveralls/foss-haas/joi-react.svg)](https://coveralls.io/r/foss-haas/joi-react?branch=master) 10 | 11 | # Install 12 | 13 | ## Node.js 14 | 15 | ### With NPM 16 | 17 | ```sh 18 | npm install joi-react 19 | ``` 20 | 21 | ### From source 22 | 23 | ```sh 24 | git clone https://github.com/foss-haas/joi-react.git 25 | cd joi-react 26 | npm install 27 | npm run test && npm run dist 28 | ``` 29 | 30 | # API 31 | 32 | ## joiToPropType(joiSchema):Function 33 | 34 | Takes a joi schema and returns a React PropType. 35 | 36 | Example: 37 | 38 | ```js 39 | var joi = require('joi'); 40 | var joiToPropType = require('joi-react'); 41 | var React = require('react'); 42 | var starsSchema = joi.number().integer().min(1).max(5).required(); 43 | var starsPropType = joiToPropType(starsSchema); 44 | 45 | var Rating = React.createClass({ 46 | displayName: 'Rating', 47 | propTypes: { 48 | stars: starsPropType 49 | }, 50 | render: function () { 51 | var stars = []; 52 | for (var i = 0; i < this.props.stars; i++) { 53 | stars.push(React.createElement('img', {src: 'star.png'})); 54 | } 55 | return stars; 56 | }; 57 | }); 58 | 59 | React.renderToString(React.createElement(Rating)); 60 | // -> Warning: value is required 61 | React.renderToString(React.createElement(Rating, {stars: 0})); 62 | // -> Warning: value must be larger than or equal to 1 63 | React.renderToString(React.createElement(Rating, {stars: 6})); 64 | // -> Warning: value must be less than or equal to 5 65 | React.renderToString(React.createElement(Rating, {stars: 5})); 66 | // no warnings 67 | ``` 68 | 69 | # License 70 | 71 | The MIT/Expat license. For more information, see http://foss-haas.mit-license.org/ or the accompanying [LICENSE](https://github.com/foss-haas/joi-react/blob/master/LICENSE) file. -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*jshint browserify: true */ 2 | 'use strict'; 3 | module.exports = joiToPropType; 4 | 5 | function joiToPropType(joiSchema) { 6 | return function joiPropType(props, propName) { 7 | return joiSchema.validate(props[propName]).error; 8 | }; 9 | } 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "joi-react", 3 | "version": "1.0.1", 4 | "description": "Convert joi schemas to React PropTypes.", 5 | "main": "index.js", 6 | "scripts": { 7 | "lint": "jshint index.js test", 8 | "test": "npm run lint && mocha --growl -R spec", 9 | "cover": "npm run lint && istanbul cover --report lcov _mocha -- -R spec", 10 | "coveralls": "npm run cover && cat ./coverage/lcov.info | coveralls ; rm -rf ./coverage" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/foss-haas/react-joi.git" 15 | }, 16 | "keywords": [ 17 | "react", 18 | "joi", 19 | "proptypes", 20 | "validation", 21 | "components", 22 | "schema" 23 | ], 24 | "author": "Alan Plum ", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/foss-haas/react-joi/issues" 28 | }, 29 | "homepage": "https://github.com/foss-haas/react-joi", 30 | "devDependencies": { 31 | "coveralls": "^2.11.2", 32 | "expect.js": "^0.3.1", 33 | "istanbul": "^0.3.2", 34 | "joi": "^4.9.0", 35 | "jshint": "^2.5.10", 36 | "mocha": "^2.0.1", 37 | "react": "^0.12.1" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /test/joi-to-proptype.js: -------------------------------------------------------------------------------- 1 | /*jshint node: true */ 2 | /*global describe, it, beforeEach, afterEach */ 3 | 'use strict'; 4 | var expect = require('expect.js'); 5 | var joi = require('joi'); 6 | var React = require('react'); 7 | var joiToPropType = require('../'); 8 | 9 | describe('joiToPropType', function () { 10 | it('is a function', function () { 11 | expect(joiToPropType).to.be.a('function'); 12 | }); 13 | it('returns a function', function () { 14 | expect(joiToPropType()).to.be.a('function'); 15 | }); 16 | describe('during validation', function () { 17 | var warn, warnings; 18 | beforeEach(function () { 19 | warn = console.warn; 20 | warnings = []; 21 | console.warn = function () { 22 | warnings.push(Array.prototype.join.call(arguments, ' ')); 23 | }; 24 | }); 25 | afterEach(function () { 26 | console.warn = warn; 27 | }); 28 | describe('when the prop is invalid', function () { 29 | it('makes React log a warning', function () { 30 | var schema = joi.forbidden(); 31 | var value = true; 32 | var expectedWarning = schema.validate(value).error.message; 33 | expect(expectedWarning.replace(/\w/g, '')).not.to.be.empty(); 34 | var Component = React.createClass({ 35 | propTypes: { 36 | invalid: joiToPropType(schema) 37 | }, 38 | render: function () { 39 | return React.createElement('div'); 40 | } 41 | }); 42 | React.renderToString(React.createElement(Component, {invalid: true})); 43 | expect(warnings.length).to.equal(1); 44 | expect(warnings[0]).to.contain(expectedWarning); 45 | }); 46 | }); 47 | describe('when the prop is missing', function () { 48 | it('makes React log a warning', function () { 49 | var schema = joi.any().required(); 50 | var expectedWarning = schema.validate(undefined).error.message; 51 | expect(expectedWarning.replace(/\w/g, '')).not.to.be.empty(); 52 | var Component = React.createClass({ 53 | propTypes: { 54 | invalid: joiToPropType(schema) 55 | }, 56 | render: function () { 57 | return React.createElement('div'); 58 | } 59 | }); 60 | React.renderToString(React.createElement(Component)); 61 | expect(warnings.length).to.equal(1); 62 | expect(warnings[0]).to.contain(expectedWarning); 63 | }); 64 | }); 65 | describe('when the prop is valid', function () { 66 | it('nothing happens', function () { 67 | var schema = joi.any(); 68 | var Component = React.createClass({ 69 | propTypes: { 70 | valid: joiToPropType(schema) 71 | }, 72 | render: function () { 73 | return React.createElement('div'); 74 | } 75 | }); 76 | React.renderToString(React.createElement(Component, {valid: true})); 77 | expect(warnings.length).to.equal(0); 78 | }); 79 | }); 80 | }); 81 | }); --------------------------------------------------------------------------------