Create your domain entities with reactive validation based on React propTypes.

https://github.com/scup/speck

Getting Started

Installing

      
$ npm install speck-entity
      
      

Sample Entities

        
import { PropTypes } from 'react';
import Speck from 'speck-entity';

class MyEntity extends Speck {
  static SCHEMA = {
    field: PropTypes.string,
    otherField: {
      validator: PropTypes.number,
      defaultValue: 10
    }
  }
}

class FatherEntity extends Speck {
  static SCHEMA = {
    children: {
      validator: PropTypes.arrayOf(PropTypes.instanceOf(MyEntity)),
      type: MyEntity
    }
  }
}
        
        

Get default values

        
const niceInstance = new MyEntity();
console.log(niceInstance.fetch());
// { field: undefined, otherField: 10 }
console.log(niceInstance.errors);
// {}
        
        

Validations

        
const buggedInstance = new MyEntity({ field: 10, otherField: 'value' });
console.log(buggedInstance.fetch()); // { field: 10, otherField: 'value' }
console.log(buggedInstance.errors);
/* 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 = new MyEntity({ field: 'myString' });
console.log(otherInstance.errors); // {}
console.log(otherInstance.valid); // true

otherInstance.field = 1;
console.log(otherInstance.errors); // {field: { errors: [ 'Invalid undefined `field` of type `number` supplied to `MyEntityEntity`, expected `string`.' ] }}
console.log(otherInstance.valid); // false
        
        

Parse children to Entity

        
const fatherInstance = new FatherEntity({
  children: [{
    field: 'A',
    otherField: 2
  }, {
    field: 'B',
    otherField: 3
  }]
})
console.log(fatherInstance.children[0]); //An instance of MyEntity
console.log(fatherInstance.children[1].fetch());
//{ field: 'B', otherField: 3 }
        
        

Clean unexpected values

        
const anotherInstance = new MyEntity({ field: 'myString', fake: 'fake' });
console.log(anotherInstance.fetch()); // { field: 'myString', otherField: 10 }