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

336
Visualizações
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 à 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