Using the Validation Framework
One of the most important aspects of the mongoose
module is validation against a defined model. Mongoose provides a built-in validation framework that only requires you to define validation functions to perform on specific fields that need to be validated. When you try to create a new instance of a document, read a document from the database or save a document, the validation framework calls your custom validation methods and returns an error if the validation fails.
The validation framework is actually very simple to implement. You call the validate()
method on the specific path in the Model
object that you want to apply validation to and pass in a validation function. The validation function accepts the value ...
Get Node.js, MongoDB, and AngularJS Web Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.