-
Notifications
You must be signed in to change notification settings - Fork 9
/
json_validator.py
executable file
·49 lines (35 loc) · 1.2 KB
/
json_validator.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
#!/usr/bin/env python3
# Copyright 2022, Red Hat, Inc.
# SPDX-License-Identifier: LGPL-2.1-or-later
import argparse
import json
import sys
from jsonschema import validate
def parse_args():
parser = argparse.ArgumentParser(prog='JSON Schema validator')
parser.add_argument("-s",
"--schema",
type=str,
default="./tests/json_schema_of_report.json",
help="Path to schema of JSON to validate."
)
parser.add_argument('JSON',
type=argparse.FileType("r"),
nargs='?',
default=sys.stdin,
help="JSON file source. Default: stdin"
)
return parser.parse_args()
def validate_json(schema_src, json_file):
json_schema = None
json_data = None
with open(schema_src, "r", encoding="utf-8") as schema_file:
json_schema = json.load(schema_file)
json_data = json.load(json_file)
json_file.close()
validate(json_data, json_schema)
def main():
args = parse_args()
validate_json(args.schema, args.JSON)
if __name__ == "__main__":
main()