I am trying to create a custom validation rule (using the JOI
validations library in a node.js
application) that dependent of other fields of the form.
For example: I need the username
field to be required if both of the email
or mobile
fields are empty or visa versa.
I have already tried the when
and or
methods, provided by the library itself and searched on the google as well. But, still standing on same place.
When example:
const validations = joi.object({
...
username: joi.any().when('email', {
is: '' || null || undefined,
then: joi.required()
}),
email: joi.any().optional(),
mobile: joi.any().optional(),
...
}).options({
abortEarly: false
});
Or Example:
const validations = joi.object({
...
username: joi.any(),
email: joi.any(),
mobile: joi.any(),
...
}).or('username', 'email', 'mobile').options({
abortEarly: false
});
Please help, this issue has already cost me too much time.