Joi: How to use default values with xor?

Viewed 225

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!

0 Answers
Related