yav is a validator that provides:
- common validators
- asynchronous validation
- conditional validation
- 0.1.0 async validation (API breaking change), added validateLength and validateNumericality
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!'] } */
});
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');
Retrieves a parameter by key.
validator.get('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 correspond 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.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
validator.validateLength('field1', { max: 6, min: 3 });
Options
-
integer
boolean -
greaterThan
,greaterThanOrEqualTo
,lessThan
,lessThanOrEqualTo
Number -
if
,unless
,allow_blank
,message
standard options
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
}
});
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'
});