Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

234
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda