Validators for mongoose models utilising validator.js
Mongoose Validator simply returns Mongoose style validation objects that utilises validator.js for the data validation.
Version 1.0.x has been refactored to support a simpler interface and also validator.js 3.0.x
NOTE: If you were using 0.2.x with your projects please be aware that upgrading to 1.0.x will break them. As of validator.js 3.0.x - many of the avilable validators have been changed. For example, 'regex' validator no longer exists and has been replaced with 'matches'.
$ npm install mongoose-validator --save
If you need to install the 0.2.2 release, use the following command:
$ npm install email@example.com --save
More details on 0.2.2 can be found here
var mongoose = require'mongoose';var validate = require'mongoose-validator';var nameValidator =validatevalidator: 'isLength'arguments: 3 50message: 'Name should be between 3 and 50 characters'validatevalidator: 'isAlphanumeric'passIfEmpty: truemessage: 'Name should contain alpha-numeric characters only';var Schema =name: type: String required: true validate: nameValidator;
Error objects are returned as normal via Mongoose.
Arguments to be passed to the validator. These can either be an array of arguments (for validators that can accept more than one i.e.
isLength), or a single argument as any type.
Some of the validator.js validators require a value to check against (isEmail, isUrl etc). There may be instances where you don't have a value to check i.e. a path that is not required and as such these few validators return an false value causing validation to fail. This can now be bypassed by setting the
Set the error message to be used should the validator fail. If no error message is set then mongoose-validator will attempt to use one of the built-in default messages, if it can't then a simple message of 'Error' will be returned.
Mongoose Validator can use the validator.js
matches method, however, it's worth noting that the regex can be passed in 2 ways - as per the validator.js documentation, firstly they can be passed as a literal:
validatevalidator: 'matches'arguments: /^[a-zA-Z\-]+$/i;
or as a string with a further argument containing any required modifiers:
validatevalidator: 'matches'arguments: '^[a-zA-Z\-]+$' 'i';
Custom validators can also be added - these are then added to the validator.js object. NOTE: Validator.js converts all values to strings internally for built-in validators - however custom validators do not do this. This allows you to create custom validators for checking all types such as arrays and objects.
// extend([method name], [validator], [default error message])var extend = require'mongoose-validator'extend;extend'isString'return ObjectprototypetoStringcallval === '[object String]';'Not a string';
Custom validators are called normally:
Custom validator can be passed directly as a function:
validatereturn val > 0;message: 'Count must be a positive number.'
NOTE: As per validator.js documentation, the currently tested value is accessed through the first argument that is automatically passed to the validator function.