-
Notifications
You must be signed in to change notification settings - Fork 0
/
example1.py
113 lines (98 loc) · 4.01 KB
/
example1.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
from datetime import datetime
from pprint import pprint
from payload_validator.exceptions import (
InvalidValueError,
ValidationException,
)
from payload_validator.validators import PayloadValidator
def validate_date_parsing(date_str):
try:
datetime.strptime(date_str, '%Y-%m-%d')
return True
except (TypeError, ValueError):
return False
# 1
class NewPayloadValidator(PayloadValidator):
# 2
DEFAULT_MANDATORY_ERROR_MESSAGE = 'mandatory data missing'
class Meta:
# 3-1, 3-2
mandatory_keys = {
'displayable': 'displayable is required',
'mode': 'mode is always required',
'amount': 'why are you not setting amount?',
'minimum_order_value': 'minimum order value is required',
'applicable_order_types': 'really you are not setting applicable order types?',
'start_date': 'start date is required',
'end_date': 'end date is required for your job',
}
# 3-1, 3-3
type_of_keys = {
'amount': [int, 'integer_type_needs'],
'minimum_order_value': [int, 'integer_type_needs'],
'maximum_download_count': [(int, type(None)), 'integer_type_needs or NoneType'],
# 3-4
'start_date': [validate_date_parsing, 'need to be date type'],
'end_date': [validate_date_parsing, 'need to be date type'],
}
# 4-1, 4-2
def validate_hello_world(self):
if not self.get_payload('displayable'):
# 4-3, 4-4
raise InvalidValueError({'displayable': 'displayable is false'})
# 4-1, 4-2
def validate_max_length(self):
if self.get_payload('max_length') <= 0:
# 4-3, 4-4, 4-5, Extra
self.add_error_and_skip_validation_key(
'max_length',
[
'validate_max_length: This max_length should be inside error context1',
'validate_max_length: This max_length should be inside error context2',
],
)
def validate_min_length(self):
if self.get_payload('min_length') <= 0:
# 4-3, 4-4
raise InvalidValueError(
{
'min_length': 'validate_min_length: This min_length should be inside error context',
},
)
# 5
def common_validate(self):
if self.get_payload('max_length') < self.get_payload('min_length'):
raise InvalidValueError(
{
'max_length': 'This Should be not exists in error context'
'because of at `validate_max_length` function'
'add_error_and_skip_validation_key method has max_length error context',
'min_length': 'This Should be not exists in error context'
'because of validate_min_length method has min_length error context'
'and this InvalidValueError has ignore_existing_error_keys of mix_length',
},
ignore_existing_error_keys={'min_length'},
)
if True:
raise InvalidValueError(
{
'max_length': 'This Should be not exists in error context'
'because of upper logic has man_length error context'
'and this InvalidValueError has ignore_existing_error_keys of max_length',
'min_length': 'common_validate: This min_length should be inside error context',
},
ignore_existing_error_keys={'max_length'},
)
validator = NewPayloadValidator(
{'displayable': True, 'start_date': 1, 'min_length': 0, 'max_length': 0}
)
try:
# 7
validator.validate()
except ValidationException as e:
print('[ validate method ]')
pprint(validator.error_context)
# 8
if not validator.is_valid():
print('[ is_valid method ]')
pprint(validator.error_context)