question-mark

Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Auto-checking request body against Swagger JSON schema

See original GitHub issue

Issue Description

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!

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JamesMessingercommented, Sep 29, 2015

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.

0reactions
JamesMessingercommented, Apr 4, 2016

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found