hapijs / hapijs/joi

custom validator not called on object if nested keys have validation errors

Open
#2,834 0 comments 0 reactions 0 assignees View on GitHub
support
Dominant language
JavaScript
Stars
21.2k
Forks
1.5k
Avg merge
4h 57m
Merged PRs (30d)
14

Description

#### 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?

```js
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.