Validate one of two fields to be present or none to be present based on value of another key
- Dominant language
- JavaScript
- Stars
- 21.2k
- Forks
- 1.5k
- Avg merge
- 4h 57m
- Merged PRs (30d)
- 14
Description
I have to validation based on some conditons:
If value of the key `pay_method` is `upi`, then ***one of the keys*** `services`
or `combos` must be present.
If value of the key `pay_method` is `pay_later`, then ***none of those keys*** should be present.
I tried combining `.when` and `.xor` also `.when` and `.oxor`.
But, I am not able to achieve the desired result.
Joi.object({
pay_method: Joi.string().valid('upi', 'later').required(),
combos: Joi.string().when('pay_method', {
is: 'later',
then: Joi.forbidden(),
}),
services: Joi.string().when('pay_method', {
is: 'later',
then: Joi.forbidden(),
}),
}).oxor('combos', 'services')
.messages({ 'object.oxor': 'Both Combo and Service can not be ordered together!' })
Data to be validated:
{
pay_method: 'later',
combos: 'asdfasf',
}
According to the documentation:
`xor` : key names that must not appear together but where one of them is required
`oxor` : key names that must not appear together but where none are required
Please test here with above details [https://joi.dev/tester/][1]
[1]: https://joi.dev/tester/
Contributor guide
Assessment
This issue has not been assessed yet.