'How to validate a Joi object based on a field type
I have the following Joi object:
const Content = Joi
.object({
type: Joi.valid('contentType1', 'contentType2')
value: 'validate with that certain type'
})
Is there a way to validate the field value based on the value of the field type
Solution 1:[1]
Got it by doing this:
const Content = Joi
.object({
type: Joi.valid('contentType1', 'contentType2')
value: Joi
.when('type', {
is: 'contentType1',
then: Joi.valid('Yey')
})
.when('type', {
is: 'contentType2',
then: Joi.valid('Yow')
})
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Von Christian R. Delos Santos |
