-
Book Overview & Buying
-
Table Of Contents
Mongoose for Application Development
By :
Mongoose offers a number of different ways to create custom validators.
If you only need one piece of validation for a given data item, you can simply specify a function and return true if the validation is passed, and false if it fails.
For example, if we want our usernames to be at least five characters long, we can create a function like the following:
var lengthValidator = function(val) {
if (val && val.length >= 5){
return true;
}
return false;
};The function is then referenced in our schema using the validate key:
name: {type: String, required: true, validate: lengthValidator }This is a very quick and easy way of adding custom validation. It is fine if you just want to check if its okay or not okay as it is reported as an error, but in the error collection the error type comes through as undefined.
If you want a bit more feedback and granularity to your...
Change the font size
Change margin width
Change background colour