Home

Awesome

<img src="https://raw.githubusercontent.com/Rekord/rekord/master/images/rekord-color.png" width="60"> Rekord Validation

Build Status devDependency Status Dependency Status License Alpha

Rekord is a javascript REST ORM that is offline and real-time capable.

rekord-validation adds rules, expressions, transforms, and custom validation functionality.

Installation

The easiest way to install rekord-validation is through bower via bower install rekord-validation.

Example

// Simplest rule definition
var Task = Rekord({
  name: 'task',
  fields: ['name', 'details', 'done', 'done_at'],
  validation: {
    rules: {
      name: 'required|alpha_dash',
      details: '$custom', // custom function
      done: 'yesno',
      done_at: 'if:done:accepted|required|date_like'
    },
    required: true // the rules must pass to $save model instances
  },
  methods: {
    $custom: function(value, getAlias, specifiedMessage, chain) {
      if ( value.length > 140 ) {
        return 'Details can be no larger than 140 characters.'
      }
    }
  }
});

var t0 = new Task({name: '^', done: true, details: '0...141'});
t0.$save();
t0.$valid; // false
t0.$validationMessages; // array of below messages
t0.$validations; /* {
  name: 'name should only contain alpha-numeric characters, dashes, and underscores.',
  details: 'Details can be no larger than 140 characters.',
  done_at: 'done_at is required.'
} */

// You can use aliases to make messages friendlier
var Task = Rekord({
  name: 'task',
  fields: ['name', 'done', 'done_at'],
  validation: {
    rules: {
      name: 'required|alpha_dash',
      done: 'yesno',
      done_at: 'if:done:accepted|required|date_like'
    },
    aliases: {
      name: 'Task name',
      done: 'Task completed',
      done_at: 'Task completed at'
    }
  }
});

var t1 = new Task({name: '', done: false});
t1.$validate(); /* {
  name: 'Task name is required.'
} */

var t2 = new Task({name: 'task2', done: true, done_at: 'not date'});
t2.$validate(); /* {
  done_at: 'Task completed at must be a valid date.'
} */

// You can specify field level custom messages
var Task = Rekord({
  name: 'task',
  fields: ['name', 'done', 'done_at'],
  validation: {
    rules: {
      name: 'required|alpha_dash',
      done: 'yesno',
      done_at: 'if:done:accepted|required|date_like'
    },
    messages: {
      name: 'Task name is required and must be alpha-numeric and can contain dashes and hyphens.',
      done_at: 'When a task is completed, the completed date is required.'
    }
  }
});

var t3 = new Task({name: '?', done: true, done_at: 'not date'});
t3.$validate(); /* {
  name: 'Task name is required and must be alpha-numeric and can contain dashes and hyphens.',
  done_at: 'When a task is completed, the completed date is required.'
} */

// You can specify rule level custom messages
var Task = Rekord({
  name: 'task',
  fields: ['name', 'done', 'done_at'],
  validation: {
    rules: {
      name: {
        'required': false, // defaults to field level - then default
        'alpha_dash': 'Task name must be alpha-numeric and can contain dashes and hyphens.'
      },
      done: 'yesno',
      done_at: {
        'if:done:accepted': false, // defaults to field level - then default
        'required': 'Task completed date is required when the task is complete.',
        'date_like': 'Task completed date must be a valid date when the task is complete.'
      }
    },
    aliases: {
      name: 'Task name'
    }
  }
});

var t4 = new Task({name: '?', done: true, done_at: 'not date'});
t4.$validate(); /* {
  name: 'Task name must be alpha-numeric and can contain dashes and hyphens.',
  done_at: 'Task completed date must be a valid date when the task is complete.'
} */

var t5 = new Task({done: true});
t5.$validate(); /* {
  name: 'Task name is required.',
  done_at: 'Task completed date is required when the task is complete.'
} */

// There are even more specific ways to define rules - depending on the rule.
// Check out the tests!

Concepts

Rules

Simple

Relationship

Expressions

Expressions can be passed to rules (like date rules) and are generated on validation

Transforms

Transforms modify the value being validated so all subsequent rules use the modified values. Some transforms can apply the modified value back to the model after validation.