Auto-checking request body against Swagger JSON schema
See original GitHub issueSince 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!
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Validating JSON against Swagger API schema - Stack Overflow
A Java library for validating request/responses against a OpenAPI / Swagger specification. Includes support for Swagger v2 and OpenAPI v3 ...
Read more >Describing Request Body - Swagger
OpenAPI 3.0 provides the requestBody keyword to describe request bodies. ... such as JSON, XML, form data, plain text, and others, and use...
Read more >Testing and validating API responses with JSON Schema
In this post, I'll walk-through an example of using Assertible to create a test that validates an API response against a JSON Schema....
Read more >Solved: Request validation against swagger specification
Solved: I have a requirement to validate requests against a swagger definition (preferably json file) . Let us know if there is something...
Read more >validating request and response against swagger - Forums - IBM
This will help us validate the request in entry point itself instead of moving further flow. We can ignore the request if failed...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is actually done by the Parse Request middleware, which is a dependency of the Validate Request middlware. Here’s the relevant documentation: Phase 2 - Swagger parsing
If
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.