React propTypes
Speck - Let you create your domain entities with reactive validation based onThis package let you create entities with schema validation based on React PropTypes.
Installing
$ npm install speck-entity
Using
Sample Entities
const Joi = const joiAdapter = 'joi' Joiconst Speck = static SCHEMA = field: otherField: validator: defaultValue: 10 static SCHEMA = children: validator: 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 }
Builder
When you need to create objects with custom verification like
const elementList = elements: type: 'product' name: true price: true type: 'default' isDefault: true ;
In such cases you can define a builder as follows:
{}ElementListSCHEMA = elements: validator: noop dataList ;
And use it like:
elementList someDependency
(note that you can pass custom dependencies to your child entities and latter access them on the builder)
By defining builder you tell Speck Entity that you take the responsibility of instansitating and returning a new object of the type which suits you the best. This is a powerful concept as it lets users dynamically create new types on the fly.
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
- Type builders and/or custom builders are not being applied on instance setters
Contextual validation
static SCHEMA = id: requiredProp1: requiredProp2: requiredProp3: static CONTEXTS = create: exclude: 'requiredProp2' 'requiredProp3' edit: include: 'id' 'requiredProp1' 'requiredProp2' onlyId: include: 'id' const myEntity = id: 1 ; const contextCreate = myEntity; console; // { requiredProp1: { errors: [ ... ] } } console; // false const contextEdit = myEntity; console; // { requiredProp1: { errors: [ ... ] }, requiredProp2: { errors: [ ... ] } } console; // false const contextOnlyId = myEntity console; // {} console; // true
Each context (create and edit in example above), could have include property OR exclude, the include property receives the properties that will be validated in this context, and the exclude property represents the properties that will be ignored on validation.
In the example, the create context, will only check the 'requiredProp1' and 'requiredProp2' fields, and the edit context will check 'requiredProp1', 'requiredProp2' and 'id' properties.
You can't combine include and exclude in the same context definition
Custom validation
You can validate your entity adding the property in fields and setting the new validator
static SCHEMA = id: requiredProp1: static CONTEXTS = create: fields: { ifobjfield === -1 return 'Error -1'; } const entity = id: 1 requiredProp1: -1 ; const contextValidated = entity; console; // undefined console; // { errors: [Error: Error -1] }
### Hooks
static SCHEMA = fieldWithHook: validator: hooks: { // data is the whole data of the instance // fieldName the current fieldName // DO WHATEVER YOU WANT } anotherFieldWithHook: validator: hooks: { return anotherField: datafieldName * 2 // if the afterSet hook returns an object is merged to data } anotherField: const myEntity = fieldWithHook: 'foo' anotherFieldWithHook: 'bar' anotherField: null ; myEntityanotherFieldWithHook = 10 //according to the after set hook anotherField newValue will be 20