Joi validation multiple conditions

user2468170 picture user2468170 · Oct 22, 2014 · Viewed 22.6k times · Source

I have the following schema:

var testSchema = Joi.object().keys({
    a: Joi.string(), 
    b: Joi.string(), 
    c: Joi.string().when('a', {'is': 'avalue', then: Joi.string().required()})
});

but I would like to add a condition on c field definition so that it is required when:

a == 'avalue' AND b=='bvalue'

How can I do that?

Answer

Gergo Erdosi picture Gergo Erdosi · Oct 22, 2014

You can concatenate two when rules:

var schema = {
    a: Joi.string(),
    b: Joi.string(),
    c: Joi.string().when('a', { is: 'avalue', then: Joi.string().required() }).concat(Joi.string().when('b', { is: 'bvalue', then: Joi.string().required() }))
};