yav

0.1.0 • Public • Published

yav - Yet another javascript validator

yav is a validator that provides:

  • common validators
  • asynchronous validation
  • conditional validation

Change Log

  • 0.1.0 async validation (API breaking change), added validateLength and validateNumericality

Example Usage

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

Parameter Functions

bind(parameters)

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(key, value)

Set a parameter by key.

validator.set('name', 'Robert');

get(key)

Retrieves a parameter by key.

validator.get('name');

Validation Functions

validate()

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()

Error Functions

addError(fieldName, errorMessage)

Add an error a specific field.

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

addGlobalError(errorMessage)

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.');

errors()

Retrieve the errors from the validator.

var errors = validator.errors();

globalErrors()

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

var errors = Validators.getGlobalErrors();

isValid()

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

validator.isValid();

clearErrors()

Remove all errors from the validator.

Validators.clearErrors();

Validators

validatePresence(fieldName, options)

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

Options

  • if, unless, message standard options

validateFormat( fieldName, 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

validateInclusion( fieldName, options )

validator.validateInclusion('field1', { 'in': ['1', '2', '3'] });

Options

  • in ( required ) an array of possible values
  • if, unless, allow_blank, message standard options

validateExclusion( fieldName, options )

validator.validateExclusion('field1', { 'in': ['1', '2', '3'] });

Options

  • in ( required ) an array of excluded values
  • if, unless, allow_blank, message standard options

validateNumericality( fieldName, options )

validator.validateNumericality('field1');
validator.validateNumericality('field1', { integer: true });
validator.validateNumericality('field1', { greaterThan: 3});
validator.validateNumericality('field1', { lessThanOrEqualTo: 50 });

Options

  • min, max Number
  • if, unless, allow_blank, message standard options

validateLength( fieldName, options )

validator.validateLength('field1', { max: 6, min: 3 });

Options

  • integer boolean
  • greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo Number
  • if, unless, allow_blank, message standard options

validateCustom( validationFunction, done )

validator.validateCustom(function(validator, done){
  if (validator.get('firstName') === 'Mickey' && validator.get('lastName') === 'Mouse') {
    validator.addGlobalError('That\'s not your real name!');
    done(); // let the validator know the validation is complete DON'T forget this
  }
});

Conditional Validation

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(function() {
  validator.valid();  // ==> true   field1 is only required when field2's value is not 'special'
});

Readme

Keywords

none

Package Sidebar

Install

npm i yav

Weekly Downloads

0

Version

0.1.0

License

MIT

Last publish

Collaborators

  • whap