node.js - 如何根据同一模式中的其他字段在猫鼬模式中进行字段验证?
问题描述
假设我们有:
const mealSchema = Schema({
_id: Schema.Types.ObjectId,
title: { type: string, required: true },
sauce: { type: string }
});
如果,我们如何sauce
强制执行title === "Pasta"
?验证也需要进行更新。
我知道解决方法是
- 寻找
- 手动更新
- 然后保存
但风险在于,如果我添加一个新属性(比如说“价格”),我也会忘记在解决方法中手动更新它。
解决方案
文件验证器
Mongoose 有几个内置的验证器。
所有 SchemaTypes 都有内置的必需验证器。所需的验证器使用 SchemaType 的 checkRequired() 函数来确定值是否满足所需的验证器。
数字有最小和最大验证器。
字符串有 enum、match、minlength 和 maxlength 验证器。
对于你的情况,你可以做这样的事情
const mealSchema = Schema({
_id: Schema.Types.ObjectId,
title: { type: string, required: true },
sauce: {
type: string,
required: function() {
return this.title === "pasta"? true:false ;
}
}
});
如果内置验证器不够用,您可以定义自定义验证器以满足您的需求。
通过传递验证函数来声明自定义验证。您可以在SchemaType#validate()中找到有关如何执行此操作的详细说明 。
更新验证器
this
指使用文档验证时正在验证的文档。但是,在运行更新验证器时,正在更新的文档可能不在服务器的内存中,因此默认情况下 this
未定义 的值。那么,解决方案是什么?
context 选项允许您将更新验证器中 this 的值设置为基础查询。
在您的情况下,我们可以执行以下操作:
const mealSchema = Schema({
_id: Schema.Types.ObjectId,
title: { type: string, required: true },
sauce: { type: string, required: true }
});
mealSchema.path('sauce').validate(function(value) {
// When running update validators with
// the `context` option set to 'query',
// `this` refers to the query object.
if (this.getUpdate().$set.title==="pasta") {
return true
}else{
return false;
}
});
const meal = db.model('Meal', mealSchema);
const update = { title:'pasta', sauce:false};
// Note the context option
const opts = { runValidators: true, context: 'query' };
meal.updateOne({}, update, opts, function(error) { assert.ok(error.errors['title']); });
不确定这是否回答了您的问题。希望这为您的最终解决方案增加一些价值。
尚未对其进行测试,如果此解决方案需要升级,请建议进行编辑。
希望这可以帮助。
推荐阅读
- python - 如何将我现有的程序绑定到 Pulumi?
- python - 从没有索引的dict导入数据框
- java - 如何验证@RequestHeader在spring boot中是否为null?
- python - 逐渐添加 Kivy 小部件
- azure-aks - 如何将第二个 ACR 附加到我的 AKS 群集?
- javascript - 无论成功与否,如何在 $.post 之后运行 Javascript 代码?
- excel - 如果天数超过一年,如何让 ExCel 生成简单的价值与天数图?
- python - 根据数学“来回”模式匹配从python主列表中制作子列表
- python - Python方法不起作用我需要做什么?
- docker - Dockerfile 错误没有这样的文件或目录”