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)?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
pistacchio
  • 56,889
  • 107
  • 278
  • 420

2 Answers2

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.

arturkuchynski
  • 790
  • 1
  • 9
  • 27
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.

Matt Kucia
  • 1,026
  • 12
  • 17