-
Notifications
You must be signed in to change notification settings - Fork 17
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
feat: Implement middleware for schema validations #306
Conversation
Pull Request Test Coverage Report for Build 1337379287
💛 - Coveralls |
This is great. We can now remove all the validations that were handled directly by the request handler function and there was a lot repeated |
const valid = validate(req.body) | ||
|
||
if (!valid) { | ||
next( |
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.
Should't you just throw the error as it was done inside the handlers?
Example from items router
if (validate.errors) {
throw new HTTPError('Invalid schema', validate.errors)
}
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.
You can do both! Express has a try-catch surrounding handlers that will execute next(error)
on error, but it is also possible to use next
with the error in your middleware and it will go straight to the error handle middleware.
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.
LGTM! question is just out of curiosity
This PR implements a middleware to do request body validations.
Each request is validated through a request schema and, in case of errors, the response is formatted as it was done before, with the exception of the status code that now is a 400 (bad request).