-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
113 lines (84 loc) · 2.97 KB
/
example.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import bval
from fieldval import FieldVal, FVCheck
#Missing and unrecognized
validating = dict(unrecognized_field='123')
validator = FieldVal(validating)
validator.get('missing_field', bval.String())
print validator.end()
validating = dict(unrecognized_field='123')
validator = FieldVal(validating)
validator.get('missing_field', bval.String(required=False))
print validator.end()
#Incorrect type
validating = dict(test='123')
validator = FieldVal(validating)
validator.get('test', bval.Integer())
print validator.end()
#String validation
validating = dict(test='123')
validator = FieldVal(validating)
validator.get('test', bval.String())
print validator.end()
#Integer validation
validating = dict(test=123)
validator = FieldVal(validating)
validator.get('test', bval.Integer())
print validator.end()
#Integer validation with parsing
validating = dict(test='123')
validator = FieldVal(validating)
validator.get('test', bval.Integer(parse=True))
print validator.end()
#Bool validation
validating = dict(test='True')
validator = FieldVal(validating)
validator.get('test', bval.Boolean(parse=True))
print validator.end()
#List validation
validating = dict(test=[1, 2, 3])
validator = FieldVal(validating)
validator.get('test', bval.List())
print validator.end()
#Dict validation
validating = dict(test=dict(key=123))
validator = FieldVal(validating)
validator.get('test', bval.Dict())
print validator.end()
#Custom check
class SortedNumbers(FVCheck):
def check(self, numbers):
ascending = self.args.get('ascending', True)
for i in range(1, len(numbers)):
if ascending:
if numbers[i] < numbers[i-1]:
return dict(error=1234, error_message='Numbers are not in the right order!')
else:
if numbers[i] > numbers[i-1]:
return dict(error=1234, error_message='Numbers are not in the right order!')
class ListOfIntegers(FVCheck):
def check(self, value):
numbers_str = value.split(' ')
numbers = []
for number_str in numbers_str:
try:
number = int(number_str)
except:
return dict(error=1234, error_message='Numbers are invalid!')
numbers.append(number)
self.assign_new_value(numbers)
validating = dict(test='1 2 3 4 5 6 7 8 9 10')
validator = FieldVal(validating)
field = validator.get('test', bval.String(), ListOfIntegers(), SortedNumbers(ascending=True))
print validator.end()
validating = dict(test='5 4 3 2 1')
validator = FieldVal(validating)
validator.get('test', bval.String(), ListOfIntegers(), SortedNumbers(ascending=False))
print validator.end()
validating = dict(test='1 3 2')
validator = FieldVal(validating)
validator.get('test', bval.String(), ListOfIntegers(), SortedNumbers(ascending=True))
print validator.end()
validating = dict(test=[1, 2, 3])
validator = FieldVal(validating)
validator.get('test', bval.String(), ListOfIntegers(), SortedNumbers(ascending=True))
print validator.end()