node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org ยป


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


Create a child Entity constructor function by using inheritance.

function Person(data) {, 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


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


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


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


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.


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;