Confirm in mongoose without saving

Is it possible to run the mongoose validator without saving? Basically, wanting to make a dry move of change and making sure that it will save if I want:

myThing.validated_property = 5; try { myThing.check(); } catch(e) { console.log("nope!") } 
+8
mongoose
source share
2 answers

You can call validate in a Mongoose document to evaluate its validation rules:

 myThing.validated_property = 5; myThing.validate(function(err) { if (err) { console.log('nope!') } }); 
+11
source share

This will be the easiest and best answer you can get.

 myThing.prototype.save = myThing.prototype.validate 

signatures and callbacks are the same. When you finish testing, delete this line, and everything will work, except for saving to the database;

-6
source share

All Articles