MongooseJS Validation

The docs on MongooseJS can be really unclear at times, here are some pointers on validation that may help (note this is valid as of MongooseJS 1.3.3)

Making a field unique:


var User = new Schema({
email: { index: {unique: true} }
});

Note: If your collection exists in Mongo, you need to drop the collection before schema changes kick in. Otherwise, you’ll see duplicated fields and wonder why it doesn’t work. If all is working, when you call User.save(), Mongoose will throw an error such as – ‘E11000 duplicate key error index: …’ if the field is duplicated.

Validating a schema field:

When defining a field you can do some basic validation through Mongoose, such as indicating a max or min for numbers, specifying the field is required, or matching to a regular expression. For more advanced validation you can provide a call to function which is evaluated after any defaults for a field is applied. In almost all instances, validating through a function seems to be the most robust approach, even for checking a required field, regex, or max/min values. Assuming you already defined a validation function for your schema field, you can associate it two ways,


var User = new Schema({
email: { index: { unique: true }, validate: [ isValidEmail, 'Please provide a valid email' }
});

or


User.path('email').validate( isValidEmail(value), 'Please provide a valid email' );

Validating a virtual schema field:

If you are using virtuals within a schema, it would be nice to validate them from the model. There are a couple approaches that won’t work. Chaining a .validate function onto a virtual schema field definition, throws a compiler error when launching node. Likewise, attempting to attach a .validate function using Schema.path() will also fail as virtuals are not accessible using path(). The best solution is to use middleware from within the model to validate the virtual attribute.


User.pre('save', function (next) {
if (this.virtualfield) {
next ( new Error('This field is required.') );
} else {
next ();
}
});

Tags: ,

1 Comment

rssComments RSS   transmitTrackBack Identifier URI

Validating virtual fields with middleware isn’t the ideal solution, since validation doesn’t take place at the same time as normal validators (for example, if you are displaying errors for individual form fields, you won’t catch the error on any field that is associated with a virtual field), and the model data is lost once the middleware validator fails (in case of forms, if you load field values from a model, you will have blank fields). But I guess it’s better than nothing.


Comment by Branko on July 10, 2011 3:05 pm


addLeave a comment