Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
Joi nested when
const schema = {
    a: Joi.any(),
    b: Joi
      .boolean()
      .default(false),
    c: Joi
      .boolean()
      .default(false)
}

How can I fix the abovementioned Joi schema to matches the following rules

  • One of b or c can be true at the same time
  • When b or c is true, a is forbidden otherwise a is required
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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()
        }
      )
    }
  ),
})

I tried nested when before asking question, but it not working before because of the order of rules

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),
})

the default value in the second schema is not set when the when is checking, So If you validate an empty object {}, the first schema has Validation Error: "c" is required error while the second Passed with {a: false, b: false}

I opened an issue here https://github.com/sideway/joi/issues/2683

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda