gpt4 book ai didi

validation - 验证多个 Mongoose 模式属性?

转载 作者:搜寻专家 更新时间:2023-10-31 22:31:17 24 4
gpt4 key购买 nike

我正在尝试在 Nodejs/Mongoose 中做经典的事情,确保用户的用户名与其密码不同。

我认为使用单独的验证函数会很好,但我不知道该怎么做。

到目前为止,我使用了 model code from Alex Young's Notepad tutorial .他创建了一个我重新使用的虚拟 password 属性。

我有如下基本验证:

function validatePresenceOf(value) {
return value && value.length;
}

User = new Schema({
'username': {
type: String,
validate: [
validatePresenceOf, 'a username is required',
],
index: { unique: true }
},
});

我如何允许验证器访问其他属性?

最佳答案

您可以通过 this.propertyToBeCalled 调用架构的其他属性。

schema.path('name').validate(function(v) {
if (v === this.password) {
return false;
} else {
return true;
}
}, 'my error type');

或者类似的东西。

关于validation - 验证多个 Mongoose 模式属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7369326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com