Skip to content

Make default error available in validation failAction#4350

Merged
devinivy merged 1 commit intov21from
validation-fail-action-error
May 1, 2022
Merged

Make default error available in validation failAction#4350
devinivy merged 1 commit intov21from
validation-fail-action-error

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.

@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.

2 participants

Comments