Mongoose验证中每个字段有多个错误

时间:2015-11-30 11:49:34

标签: javascript node.js mongodb validation mongoose

有没有人在Mongoose验证中获取每个字段的所有错误?现在,当某个字段出现错误时,该字段的验证将停止,因此,您需要重新提交表单才能看到下一个错误。

此行为可能会在将来修复(https://github.com/Automattic/mongoose/issues/2612),但在此之前,我希望看到一种解决方法,让我可以使用validation方法,例如:

User.schema.path('hashed_password').validate(function (value) {
    if ( ! something)
        this.invalidate('password', 'Something is wrong with the password');
});

注意:使用validation方法,我可以将错误绑定到任何字段,而不仅仅是当前验证的字段。现在没有插件可以让我这样做(我测试了 mongoose-validator-all mongoose-validate-all ,这些插件正在使用不同的策略)。

0 个答案:

没有答案
相关问题