ebi-entity

Base constructor function for the entities, according to the EBI architectural pattern. Provides very simple validation on these objects.

npm install ebi-entity
20 downloads in the last week
40 downloads in the last month

Base constructor function for the entities, according to the EBI architectural pattern. Provides very simple validation on these objects.

Usage

Create a child Entity constructor function by using inheritance.

function Person(data) {
  Entity.call(this, data);
}
util.inherits(Person, Entity);

Copy the method validates to the new entity.

Person.validates = Entity.validates;

Add validations

Person.validates(['name', 'email'], {
  presence: true
});

Create an entity object and initialize it

var person = new Person({
  name: 'alfred'
});

Check if the object is valid

person.isValid(); // returns false

Check the errors

console.log(person.errors); // prints: { email: [ 'Required' ] } ] }

Available validations

Presence

Person.validates(['name'], {
  presence: true
});

Format

Person.validates(['name'], {
  format: { with: /\S/ }
});

In

Person.validates(['name'], {
  in: ['Alfred', 'Jeeves', 'Bernard']
});

Numericality

Person.validates(['age'], {
  numericality: true
});

This validation may be used with some additional options (greaterThan, lessThan, greaterThanOrEqualTo, lessThanOrEqualTo and onlyInteger)

Person.validates(['age'], {
  numericality: {
    greaterThan: 0,
    lessThan: 1000,
    onlyInteger: true
  }
});

Each of the failed criteria adds an error message to the entity object.

Contribute

Add a new validation

  1. Test! (using mocha and expect.js)

  2. Write the predicate with all its options

  3. Add the error message(s) for the newly created validation

Future improvements

  • Add more validatins;
  • Add the error messages outside the predicate code (I would pleased to make them pure predicates, with no side effects);
  • Perhaps, use the same options on different kinds of validations;
npm loves you