Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make default error available in validation failAction #4350

Merged
merged 1 commit into from
May 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -3432,7 +3432,8 @@ Default value: `'error'` (return a Bad Request (400) error response).

A [`failAction` value](#lifecycle-failAction) which determines how to handle failed validations.
When set to a function, the `err` argument includes the type of validation error under
`err.output.payload.validation.source`.
`err.output.payload.validation.source`. The default error that would otherwise have been logged
or returned can be accessed under `err.data.defaultError`.

#### <a name="route.options.validate.headers" /> `route.options.validate.headers`

Expand Down
2 changes: 1 addition & 1 deletion lib/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ internals.input = async function (source, request) {
// Prepare error

const defaultError = validationError.isBoom ? validationError : Boom.badRequest(`Invalid request ${source} input`);
const detailedError = Boom.boomify(validationError, { statusCode: 400, override: false });
const detailedError = Boom.boomify(validationError, { statusCode: 400, override: false, data: { defaultError } });
detailedError.output.payload.validation = { source, keys: [] };
if (validationError.details) {
for (const details of validationError.details) {
Expand Down
30 changes: 30 additions & 0 deletions test/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,36 @@ describe('validation', () => {
expect(res.result).to.equal('Got error in query where a is bad');
});

it('makes default error available in failAction', async () => {

const server = Hapi.server();
server.validator(Joi);
server.route({
method: 'GET',
path: '/',
handler: () => 'ok',
options: {
validate: {
query: {
a: Joi.string().min(2)
},
failAction: function (request, h, err) {

err.data.defaultError.output.payload.message += ': ' + err.output.payload.validation.keys.join(', ');

throw err.data.defaultError;
}
}
}
});

const res = await server.inject('/?a=1');
expect(res.statusCode).to.equal(400);
expect(res.result).to.contain({
message: 'Invalid request query input: a'
});
});

it('catches error thrown in failAction', async () => {

const server = Hapi.server({ debug: false });
Expand Down