Skip to content

Commit

Permalink
Handle multiple validation reports in error(). Closes #1940
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jun 28, 2019
1 parent fda066c commit e54c505
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 165 deletions.
3 changes: 2 additions & 1 deletion API.md
Expand Up @@ -838,7 +838,8 @@ schema.validate(''); // returns { error: "value" is not allowed to be empty, val
Overrides the default **joi** error with a custom error if the rule fails where:
- `err` can be:
- an instance of `Error` - the override error.
- a function with the signature `function(errors)`, where `errors` is an array of errors and it returns a single `Error`.
- a function with the signature `function(errors)`, where `errors` is an array of validation
reports and it returns either a single `Error` or an array of validation reports.

Note that if you provide an `Error`, it will be returned as-is, unmodified and undecorated with any
of the normal error properties. If validation fails and another error is found before the error
Expand Down
10 changes: 9 additions & 1 deletion lib/validator.js
Expand Up @@ -249,7 +249,15 @@ internals.finalize = function (value, schema, original, errors, state, prefs) {
if (errors.length &&
schema._flags.error) {

errors = [typeof schema._flags.error === 'function' ? schema._flags.error(errors) : schema._flags.error];
if (typeof schema._flags.error === 'function') {
errors = schema._flags.error(errors);
if (!Array.isArray(errors)) {
errors = [errors];
}
}
else {
errors = [schema._flags.error];
}
}

// Default
Expand Down

0 comments on commit e54c505

Please sign in to comment.