I’m trying to implement the following validation rule using Joi.
Basically I have 2 properties a and b, I’d like to check that a is less than or equal to b only if b is not undefined.
Here is what I’ve tried so far:
const mySchema = Joi.object().keys({
a: Joi.number().concat(Joi.number().when('b', { is: Joi.not(undefined), then: Joi.number().max(Joi.ref('b')).message('a should be less than or equal to b') })
b: Joi.number() })
I get the following error message when testing the schema:
Cannot call allow/valid/invalid with undefined