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

342
Vistas
Joi: How to use default values with xor?

I need to do a validation somewhat like xor, but Joi treats null values as truthy. The only thing that is falsey in their xor is undefined / missing keys, so something like this won't work:

const schema = Joi.object({
  a: Joi.string().empty('').default(null),
  b: Joi.string().empty('').default(null),
}).xor('a', 'b')

The reason I need this is I need to grab the returned value and I need all keys to be present, and set to null rather than leaving them undefined / missing.

// this doesn't validate since 'b' defaults to null before the .xor() check
const { error, value } = schema.validate({ a: 'test' });

I'm having a hard time coming up with a way to do this with Joi. I can easily add the key back with some custom handler or something, but if there is a way to do this with Joi, that would be much cleaner!

about 4 years ago · Juan Pablo Isaza
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