Node.js 不满足条件时,不需要Joi验证-字段

Node.js 不满足条件时,不需要Joi验证-字段,node.js,validation,joi,Node.js,Validation,Joi,我正在nodeJs中编写一个更新端点。我想确保,如果选中某些标志,则不允许添加其他字段 all_restaurant: Joi.boolean().required(), merchant_id: Joi.when('all_restaurant',{'is':false, then: Joi.string().required(), otherwise:Joi.disallow()}) 如果all_restaurant为true,则当用户尝试包含任何商户id

我正在nodeJs中编写一个更新端点。我想确保,如果选中某些标志,则不允许添加其他字段

all_restaurant: Joi.boolean().required(),
merchant_id: Joi.when('all_restaurant',{'is':false, 
                then: Joi.string().required(), otherwise:Joi.disallow()})
如果all_restaurant为true,则当用户尝试包含任何商户id时,我需要抛出一个错误,但它仍然允许我进行更新。 我尝试在字段后面和顶层使用strict(),但没有任何效果。有什么办法可以做到这一点吗?

您可以使用代替Joi。不允许:

const schema = Joi.object({
    all_restaurant: Joi.boolean().required(),
    merchant_id: Joi.string().when('all_restaurant', {
        is: false, 
        then: Joi.required(), 
        otherwise: Joi.forbidden()
    })
})
顾名思义,“禁止”将钥匙标记为“禁止”