Skip to content

Commit

Permalink
Fix error() function. Closes #2147
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Sep 29, 2019
1 parent 4753311 commit 5d6d5ed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/validator.js
Expand Up @@ -461,6 +461,10 @@ internals.finalize = function (value, errors, helpers) {
if (!Array.isArray(errors)) {
errors = [errors];
}

for (const error of errors) {
Assert(error instanceof Error || error instanceof Errors.Report, 'error() must return an Error object');
}
}
else {
errors = [schema._flags.error];
Expand Down
9 changes: 7 additions & 2 deletions test/base.js
Expand Up @@ -1372,7 +1372,6 @@ describe('any', () => {
}
});
}).to.throw('Must provide a valid Error object or a function');

});

it('errors on missing error option', () => {
Expand All @@ -1386,7 +1385,6 @@ describe('any', () => {
}
});
}).to.throw('Missing error');

});

describe('with a function', () => {
Expand Down Expand Up @@ -1540,6 +1538,13 @@ describe('any', () => {
expect(result2.value).to.equal({ a: [' xx', 'yy?'], b: ' x' });
expect(result2.error).to.be.an.error('my new error message');
});

it('errors on invalid error function', () => {

const schema = Joi.number().error(() => 'not an Error');

expect(() => schema.validate('x')).to.throw('error() must return an Error object');
});
});
});

Expand Down

0 comments on commit 5d6d5ed

Please sign in to comment.