Fix #604: Send custom objects to middleware from security validators #752
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I was having the same issue where I wanted to send a custom error response format when encountering a 401 or 403 when validating tokens.
The solution that I went with was to create and export a CustomError class which people can instantiate and throw in their security handler callback functions. The handler simply needs to create an instance of the CustomError with a payload that will be passed to the next express middleware method.
I'll also need to update the documentation for the security validator usage to showcase how a custom object can be returned.
https://github.com/cdimascio/express-openapi-validator/wiki/Documentation#security-handlers
See the test for an example of it's usage.