Skip to content

Commit

Permalink
Add JSON validator tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Honny1 committed Feb 22, 2023
1 parent d76b734 commit 974fdea
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions json_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()

0 comments on commit 974fdea

Please sign in to comment.