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

236
Visualizações
Joi - how to check when other key's value is null/empty

I am having trouble trying to validate a JSON file, to be more specific I am having trouble with a when condition.

I have a a key that has to be a past date, and isn't required. Then I have a b key that should be an url and if a is null/empty, b can be empty, otherwise must be required.

I'll leave my code below to make it clear, but right now I'm not getting any error if b is null but a isn't.

a: Joi.date().less("now").raw().optional().allow(null, ""),
b: Joi.string()
  .uri({ scheme: [/https?/] })
  .when("a", {
    is: Joi.any().empty(),
    then: Joi.optional().allow(null, ""),
    otherwise: Joi.required(),
  }),
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Found the solution, my is condition was wrong.

Instead of using is: Joi.any().empty() I put is: Joi.any().valid(null, "") and it worked.

So the solution is this:

a: Joi.date().less("now").raw().optional().allow(null, ""),
b: Joi.string()
  .uri({ scheme: [/https?/] })
  .when("a", {
    is: Joi.any().valid(null, ""),
    then: Joi.optional().allow(null, ""),
    otherwise: Joi.required(),
  }),
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