-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.py
134 lines (105 loc) · 4.13 KB
/
validator.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import json
class Validator:
solution_from_file: dict
def __init__(self, path):
self._path = path
def validate(self):
self.validate_json_is_it()
self.validate_json_is_empty()
self.validate_count_objects()
self.validate_json_structure()
self.validate_limit_keys()
self.validate_diagnosis_length()
self.validate_diagnosis_exists()
self.validate_incorrect_key_values()
pass
def validate_json_is_it(self):
try:
with open(self._path) as f:
solution_raw_content = f.read()
self.solution_from_file = json.loads(solution_raw_content)
except:
raise NotJsonContentInFileError()
def validate_json_is_empty(self):
if len(self.solution_from_file) < 1:
raise JsonIsEmpty()
pass
def validate_count_objects(self):
if len(self.solution_from_file) > 100:
raise TooManyObjectsInTheArrayError()
pass
def validate_json_structure(self):
av_keys = ['decorCode', 'code']
for val in self.solution_from_file:
list = val.keys()
for key in list:
if key not in av_keys:
raise StructureJsonIsIncorrect()
# status = "start" in val and "end" in val and "decorCode" in val and "code" in val and "name" in val \
# and "xPath" in val
# if not status:
# raise StructureJsonIsIncorrect()
pass
def validate_limit_keys(self):
count_desease = 0
count_sup = 0
count_main = 0
for val in self.solution_from_file:
if "decorCode" in val:
if val['decorCode'] == 'attendDisease':
count_desease += 1
if val['decorCode'] == 'diagnosisSup':
count_sup += 1
if val['decorCode'] == 'diagnosisMain':
count_main += 1
if count_sup > 10 or count_desease > 10 or count_main > 1:
raise LimitKeysInJson()
pass
def validate_incorrect_key_values(self):
av_keys = ['attendDisease', 'diagnosisSup', 'diagnosisMain']
for val in self.solution_from_file:
if "decorCode" in val:
if val['decorCode'] not in av_keys:
raise IncorrectKeyValues()
pass
def validate_diagnosis_exists(self):
count_main = 0
for val in self.solution_from_file:
if "decorCode" in val:
if val['decorCode'] == 'diagnosisMain':
count_main += 1
if count_main == 0:
raise ThereIsNoMainDiagnosis()
pass
def validate_diagnosis_length(self):
diagnosis_length = 10
for val in self.solution_from_file:
if "decorCode" in val:
if val['decorCode'] == 'diagnosisMain':
if len(val['code']) > diagnosis_length:
raise DiagnosisMainLength()
pass
class JsonIsEmpty(Exception):
def __str__(self):
return f'Answer JSON is empty.'
class StructureJsonIsIncorrect(Exception):
def __str__(self):
return f'The structure of the JSON sent in the response does not match the TR.'
class LimitKeysInJson(Exception):
def __str__(self):
return f'The number of decorCode key in the attendDisease or diagnosisSup or diagnosisMain values has been exceeded'
class IncorrectKeyValues(Exception):
def __str__(self):
return f'The decorCode field uses invalid key values'
class DiagnosisMainLength(Exception):
def __str__(self):
return f'The length of the main diagnosis line exceeded 10 characters'
class NotJsonContentInFileError(Exception):
def __str__(self):
return f'The file at the specified path does not contain json.'
class ThereIsNoMainDiagnosis(Exception):
def __str__(self):
return f'The main diagnosis does not exist in the sent markup.'
class TooManyObjectsInTheArrayError(Exception):
def __str__(self):
return f'The maximum length of an array of objects with a response is 100 elements.'