显示Mongoose模型的多个validation错误

如果我有这个模式:

var userSchema = Schema( {name : { type: String } }); userSchema.path('name').validate(function(value) { return value.length > 4; }, 'Name is too short'); userSchema.path('name').validate(function(value) { return hasNoNumbers(value); }, 'Name cannot have numbers'); var User = mongoose.model('User', userSchema); 

然后我创build一个这样的模型并运行validation函数:

 var newUser = new User({name: '1da'}); newUser.validate(function(err) { console.log(err.errors.name); }) 

这只logging第一个错误消息“名称太短”。 但是,名称属性不符合validation要求。 有没有办法显示这两个错误信息?

谢谢

显然这个function在mongoose的v3中没有实现。

https://github.com/LearnBoost/mongoose/pull/1214#issuecomment-15746525

当v4稳定后,我会再试一次。

在此之前,这个模块似乎解决了这个问题:

https://github.com/szdc/mongoose-validate-all