Validation encapsulated in this abstract class.

Basically validation configurators is just class methods, which adds validations configs to AbstractClass._validations. Each of this validations run when obj.isValid() method called.

Each configurator can accept n params (n-1 field names and one config). Config is Object depends on specific validation, but all of them has one common part: message member. It can be just string, when only one situation possible, e.g. Post.validatesPresenceOf('title', { message: 'can not be blank' });

In more complicated cases it can be Hash of messages (for each case): User.validatesLengthOf('password', { min: 6, max: 20, message: {min: 'too short', max: 'too long'}});

Source code
Class methods Instance methods Helper methods


Declared as getConfigurator('presence');

Validate presence. This validation fails when validated field is blank.

Default error message "can't be blank"

example Post.validatesPresenceOf('title')
example Post.validatesPresenceOf('title', {message: 'Can not be blank'})

see helper/validatePresence

Source code


Declared as getConfigurator('length');

Validate length. Three kinds of validations: min, max, is.

Default error messages:

  • min: too short
  • max: too long
  • is: length is wrong

example User.validatesLengthOf('password', {min: 7});
example User.validatesLengthOf('email', {max: 100});
example User.validatesLengthOf('state', {is: 2});
example `User.validatesLengthOf('nick', {min: 3, max: 15});

see helper/validateLength

Source code


Declared as getConfigurator('numericality');

Validate numericality.

example User.validatesNumericalityOf('age', { message: { number: '...' }});
example User.validatesNumericalityOf('age', {int: true, message: { int: '...' }});

Default error messages:

  • number: is not a number
  • int: is not an integer


see helper/validateNumericality

Source code


Declared as getConfigurator('inclusion');

Validate inclusion in set

example User.validatesInclusionOf('gender', {in: ['male', 'female']});

Default error message: is not included in the list

see helper/validateInclusion

Source code


Declared as getConfigurator('exclusion');

Validate exclusion

example Company.validatesExclusionOf('domain', {in: ['www', 'admin']});

Default error message: is reserved

see helper/validateExclusion

Source code


Declared as getConfigurator('format');

Validate format

Default error message: is invalid

see helper/validateFormat

Source code


Declared as getConfigurator('custom');

Validate using custom validator

Default error message: is invalid

see helper/validateCustom

Source code


Declared as getConfigurator('custom',

Validate using custom async validator

Default error message: is invalid

see helper/validateCustom

Source code


Declared as getConfigurator('uniqueness',

Validate uniqueness

Default error message: is not unique

see helper/validateUniqueness

Source code


Declared as function (callback)

This method performs validation, triggers validation hooks. Before validation obj.errors collection cleaned. Each validation can add errors to obj.errors collection. If collection is not blank, validation failed.

Warning! This method can be called as sync only when no async validation configured. It's strongly recommended to run all validations as asyncronous.

param Function callback called with (valid)
return Boolean true if no async validation configured and all passed

example ExpressJS controller: render user if valid, show flash otherwise

user.isValid(function (valid) {
    if (valid) res.render({user: user});
    else res.flash('error', 'User is not valid'), console.log(user.errors), res.redirect('/users');
Source code


Declared as function validatePresence(attr, conf, err)

Presence validator

Source code


Declared as function validateLength(attr, conf, err)

Length validator

Source code


Declared as function validateNumericality(attr, conf, err)

Numericality validator

Source code


Declared as function validateInclusion(attr, conf, err)

Inclusion validator

Source code


Declared as function validateExclusion(attr, conf, err)

Exclusion validator

Source code


Declared as function validateFormat(attr, conf, err)

Format validator

Source code


Declared as function validateCustom(attr, conf, err, done)

Custom validator

Source code


Declared as function validateUniqueness(attr, conf, err, done)

Uniqueness validator

Source code


Declared as function blank(v)

Return true when v is undefined, blank array, null or empty string otherwise returns false

param Mix v
returns Boolean whether v blank or not

Source code

© 1602 Software