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

Accept response validation error handler #456

Merged
merged 4 commits into from Nov 17, 2020

Conversation

nampas
Copy link
Contributor

@nampas nampas commented Nov 11, 2020

Here's a stab at the feature outlined here: #454

This PR adds a new validateResponses.onError option. There's no strong contract with the function, but express-openapi-validator will pass the err object, as well as the response data. Usage might look something like this

const handler = (err, data) => {
   console.log('OpenAPI response validation error', err);
};

OpenApiValidator.middleware({
   apiSpec: './spec/openapi.json',
   validateRequests: true,
   validateResponses: { onError: handler }
});

apiSpec: apiSpecPath,
validateResponses: {
onError: function() {
onErrorArgs = Array.from(arguments);
Copy link
Owner

Choose a reason for hiding this comment

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

can you also add a test, where you re-throw the error and/or create a new error. looks like should all work out, but having a test will be good.

@cdimascio
Copy link
Owner

thanks for this @nampas

@cdimascio
Copy link
Owner

one more note, please update the validateResponses section of the README to describe this new option

Copy link
Owner

@cdimascio cdimascio left a comment

Choose a reason for hiding this comment

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

@nampas thanks for the feature

@cdimascio cdimascio merged commit 0cc8c4d into cdimascio:master Nov 17, 2020
ex1st pushed a commit to ex1st/express-openapi-validator that referenced this pull request Dec 9, 2020
* Accept response validation error handler

* Fewer args to onError

* Another test

* Additional test and readme updates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants