如何根据 Python 中的 OpenAPI3 规范验证 HTTP 请求? [英] How to validate an HTTP requests against an OpenAPI3 specification in Python?

查看:72
本文介绍了如何根据 Python 中的 OpenAPI3 规范验证 HTTP 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有许多 Python 库专门用于验证 OpenApi 模式的正确性.虽然这可能有用,但坦率地说,我可以在 https://editor.swagger.io 中编写我的 OpenApi 架构a> 并对其进行验证/转换/格式化.

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 架构验证数据的问题通常是通过扩展数据验证工具来解决的,这些插件可以从数据验证声明中推断出 OpenAPI 规范.然而,反过来通常是不可能的,即从 OpenAPI 模式开始并实例化数据验证器.此外,从验证声明生成的 OpenAPI 往往不完整.

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.

如何根据 Python 中的 OpenAPI3 规范验证 HTTP 请求数据(最好以通用且与框架无关的方式)?

推荐答案

OpenAPI 3 兼容大部分 JSON Schema - 阅读文档

您可以为 Python 安装 json schema 包装器并验证您的 JSON.

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

这篇关于如何根据 Python 中的 OpenAPI3 规范验证 HTTP 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆