Joi validation multiple conditions Joi validation multiple conditions javascript javascript

Joi validation multiple conditions


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() }))};


The answer by Gergo Erdosi didn't work with Joi 14.3.0, this resulted in an OR condition:

a === 'avalue' || b === 'bvalue'

The following worked for me:

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

This results in a === 'avalue' && b === 'bvalue'