Mongoose Validation

49 views
Skip to first unread message

Saransh Mohapatra

unread,
Jul 1, 2013, 10:13:53 AM7/1/13
to mongoo...@googlegroups.com
I have a Mongoose User Model with a virtual method for password with set method encrypting the password and get method also. I also have few validations running on it. I wanted to know few things
  • Are the validations run async, I mean if I have 5 validations are they run in parallel or in series. I tried to know using console.log but wasn't able to be sure, though I get the feeling its in series. If its in series, is there any way to run it parallel?
  • In the set method the password encryption is a async function with callback. And there is a validator to check if the encrypted password field is blank or not. Now my problem is the validation gets called before the password is encrypted. Is there any way that I could start other validations but wait for this particular validation to be run only after encryption.
Please help me with this....

Aaron Heckmann

unread,
Jul 8, 2013, 2:50:21 PM7/8/13
to mongoo...@googlegroups.com
All validations are run in parallel.

For async setters use a custom method:

yourSchema.methods.setPassword = function (pwd, cb) {
  var self = this;
  doSomethingAsync(pwd, function (err, res) {
    if (err) return cb(err);
    self.password = res;
    cb();
    // etc
  })
}


--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Aaron


Reply all
Reply to author
Forward
0 new messages