Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best place to handle data validation with mongoose and express

Maybe there is not a definitive answer here but I would like to know where to handle data validation when dealing with express.js and mongoose. Which of the following is the best practice (I currently use a combination and it's starting to feel very clumsy):

  1. the Model (mongoose)
  2. the Controller / Route (express)

Some older posts I have read are:

  • this;
  • this;
  • and, this;

but conflicting answers just add to the confusion. Maybe it simply isn't clear cut, in which case is one a better option?

like image 695
adamK Avatar asked Dec 09 '13 07:12

adamK


People also ask

Does mongoose have built in validation?

Mongoose has several built-in validators. All SchemaTypes have the built-in required validator. The required validator uses the SchemaType's checkRequired() function to determine if the value satisfies the required validator. Numbers have min and max validators.

What is the need of Mongoose .how it is useful for validation?

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node. js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB. MongoDB is a schema-less NoSQL document database.

Can I use MongoDB and mongoose at the same time?

Yes, we're actually using multiple drivers, in a production application. We need connections to multiple databases, and mongoose is only able to connect to one DB. So we use MongoDB for the connections to the secondary databases. It should be the same using MongoJS instead.

What is the difference between Mongoose and express?

Mongoose, by the way, is simply a layer of abstraction on top of MongoDB, the same way Express is a layer of abstraction on top of Node. We'll require Mongoose at the top of our JS file that starts the server and then invoke Mongoose's connect() method.


2 Answers

When using mongoose I would push most of my validation logic to the mongoose model/schema. You can use mongoose-validator which is just a wrapper around node-validator for simple model validation. If you need validation against other models or more complex logic in the validation you can write your own custom mongoose pre validate or post validate hook (see mongoose middleware).

An additional benefit you gain when using mongoose to validate your model is that mongoose adds an error property to your model which can be accessed via model.errors[property]. This property can be used for validation error messages on the web or for a service client.

When writing more/very complex software tying the validation to the model may become a problem. But I'd deal with this problem when it arises. Since JavaScript has functions as first class citizens your validation functions still can be reused even in these complex situations.

like image 99
saintedlama Avatar answered Sep 30 '22 04:09

saintedlama


The mongoose validator is a great place to start on a model level, but if you need to have controller specific validation, I use the following code in a utils.js file:

var async = require('async')
exports.validator = function (reqProps, props, mongoEnt, next) {
    var propsErr = [];
    var mongoErr = {};

    async.parallel([function (cb) {
        reqProps.forEach(function (rp) {
            if (!props[rp])propsErr.push(rp);
        })
        cb()
    }, function (cb) {
        if (mongoEnt != null) {
            var test = new mongoEnt(props);

            test.validate(function (err) {
                mongoErr = err;
                cb();
            });
        } else {
            mongoErr = null;
            cb();
        }
    }], function (err, result) {
        if (propsErr.length != 0) {
                return next(new Error('The following props were not included: ' + propsErr));
        } else if(mongoErr != null) {
                return next(new Error('The following prop was not included: ' +mongoErr.errors[Object.keys(mongoErr.errors).pop()].path));
        } else {
            return next(null);
        }
    })
}

This allows me to both validate using the mongoose validator and check for the additional props that I include in the reqProps property in one line of code. Though this is only checking for required properties, you could easily extend it for your own validation scheme.

An example of usage for this code:

var Person = mongoose.model('Person')

exports.addUSCitizen = function(props, next){ 
    utils.validator(['ssn'], props, Person, function (err) {
        if(err) return next(err);
        //do something using props.ssn
    })
}
like image 38
oconnecp Avatar answered Sep 30 '22 03:09

oconnecp