-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcomplex_check.py
More file actions
executable file
·67 lines (55 loc) · 1.58 KB
/
Copy pathcomplex_check.py
File metadata and controls
executable file
·67 lines (55 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'MFC'
__time__ = '18/9/14 15:23'
from cerberus import Validator
def check_kwargs(kwargs):
"""
检测必要的字段是否满足创建要求
"""
schema = {
"name": {"type": "string", "required": True},
"nick_name": {"type": "string"},
"class_info": { # 注意这里也是要标注rule的
"type": "dict", "schema": {
"class_type": {"type": "integer", "required": True},
"class_fee": {"type": "integer"}
}
},
}
v = Validator()
v.allow_unknown = True # default False
flag = v(kwargs, schema),
ret = {
"flag": flag,
"mag": "订单信息填写不符合要求" if not flag else "success",
"data": v.errors,
}
return ret
if __name__ == '__main__':
data_01 = {"name": "tester1",
"class_info": {
"class_type": 0
}
}
data_02 = {"name": "tester2",
"nick_name": "cute_cat",
"class_info": {
"class_type": 1,
"class_fee": 300
}
}
data_03 = {"name": "tester3",
"class_info": {
"class_type": 0
},
"vip": True # can add unknown
}
# error test_case
data_04 = {"name": "tester4",
"class_info": {
"class_type": ''
},
} # error
print(data_04)
print(check_kwargs(data_04))