-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathschemacheck.py
46 lines (37 loc) · 1.37 KB
/
schemacheck.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
#!/usr/bin/env python3
import sys
import argparse
import yaml
import json
import jsonschema
def main():
""" Main function"""
parser = argparse.ArgumentParser(description='Schema checker')
parser.add_argument('schema',
nargs='+',
metavar='filename')
parser.add_argument('--input',
metavar='filename')
args = parser.parse_args()
for filename in args.schema:
with open(filename) as file:
print("Checking schema", filename, file=sys.stderr)
if filename.endswith('.json'):
schema = json.load(file)
elif filename.endswith('.yaml'):
schema = yaml.load(file, Loader=yaml.SafeLoader)
else:
raise Exception("Unknown schema format")
jsonschema.Draft202012Validator.check_schema(schema)
if args.input:
with open(args.input) as file:
print("Checking input", args.input, file=sys.stderr)
if args.input.endswith('.json'):
data = json.load(file)
elif args.input.endswith('.yaml'):
data = yaml.load(file, Loader=yaml.SafeLoader)
else:
raise Exception("Unknown input format")
jsonschema.validate(data, schema)
if __name__ == "__main__":
main()