使用Python的OpenApi 3请求验证 [英] OpenApi 3 request validation with Python
问题描述
似乎有许多专用于验证OpenApi模式正确性的python库.虽然这可能有用,但坦白地说,我可以在 https://editor.swagger.io 并对其进行验证/转换/格式化.
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.
然后我剩下的客户可能会向我的OpenApi记录的端点发送任何类型的脏数据.
I'm then left with clients potentially sending any kind of dirty data to my OpenApi documented endpoint.
因此,我找不到的是提供给定OpenApi模式和JSON(通常来自HTTP请求)的库,该库根据模式验证了输入/数据JSON.所有必填字段都到位了吗?我希望age
是一个数字,所以不要通过验证,因为它是作为字符串传递的,依此类推.
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.
我能找到的最接近的东西是 https://flask-restplus.readthedocs.io/zh/stable/但
The closest thing I could find is https://flask-restplus.readthedocs.io/en/stable/ but
- 它与Flask链接,不能在不同的环境中使用
- 它不接受OpenApi模式,但是产生了一个,我发现它是很不完整的
- 其GitHub存储库中有许多未解决/尚未解决的未解决问题
是否有任何Python库可以再次验证输入数据和OpenApi模式?
Is there any python library that validates input data agains an OpenApi schema?
推荐答案
OpenAPI 3 与 JSON Schema 的大部分内容兼容-
OpenAPI 3 is compatible with the most part of JSON Schema - read The Docs
您可以为Python安装 json模式包装器并验证JSON.
You can install json schema wrapper for Python and validate your JSON.
这篇关于使用Python的OpenApi 3请求验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!