I am trying to validate field in an object based on a set of values of another field. Assuming my object has to fields, field1
and field2
. Field one can take values A, B, C, D, E, F, H, I. Now say if field1 is any of the values; A, B, C, H, then field2 should be null. How do I do this.
I think it should have the synthax below but I don't know how to implement it completely.
const mySchema = Joi.object({
field1: Joi.string().valid('A','B','C','D','E','F','H','I').required(),
field2: Joi.when('field1', is: <A or B or C or H> then: <field2 should be null> otherwise: Joi.date().required())
});
NB: I am using "@hapi/joi": "^17.1.1"