React propTypes
react-entity - Create entities base onThis package let you create entities with schema validator like React PropTypes.
Installing
$ npm install react-entity
Using
Sample Entities
; static SCHEMA = field: PropTypesstring otherField: type: PropTypesnumber defaultValue: 10 static SCHEMA = children: validator: PropTypes type: MyEntity
Get default values
const niceInstance = ;console; // { field: undefined, otherField: 10 }console; // {}
Validations
const buggedInstance = field: 10 otherField: 'value' ;console; // { field: 10, otherField: 'value' }console; /* or buggedInstance.getErrors() -- but... getErrors also includes children errors { field: { errors: [ 'Invalid undefined `field` of type `number` supplied to `MyEntityEntity`, expected `string`.' ] }, otherField: { errors: [ 'Invalid undefined `otherField` of type `string` supplied to `MyEntityEntity`, expected `number`.' ] } }*/
Validate on change value
const otherInstance = field: 'myString' ;console; // {}console; // true otherInstancefield = 1;console; // {field: { errors: [ 'Invalid undefined `field` of type `number` supplied to `MyEntityEntity`, expected `string`.' ] }}console; // false
Parse children to Entity
const fatherInstance = children: field: 'A' otherField: 2 field: 'B' otherField: 3 console; //An instance of MyEntityconsole;//{ field: 'B', otherField: 3 }
Clean unexpected values
const anotherInstance = field: 'myString' fake: 'fake' ;console; // { field: 'myString', otherField: 10 }
To understand the validators React PropTypes
Well known issues
- Create helpers for relationships validations(Like, mininum, maximum)
- Create identifier and equal comparison