在烧瓶中请求json验证
在Flask中验证json请求是否有一些最佳实践? 在Flask restful扩展中有一种有趣的方法,但我的应用程序中不需要它。 我只想要这样的东西:在烧瓶中请求json验证,json,validation,flask,Json,Validation,Flask,在Flask中验证json请求是否有一些最佳实践? 在Flask restful扩展中有一种有趣的方法,但我的应用程序中不需要它。 我只想要这样的东西: user_schema = { 'username': email, 'password': required, 'age': required } @app.route('new_user/', methods=['POST']) def new_user(): validate_json(request.
user_schema = {
'username': email,
'password': required,
'age': required
}
@app.route('new_user/', methods=['POST'])
def new_user():
validate_json(request.json, user_schema)
看看
用法示例:
>>> from cerberus import Validator
>>> schema = {'name': {'type': 'string', 'required': True}}
>>> v = Validator(schema)
>>> document = {'bla': 'john doe'}
>>> v.validate(document)
False
>>>
可能值得一看或类似。你想验证什么?非常适合序列化和验证。我正在尝试验证来自客户端的JSON。我需要验证这个JSON中的字段。您应该使用@PatrickAllen提到的棉花糖。我在一个项目中使用了webargs(),它是在marschmallow的基础上构建的,或者更确切地说是使用它的验证器。如果您正在构建某种RESTAPI,您可以尝试一下。非常感谢:@PatrickAllen和minato。我会看看他们两个。