How to validate an HTTP requests against an OpenAPI3 specification in Python?
Asked Answered
J

3

15

There seems to be number of Python libraries dedicated to validate the correctness of an OpenAPI schema. While this might be useful, I can frankly just write my OpenAPI schema here it in https://editor.swagger.io and have it validated / converted / pretty formatted.

I'm then left with clients potentially sending any kind of dirty data to my OpenAPI documented endpoint.

Currently, the problem of validating data against the OpenAPI schema is commonly addressed by extending data validation tools with plugins that can infer the OpenAPI spec from their data validation declaration. However, the reverse is not typically possible, that is to start from the OpenAPI schema and instantiate data validators. Also, the OpenAPI generated from validation declarations tends to be incomplete.

How can I validate HTTP request data against an OpenAPI3 spec in Python (preferably in a generic and framework-agnostic way)?

Jackknife answered 18/2, 2019 at 13:46 Comment(0)
W
4

OpenAPI 3 is compatible with the most part of JSON Schema - read The Docs

You can install json schema wrapper for Python and validate your JSON.

Welsh answered 18/2, 2019 at 14:16 Comment(0)
I
1

As for Q1 2019 there is no such tool. The good list of existing tools is https://openapi.tools/#data-validators

As a workaround one could use one of the existing tools and call binary created with such tool from python.

PS. There is https://github.com/p1c2u/openapi-core but it seems not to support full OpenApi 3.

Ines answered 30/4, 2019 at 12:33 Comment(1)
openapi-core is a correct answer.Margit
M
0

You can use connexion to validate requests and responses. The initial server implementation can be generated from the OpenAPI specification.

You can also use OpenAPI-core.

Monda answered 4/5, 2024 at 19:33 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.