Skip to content

Instantly share code, notes, and snippets.

@silently
Created September 21, 2012 16:23
Show Gist options
  • Save silently/3762461 to your computer and use it in GitHub Desktop.
Save silently/3762461 to your computer and use it in GitHub Desktop.
[Mongoose] multiple validation errors on Document#save
// package.json file
////////////////////
{
"name": "test",
"version": "0.0.1",
"dependencies": {
"mongoose": "3.1.2"
}
}
// app.js
////////////////////
mongoose = require('mongoose')
var db = mongoose.createConnection('localhost', 'multi_validation_test');
/* Schema definition */
var userSchema = new mongoose.Schema({
email : { type: String, unique: true, required: true, trim: true },
password : { type: String, required: true, trim: true },
})
/* Validations */
userSchema.path('password').validate(function(value) { return value.length >= 6 }, 'minLength')
userSchema.path('password').validate(function(value) { return value.length >= 5 }, 'minLength2')
userSchema.path('password').validate(function(value) { return /[0-9]/.test(value) }, 'noNumberFound')
/* Model declaration */
var User = db.model('User', userSchema)
var u = new User({ email: "jean-luc@godard.fr", password: "lefou" })
u.save(function(err, user) {
console.log(err)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment