使用jsonschema进行参数校验
1.安装jsonschema
**pip install jsonschema**
2.代码
点击查看代码
from jsonschema import validate
def verify_code_2_member(data: dict):
schema = {
"type": "object",
"additionalProperties": True,
"required": ["code", "account_id", "telephone"],
"properties": {
"code": {
"type": "string",
"description": "兑换码",
"minLength": 9,
"maxLength": 9,
},
"account_id": {
"type": "integer",
"description": "账号"
},
"telephone": {
"type": "string",
"description": "手机号",
"pattern": "^1[3-9]\d{9}$"
}
}
}
return __validator(data, schema)
def __validator(data, schema):
try:
validate(data, schema)
return True, None
except Exception as e:
return False, str(e).split("\n")[0]
3.使用
_res, _err = verify_code_2_member(body)