首页 > 解决方案 > 如何根据同一模式中的其他字段在猫鼬模式中进行字段验证?

问题描述

假设我们有:

const mealSchema = Schema({
  _id: Schema.Types.ObjectId,
  title: { type: string, required: true },
  sauce: { type: string }
});

如果,我们如何sauce强制执行title === "Pasta"?验证也需要进行更新。

我知道解决方法是

  1. 寻找
  2. 手动更新
  3. 然后保存

但风险在于,如果我添加一个新属性(比如说“价格”),我也会忘记在解决方法中手动更新它。

标签: node.jsmongodbmongoosemongodb-querymongoose-schema

解决方案


文件验证器

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']); });


不确定这是否回答了您的问题。希望这为您的最终解决方案增加一些价值。

尚未对其进行测试,如果此解决方案需要升级,请建议进行编辑。

希望这可以帮助。


推荐阅读