Passed
Pull Request — main (#130)
by Jan
07:01
created

json_validator.main()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python3
2
3
# Copyright 2022, Red Hat, Inc.
4
# SPDX-License-Identifier: LGPL-2.1-or-later
5
6
import argparse
7
import json
8
import sys
9
10
from jsonschema import validate
11
12
13
def parse_args():
14
    parser = argparse.ArgumentParser(prog='JSON Schema validator')
15
    parser.add_argument("-s",
16
                        "--schema",
17
                        type=str,
18
                        default="./tests/json_schema_of_report.json",
19
                        help="Path to schema of JSON to validate."
20
                        )
21
    parser.add_argument('JSON',
22
                        type=argparse.FileType("r"),
23
                        nargs='?',
24
                        default=sys.stdin,
25
                        help="JSON file source. Default: stdin"
26
                        )
27
    return parser.parse_args()
28
29
30
def validate_json(schema_src, json_file):
31
    json_schema = None
32
    json_data = None
33
34
    with open(schema_src, "r", encoding="utf-8") as schema_file:
35
        json_schema = json.load(schema_file)
36
37
    json_data = json.load(json_file)
38
    json_file.close()
39
40
    validate(json_data, json_schema)
41
42
43
def main():
44
    args = parse_args()
45
    validate_json(args.schema, args.JSON)
46
47
48
if __name__ == "__main__":
49
    main()
50