'Joi conditional validation on two dates
I would like to validate against two dates in an object. The rules should be:
amust include one valid date, eitherborcmust be present and valid- If
borcisnullit should not validate that value...if the other value is valid.
It should validate these:
{
a: {
b: '2021-11-01T00:00:00.000Z'
c: null
}
}
{
a: {
b: '2021-11-01T00:00:00.000Z',
c: '2021-11-01T00:00:00.000Z'
}
}
{
a: {
b: null,
c: '2021-11-01T00:00:00.000Z'
}
}
It should NOT validate these:
{
a: {
b: null
c: null
}
}
I was thinking of using an or but it seems when the value is null it doesn't check. The reason for the null is that the calendar component I'm using sets it to null if the user clears the date picker.
{
a: Joi.object({
b: Joi.date(),
c: Joi.date(),
}).or('b', 'c'),
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
