Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

341
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!