forked from abusix/xarf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.py
44 lines (33 loc) · 1.28 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
from __future__ import print_function
import os
import json
import jsonschema
import argparse
import traceback
import sys
from jsonschema.exceptions import ValidationError, SchemaError
parser = argparse.ArgumentParser(description='Validate document against jsonschema')
parser.add_argument('schema', help="path to the schema file")
parser.add_argument('documents', nargs='+', help="paths of documents to validate")
args = parser.parse_args()
schema = args.schema
documents = args.documents
absolute_path_to_base_directory = os.path.abspath(os.path.dirname(schema))
with open(schema, 'r') as schema_file:
loaded_schema = json.load(schema_file)
# Note that the second parameter does nothing.
resolver = jsonschema.RefResolver('file://' + absolute_path_to_base_directory + '/', None)
for document in documents:
with open(document, 'r') as document_file:
loaded_document = json.load(document_file)
try:
jsonschema.validate(loaded_document, loaded_schema, resolver=resolver)
print('Validation of %s successful!' % document)
except ValidationError as e:
print('Validation of %s failed!' % document)
traceback.print_exc()
sys.exit(1)
except SchemaError as e:
print('Invalid schema!')
traceback.print_exc()
sys.exit(2)