【问题标题】:Joi nested object operationsJoi 嵌套对象操作
【发布时间】:2021-09-06 04:06:02
【问题描述】:

我有一个 Joi 模式类型:

const schema = Joi.object().keys({
  a: Joi.string(),
  b: Joi.string(),
  c: Joi.string()
})

现在,我想添加一个条件,如果 a 不存在,那么 bc 都应该存在。我知道有object.and()object.or() 之类的操作,但我不确定如何在我的情况下使用这些操作,即a or (b and c)。谢谢!

【问题讨论】:

    标签: node.js validation npm backend joi


    【解决方案1】:

    您需要使用 Joi 的替代方案和条件功能,您可以在 documentation 中了解这些内容

    const schema = {
        a: Joi.string(),
        b: Joi.when('a', { is: !Joi.exist(), then: Joi.string().required() }),
        c: Joi.when('a', { is: !Joi.exist(), then: Joi.string().required() }),
    };
    

    【讨论】:

      猜你喜欢
      • 2018-02-27
      • 1970-01-01
      • 2016-02-19
      • 2022-11-20
      • 1970-01-01
      • 2016-08-12
      • 2020-02-21
      • 2014-02-13
      • 2019-04-14
      相关资源
      最近更新 更多