一个简单的json验证器,用于flask项目。(内置python 3)
json-simple-validator的Python项目详细描述
json简单验证器
关于
这是一个简单的json验证器,为flask应用程序构建并测试。
使用量
安装验证器:
pip install json-simple-validator
验证程序的示例用法:
fromjson_simple_validatorimportfield_validation## Example Flask request@app.route("/post",methods=["POST"])defpost():validation_list=[{"field":"name"},{"field":"pass","alias":"Password"},{"field":"number","var_type":[int]}]# validation_list = [# # Each element is made up of the field that is being validated# # and the alias of the field that is to be displayed, should there be an error# # Sample dict:# # {"name": "", e.g "first_name"# # "alias[optional]": "", eg "First Name" If left out, title case of field name is used# # "var_type[optional]": "", eg [str, bool, int]# # "length[optional]": "[min, max(optional)]", eg [2, 10]# # "special_rules": ""} eg ["email"] Only email is supported for nowmessages=fieldValidation(request.json,validation_list)ifmessages:returnjsonify({"messages":messages}),422
将以下内容发布到上述端点:
{"name":"","pass":"","number":"121231"}
返回以下内容:
{"messages":["Name is empty.","Password is empty.","Number data type is not of the expected data type."]}