Auto-checking request body against Swagger JSON schemaSee original GitHub issue
Since the Swagger spec supports JSON schemas for
req.body, I expected the Validate Request middleware to use this to test incoming request bodies, and emit a (customizable) HTTP 4xx code on error. But that seems not to be the case?
If I am mistaken, please let me know how to enable this functionality. Otherwise, consider this a feature request.
In any case, thanks for these fantastic tools!
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
req.body — or any other parameter in the request — is invalid, then an error will be thrown. The HTTP error code varies, depending on the cause of the error. Possible errors include 400, 411, and 500. But, as with any error thrown by Swagger Express Middleware, you can catch the error in your own error-handling middleware and change the error code to whatever you want.
@swissspidy - Yes, the request body is validated against the schema in your Swagger API. So, in your example, if the body doesn’t contain a
name property, then an HTTP 400 error will be thrown. As @mhelvens pointed out, I need to document this better.
Here’s an example that demonstrates the functionality.