0.1.0 • Published 11 years ago

yav v0.1.0

Weekly downloads
2
License
-
Repository
github
Last release
11 years ago

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.

Example Usage

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!'] } */

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

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

get(key)

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

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 correcspond 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

validateCustom( validationFunction )

validator.validateCustom(function(validator){
  if (validator.get('firstName') === 'Mickey' && validator.get('lastName') === 'Mouse') {
    validator.addGlobalError('That\'s not your real name!');
  }
});

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

Validators To Implement

  • validateNumericality
  • validateLength
  • validateUrl ?
  • validateEmail ?
  • validateIpAddress ?
0.1.0

11 years ago

0.0.4

11 years ago

0.0.3

11 years ago

0.0.2

12 years ago

0.0.1

12 years ago