-
Notifications
You must be signed in to change notification settings - Fork 21
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
Fixed bug in Response validator #40
base: master
Are you sure you want to change the base?
Fixed bug in Response validator #40
Conversation
Response validator replaces res.json with a custom function. In case the user configured passError option, we need to restore the original res.json function so that the error handling middleware can update the response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good
Unfortunately this is not good enough. Any ideas how this can be resolved? |
… when an error occures before the response is sent
Added another commit that fixes the issue, in a hackish way. I don't really like this, but did not find another solution. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this PR! Can you take a look at the comments?
} | ||
} | ||
|
||
module.exports.fixupResponse = restoreResponseAfterResponseValidation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be best not to expose this. Make the fix-up automatic in all scenarios, including response
, if passError
is enabled. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see your last comment. Right, this is a tricky...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is indeed Hacky. Do you have any other suggestion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had this idea stewing in my head around providing schemas for certain status code ranges. It's a bit more complex though. Something like:
validation.response({
// Schemas to run for given status codes
'2xx': schemaSuccess,
'4xx': schemaFail
}, {
// These could be defined globally instead of as part of
// the call to response()
passError: true
passErrorSchemas: {
'5xx': errrorHandlerSchema
}
})
If no schema is provided for 5xx, then perhaps validation is bypassed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a very good interface.
It seems to me that it will still need the mechanism of overriding the json
function on the Response
object and require restoring the original json
function
}) | ||
const res = getRequester(middleware, errorHandler) | ||
.get('/error') | ||
.expect(422) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a 500 error, yet the test is passing. That's odd. Checking res.statusCode
in the end
block instead will do the trick.
I'll need to look into why supertest is not asserting it correctly since it seems to be the same for other tests...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The errorHandler sets the status code to 422, so I think this works as expected
Response validator replaces res.json with a custom function.
In case the user configured passError option, we need to restore the
original res.json function so that the error handling middleware can
update the response.