显示 Mongoose 模型的多个验证错误
Display multiple validation errors for Mongoose model
如果我有这个模式:
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);
然后我创建一个这样的模型和 运行 验证函数:
var newUser = new User({name: '1da'});
newUser.validate(function(err) {
console.log(err.errors.name);
})
这仅记录第一条错误消息 'Name is too short'。但是,名称 属性 未通过这两个验证要求。有没有办法同时显示这两个错误消息?
谢谢
显然这个功能没有在 mongoose v3 中实现。
https://github.com/LearnBoost/mongoose/pull/1214#issuecomment-15746525
等v4稳定了,我再试试
到那时,这个模块似乎解决了这个问题:
如果我有这个模式:
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);
然后我创建一个这样的模型和 运行 验证函数:
var newUser = new User({name: '1da'});
newUser.validate(function(err) {
console.log(err.errors.name);
})
这仅记录第一条错误消息 'Name is too short'。但是,名称 属性 未通过这两个验证要求。有没有办法同时显示这两个错误消息?
谢谢
显然这个功能没有在 mongoose v3 中实现。
https://github.com/LearnBoost/mongoose/pull/1214#issuecomment-15746525
等v4稳定了,我再试试
到那时,这个模块似乎解决了这个问题: