-
Notifications
You must be signed in to change notification settings - Fork 36
/
schemavalidator.py
executable file
·167 lines (149 loc) · 5.58 KB
/
schemavalidator.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/python
from jsonschema import Draft7Validator
from jsonschema.exceptions import ValidationError, SchemaError
import json
import sys
def printPath(pathObj, fields):
path = ''
for item in pathObj:
if isinstance(item, int):
path += u'[{}]'.format(item)
else:
path += u'/'
path += item
path += '/[{}]'.format(fields)
return path
def validate(data, version, url):
if version == '3.0':
with open('schema/iiif_3_0.json') as json_file:
try:
schema = json.load(json_file)
except ValueError as err:
print ('Failed to load JSON due to: {}'.format(err))
raise
try:
validator = Draft7Validator(schema)
results = validator.iter_errors(json.loads(data))
except SchemaError as err:
print('Problem with the supplied schema:\n')
print(err)
raise
okay = 0
errors = sorted(results, key=lambda e: e.path)
error = ''
errorsJson = []
if errors:
print('Validation Failed')
errorCount = 1
if len(errors) == 1 and 'is not valid under any of the given schemas' in errors[0].message:
errors = errors[0].context
for err in errors:
if 'is not valid under any of the given schemas' in err.message:
subErrorMessages = []
for subErr in err.context:
if 'is not valid under any of the given schemas' not in subErr.message:
subErrorMessages.append(subErr.message)
errorsJson.append({
'title': 'Error {} of {}.\n Message: Failed to process submission due to many errors'.format(errorCount, len(errors)),
'detail': 'This error is likely due to other listed errors. Fix those errors first.',
'description': "{}".format(subErrorMessages),
'path': '',
'context': ''
})
else:
detail = ''
if 'title' in err.schema:
detail = err.schema['title']
description = ''
if 'description' in err.schema:
detail += ' ' + err.schema['description']
context = err.instance
#print (json.dumps(err.instance, indent=4))
if isinstance(context, dict):
for key in context:
if isinstance(context[key], list):
context[key] = '[ ... ]'
elif isinstance(context[key], dict):
context[key] = '{ ... }'
errorsJson.append({
'title': 'Error {} of {}.\n Message: {}'.format(errorCount, len(errors), err.message),
'detail': detail,
'description': description,
'path': printPath(err.path, err.message),
'context': context
})
#print (json.dumps(err.instance, indent=4))
errorCount += 1
# Return:
# infojson = {
# 'received': data,
# 'okay': okay,
# 'warnings': warnings,
# 'error': str(err),
# 'url': url
# }
okay = 0
else:
print ('Passed Validation!')
okay = 1
return {
'received': data,
'okay': okay,
'warnings': [],
'error': error,
'errorList': errorsJson,
'url': url
}
if __name__ == '__main__':
if len(sys.argv) != 3:
print ('Usage:\n\t{} manifest schema'.format(sys.argv[0]))
exit(-1)
with open(sys.argv[1]) as json_file:
print ('Loading: {}'.format(sys.argv[1]))
try:
iiif_json = json.load(json_file)
except ValueError as err:
print ('Failed to load JSON due to: {}'.format(err))
exit(-1)
with open(sys.argv[2]) as schema_file:
print ('Loading schema: {}\n'.format(sys.argv[2]))
try:
schema = json.load(schema_file)
except ValueError as err:
print ('Failed to load schema due to JSON error: {}'.format(err))
exit(-1)
try:
validator = Draft7Validator(schema)
results = validator.iter_errors(iiif_json)
except SchemaError as err:
print('Problem with the supplied schema:\n')
print(err)
errors = sorted(results, key=lambda e: e.path)
if errors:
print('Validation Failed')
errorCount = 1
for err in errors:
print('Error {} of {}.\n Message: {}'.format(errorCount, len(errors), err.message))
if 'title' in err.schema:
print (' Test message: {}'.format(err.schema['title']))
if 'description' in err.schema:
print (' Test description: {}'.format(err.schema['description']))
print('\n Path for error: {}'.format(printPath(err.path, err.message)))
context = err.instance
for key in context:
if isinstance(context[key], list):
context[key] = '[ ... ]'
elif isinstance(context[key], dict):
context[key] = '{ ... }'
print (json.dumps(err.instance, indent=4))
errorCount += 1
# Return:
# infojson = {
# 'received': data,
# 'okay': okay,
# 'warnings': warnings,
# 'error': str(err),
# 'url': url
# }
else:
print ('Passed Validation!')