Validate request arguments, Including Cross Field, Cross Struct
Project description
欢迎您使用pre-request框架,pre-request致力于简化请求参数验证工作。为Flask的 网络请求参数验证提供了解决方案。
pre-request提供了非常方便的使用的方法,也提供了灵活的扩展接口方便您实现自定义的 业务逻辑。
特点
提供绝大部分常用的参数基础验证能力,也提供callback函数让用户自定义验证
提供跨字段的、跨数据结构的参数验证能力,实现参数之间关联验证
提供响应对象、格式化函数、过滤器等核心组件的自定义能力
详细完善的测试用例,保证整体测试覆盖率高于90%
丰富的example,演示了pre-request目前提供的所有能力
安装
pip install pre-request
快速使用
集成pre-request到您的请求中非常简单
from flask import Flask
from pre_request import pre
from pre_request import Rule
app = Flask(__name__)
args = {
"userId": Rule(type=int, required=True)
}
@app.route("/")
@pre.catch(args)
def hello_world(params):
from flask import g
return params == g.params
上面的代码中发生了什么呢?
首先我们从 pre-request 库中引入全局 pre 对象,使用该对象来过滤用户参数
我们定义了一个请求参数 userId 并规定该参数的目标类型为 int ,并且不允许为空
使用 @pre.catch(req_params) 将参数规则赋值给装饰器,并装饰处理函数
格式化后的参数置于 ~flask.g 中,同时尝试将格式化后的参数置于原函数的 params 参数中。
Links
Documentaion: https://pre-request.readthedocs.io/en/master/index.html
Issue tracker: https://github.com/Eastwu5788/pre-request/issues
Test status: https://coveralls.io/github/Eastwu5788/pre-request
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.