【问题标题】:Node js Joi validation conditional disallow keysNode js Joi验证条件禁止键
【发布时间】:2020-07-15 17:35:18
【问题描述】:

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

price: Joi.when('pricing', {
    is: 'VARIABLE',
    then: Joi.number()
        .min(1)
        .max(1000)
        .required(),
    otherwise: // prevent adding price in the payload
})

如果定价等于“VARIABLE”,我希望用户提供价格值,否则防止用户在有效负载中提供价格。

【问题讨论】:

    标签: javascript node.js mongodb express joi


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-18
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 2021-11-03
      • 2017-08-05
      • 2019-03-23
      相关资源
      最近更新 更多