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

Conversation

devinivy
Copy link
Member

Following from the conversation in #4040, I've added the default validation error to err.data.defaultError where err is the detailed error passed to failAction() for input validation. Since the common use-case seems to be logging detailed validation errors without modifying the error response, I considered logging the detailed error by default. But I determined that it contained user input which could be sensitive: I believe users should opt-in to this behavior with a custom failAction() rather than it become the default 'log' behavior. Here's how that would look with this update:

server.route({
    method: 'get',
    path: '/',
    handler: () => 'ok',
    options: {
        validate: {
            query: {
                a: Joi.string().min(2)
            },
            failAction: function (request, h, err) {
                request.log(['validation', 'error', err.payload.validation.source], err);
                throw err.data.defaultError;
            }
        }
    }
});

Resolves #4040

@devinivy devinivy added the feature New functionality or improvement label Apr 26, 2022
@devinivy devinivy added this to the 21.0.0 milestone Apr 26, 2022
Copy link
Member

@Nargonath Nargonath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, good addition @devinivy . Thank you.

API.md Show resolved Hide resolved
@devinivy devinivy self-assigned this May 1, 2022
@devinivy devinivy merged commit 619380a into v21 May 1, 2022
@devinivy devinivy deleted the validation-fail-action-error branch May 1, 2022 04:59
@devinivy devinivy mentioned this pull request Nov 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants