yav

yav is a validator that provides:

Yav - Yet another javascript validator

I'm scratching my own itch here. I can't seem to find a javascript validator that has all the features I need.

var Yav = require('Yav'); // for nodejs 
var validator = Yav.new();
validator.validatePresence('field1', { message: 'You forgot field1!' })
  .validatePresence('field2', { message: 'You forgot field2!' })
  .bind({field1: 'value'})
  .validate();
 
validator.errors();  /* => { field2: ['You forgot field2!'] } */

Set the parameters to validate against. This will replace anything other previously set parameters.

validator.bind({ name: 'Robert', email: 'bob@example.com', age: '32' });

Set a parameter by key.

validator.set('name', 'Robert');
 
validator.set('user', { name: 'Robert', email: 'bob@example.com' });

Retrieves a parameter by key. Use of an array for a key is possible to retrieve "deep" parameters.

validator.get('name');
 
validator.get(['user', 'name']);

Trigger validation. Once the validations have been added and parameters have been bound/set validate() initiates validation.

Any errors from previous validations will be cleared on each call to validate().

validator.validate()

Add an error a specific field.

validator.addError('email', 'The email address is invalid.');

Add a general Error. This should be used when an error doesn't correspond to a single field.

validator.addGlobalError('The selected item cannot be order in the quantity indicated.');

Retrieve the errors from the validator.

var errors = validator.errors();

Retrieve the errors that didn't correcspond to a specific field.

var errors = Validators.getGlobalErrors();

Returns true when there are no global errors or any errors associated any field.

validator.isValid();

Remove all errors from the validator.

Validators.clearErrors();
validator.validatePresence('field1', { message: 'You forgot field1!' });

Options

  • if, unless, message standard options
validator.validateFormat('field1', { 'with': /123/ });

Options

  • with ( required ) a regular expression that will be matched against the field
  • if, unless, allow_blank, message standard options
validator.validateInclusion('field1', { 'in': ['1', '2', '3'] });

Options

  • in ( required ) an array of possible values
  • if, unless, allow_blank, message standard options
validator.validateExclusion('field1', { 'in': ['1', '2', '3'] });

Options

  • in ( required ) an array of excluded values
  • if, unless, allow_blank, message standard options
validator.validateCustom(function(validator){
  if (validator.get('firstName') === 'Mickey' && validator.get('lastName') === 'Mouse') {
    validator.addGlobalError('That\'s not your real name!');
  }
});

Every validator except validateCustom accepts if and unless options. These two options take a function that should return true or false. The function will be passed the validator object. When the if option is used the validation will only be run when the corresponding function returns true. Likewise when the unless options is used the validation will only run when the corresponding function returns false. The if and unless options may be used together.

validator.validatePresence(
  'field1', 
  { 
    "message": 'You forgot field1!', 
    "if"function(v){ return v.get('field2') !== 'special'; } 
  }
)
.bind({field1: '', field2: 'special'})
.validate();
  
validator.valid();  // ==> true   field1 is only required when field2's value is not 'special' 
  • validateNumericality
  • validateLength
  • validateUrl ?
  • validateEmail ?
  • validateIpAddress ?