A validation middleware for mongoose.
A validation middleware for mongoose.
npm install mongolate
Or you can clone the latest mongolate with git.
First, you should require the module.
var mongolate = require'mongolate';
Mongolate is aimed to be very easy to use. Supposing you have a schema called dogSchema, which has two field, name and age.
var dogSchema =name: Stringage: Number;
And you want to ensure that the name of the dog cannot be longer than 12 characters. You can simply do this.
ifnamelength > 12return 'Arent you tired of calling it?';;return null;;mongolatedogSchemaname: validateName;
The first argument of mongolate is the schema you want to add the validation to. And the second argument is an associated list whose key is a property of the schema and value the validation function. If you want to add an overall validation for the schema, that validation function will be the third argument. Note that either the second or the third argument is optional.
mongolateschema validator_alist overall_validator;
The argument passed to the validator functions in validator_alist is the property that it validates, these validators are called property validators.
The argument passed to the overall validator is the document itself. So you have to use the overall validator if the validation is associated with other properties or other models.
It cannot be easier to add an asynchronized validator. You only need:
ifnamelength > 12callback'Such a long name it has!';;callbacknull;;mongolateschemaname: validator: validateName async: true;
Each validator should validate only one aspect. If you want to validate both the length of the name and the spelling of the name, you should use two validators, which is as easy as you see.
mongolatedogSchemaname: validateLength validateSpelling;
The validation stops as the first fails, and the overall validator is called until all the other validations are done.
Any validation failure causes an Error to be passed to the callback function, whose message attribute is 'Validation Error'.
If the validation for name fails, and the error message is 'Too long', then
dogsaveerrmessage === 'Validation Error'; // Trueerrerrorsname === 'Too long'; // True;
1.Sorts of validators. There are two sorts of validators: property validators, and overall validators.
Property validators are used to validate one single property, and the argument passed to the validator is the property it validates.
Overall validators are used to validate the whole document, and the argument passed to the validator is the document itself.
//...;//...;mongolateschema prop: propertyValidator overallValidator;
2.Return value of validators. For a synchronized validator, it returns null or undefined if the validation passes, or a string indicating the error.
For an asynchronized validator, it finishes the validation by calling the callback passed to it as the second argument.
// A synchronized validator.ifprop === 'Good'return null;elsereturn 'NotGood';;;// An asynchronized validator (Uniqueness validation).myModelfindOne name: propif obj done'Duplicated';else done;;;
You can use a validator with default options by:
mongolateschema prop: validatorFunc ;
Or with specified options by: (taking async option as an example)
mongolateschema prop: validator: validatorFunc async: true ;
Note that while you are specifying options for an overall validator, you have to put it in an array as if you are using more than one validators, to distinguish from the property validators which is also an object.
mongolateschema validator: myOverall async: true ;
An asynchronized validator need not return the validation result immediately. However, it calls the callback function to finish its validation. The callback function is the second argument. And it must call the callback, or the validation hangs up forever.
// ...done; // Validation passed.// Ordone'Not a good value'; // Validation failed.;
More than one validators should be put into an array, so are overall validators.
mongolateschemamyprop: validator1 validator: validator2 async: trueoverall1 validator: overall2 async: true;