您还在怎么校验请求体,请求参数,复杂嵌套字典?使用if…else…,for…?,oh No,小心leader给你加“鸡腿”,今天给大家分享一种博主使用很久的参数校验方式。具有无限嵌套一次校验,配置简单可读性高,兼容函数等诸多优点
场景:校验以下格式的示例参数
my_data = {
"header_data": {
"user_name": {
"first_name": "请关注下面这个公众号", # 必填 全中文,少于20字
"second_name": "Hello Python", # 选填,或只能填写字符串 少于20字
"msg": ["都是干货", 5, 6, 7] # 必填,列表可为空,或只能填写字符串或整数
},
"age_binning": {
"hospitalizations": ["aa", "bb", "cc", "dd"],
"population": [5, 6, 7, 8]
}
},
"day_data": {
"2020-03-16": { # 可变key,必填,校验其格式
"weather": "rain", # 选填
"temperature": {
"average": 1.638889,
"min": -2.604167,
"max": 78,
}
},
"2020-03-17": {
"weather": "snow",
"temperature": {
"average": 1.638889,
"min": -2.604167,
"max": 8,
}
},
}
}
使用schema 校验如下:具体语法可参考schema官方说明。
from schema import Regex, Or, Schema, And, Optional
def is_all_zh_digit(s):
"""
校验全是汉字
"""
if not s:
return False
for c in s:
if not (('\u4e00' <= c <= '\u9fa5') or c.isdigit()):
return False
return True
sample = Schema({
"header_data": {
"user_name": {
"first_name": And(str, lambda n: len(n) < 20 and is_all_zh_digit(n),
error='first_name is needed or Invalid'),
Optional('second_name'): And(str, lambda n: len(n) < 20, error='second_name Invalid'),
"msg": [Or(str, int, None, lambda n: len(n) <= 100, error='msg Invalid')]
},
"age_binning": {
"hospitalizations": [str],
"population": [int]
}
},
"day_data": {
Regex('^[0-9]{4}-[0-9]{2}-[0-9]{2}'): {
Optional('weather'): str,
"temperature": {
"average": float,
"min": float,
"max": int,
}
},
}
})
try:
sample.validate(my_data)
print('true')
except Exception as e:
print(e)
当在接口中校验请求json数据时,Django类视图示例:
在基类LoonBaseView中进行校验:
import simplejson
import json
from django.views import View
from services.pub_demos.demo_common import api_response
class LoonBaseView(View):
"""
base view for params validate
"""
def dispatch(self, request, *args, **kwargs):
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
request_method = request.method.lower()
meth_schema = getattr(self, request.method.lower() + '_schema', None)
if meth_schema and request_method in ['post', 'patch', 'put']:
try:
json_dict = simplejson.loads(request.body)
print(json_dict)
meth_schema.validate(json_dict)
except Exception as e:
print(e.__str__())
return api_response(-1, '请求参数不合法:{}'.format(e.__str__()), {})
return handler(request, *args, **kwargs)
是不是很方便,赶快体验吧,更多干货,请关注公众号:Hello Python