Joi嵌套时

Joi nested when

const schema = {
    a: Joi.any(),
    b: Joi
      .boolean()
      .default(false),
    c: Joi
      .boolean()
      .default(false)
}

如何修复上述 Joi 架构以匹配以下规则

Joi.object( {
  a: Joi
      .boolean()
      .default(false),
  b: Joi
      .boolean()
      .default(false),
  c: Joi.string().when(
    'a', {
      is: false,
      then: Joi.when(
        'b', {
          is: false,
          then: Joi.string().required()
        }
      )
    }
  ),
})

我在提问前试过嵌套when,但由于规则的顺序,它之前不起作用

Joi.object( {
  c: Joi.string().when(
    'a', {
      is: false,
      then: Joi.when(
        'b', {
          is: false,
          then: Joi.string().required()
        }
      )
    }
  ),
  a: Joi
      .boolean()
      .default(false),
  b: Joi
      .boolean()
      .default(false),
})

第二个模式中的default值在when检查时没有设置,所以如果你验证一个空对象{},第一个模式有Validation Error: "c" is required 错误,而第二个 Passed{a: false, b: false}

我在这里开了个issue https://github.com/sideway/joi/issues/2683