Javascript 节点js Joi验证条件不允许键

Javascript 节点js Joi验证条件不允许键,javascript,node.js,mongodb,express,joi,Javascript,Node.js,Mongodb,Express,Joi,我是nodeJS的新手,所以如果我的问题听起来很愚蠢,请原谅我, 我想支持基于另一个键的条件有效载荷 price: Joi.when('pricing', { is: 'VARIABLE', then: Joi.number() .min(1) .max(1000) .required(), otherwise: // prevent adding price in the payload }) 如果定价等于“变量”,我希

我是nodeJS的新手,所以如果我的问题听起来很愚蠢,请原谅我, 我想支持基于另一个键的条件有效载荷

price: Joi.when('pricing', {
    is: 'VARIABLE',
    then: Joi.number()
        .min(1)
        .max(1000)
        .required(),
    otherwise: // prevent adding price in the payload
})
如果定价等于“变量”,我希望用户提供价格值,否则会阻止用户在有效负载中提供价格

从中,我们可以使用
Joi.probled()
Joi.any().probled()
来禁止模式中的任何键。在您的情况下,您的最终架构将是:

price: Joi.when('pricing', {
  is: 'VARIABLE',
  then: Joi.number().min(1).max(1000).required(),
  otherwise: Joi.forbidden()
})