joi
joi copied to clipboard
custom validator not called on object if nested keys have validation errors
Support plan
- is this issue currently blocking your project? yes:
- is this issue affecting a production system? no:
Context
- node version: 16.x
- module version with issue: 17.6.0
- last module version without issue:
- environment (e.g. node, browser, native): node
- used with (e.g. hapi application, another framework, standalone, ...): standalone
- any other relevant information:
What are you trying to achieve or the steps to reproduce?
const schema = Joi.object({
username: Joi.string().alphanum().min(3).max(5).required(),
password: Joi.string().required(),
}).custom((value, helpers) => {
if (value.password !== 'abc') {
return helpers.error('custom.error.password');
}
return value;
}).messages({
'custom.error.password': 'My Custom Message',
});
const result = schema.validate({
username: '123456',
password: 'abcd',
}, { abortEarly: false });
When one of the object keys fails validation, custom validator is never called.
The example is simplified on purpose, custom validator contains validation logic combining checks for several nested keys, so it's not something that could be moved to keys instead.