问题
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.
So, what I can't find is a library that given an OpenApi schema and a JSON (typically from an HTTP request) validates the input / data JSON against the schema. Are all the required fields in place? I expect age
to be a number, so don't pass the validation because it's been passed as string and so on.
The closest thing I could find is https://flask-restplus.readthedocs.io/en/stable/ but
- It's linked to Flask and cannot be used in different environments
- It doesn't accepts an OpenApi schema, but produces one and I've found it to be quite incomplete
- Its GitHub repository has tons of open issues that are not being worked on / acknowledged
Is there any python library that validates input data agains an OpenApi schema?
回答1:
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.
回答2:
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.
回答3:
See Falcon Heavy. It can be integrated into your Falcon, Flask or Django project or you can use factories from it to generate validators for your data.
来源:https://stackoverflow.com/questions/54748710/openapi-3-request-validation-with-python