-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.py
47 lines (32 loc) · 1.21 KB
/
validate.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
import os
import json
import jsonschema
schemas_dict = { }
type_hierarchy_dict = None
# reads in all json schemas from the 'schemas' directory
# puts them in a dict, indexed by title
for filename in [each for each in os.listdir('schemas') if each.endswith('.json')]:
path = os.path.join('schemas', filename)
print "reading schema:", path
with open(path) as json_data:
tmp = json.load(json_data)
if (tmp['properties'].has_key('type')):
schemas_dict[tmp['title']] = tmp
schema_types = schemas_dict.keys()
def validate(data):
try:
uri = 'file://'+os.path.join(os.getcwd(), 'schemas')
print "uri:", uri
r = jsonschema.RefResolver(uri, None)
v = jsonschema.Draft4Validator(schemas_dict[data['type']], resolver=r)
v.validate(data)
except jsonschema.exceptions.ValidationError as e:
msgs = [e.message]
errors = sorted(v.iter_errors(data), key=lambda e: e.path)
for error in errors:
for suberror in sorted(error.context, key=lambda e: e.schema_path):
msgs.append(suberror.message)
raise TypeError(msgs)
return data
if __name__ == "__main__":
validate({'type': 'project'})