-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvalidate.py
44 lines (36 loc) · 1.1 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
#!/usr/bin/env python3
import json
import logging
import re
import sys
import click
import jsonschema
import utils
@click.command()
@click.argument("schema", type=click.File("r"), required=True)
@click.argument("jsonfiles", type=click.Path(exists=True), required=True)
def validate(schema, jsonfiles):
"""Validate a JSON files against a JSON schema.
\b
SCHEMA: JSON schema to validate against. Required.
JSONFILE: JSON files to validate. Required.
"""
schema = json.loads(schema.read())
success = True
for path in utils.get_files(jsonfiles):
with open(path) as f:
try:
jsonfile = json.loads(f.read())
except ValueError:
logging.error("Error loading json file " + path)
raise Exception("Invalid json file")
try:
jsonschema.validate(jsonfile, schema)
except Exception as e:
success = False
logging.error("Error validating file " + path)
logging.error(str(e))
if not success:
sys.exit(-1)
if __name__ == "__main__":
validate()