atlas/tools/ci/validate_json.py

43 lines
1.1 KiB
Python
Raw Normal View History

2022-04-05 13:02:43 +02:00
#!/usr/bin/python
import sys
2022-04-05 12:53:53 +02:00
import json
2023-06-16 12:13:08 +02:00
from jsonschema import validate, RefResolver
from pathlib import Path, PurePosixPath
import os
2022-04-05 12:53:53 +02:00
if len(sys.argv) == 1:
while not os.path.exists('README.md'):
os.chdir('..')
instance_path = "web/atlas.json"
2022-04-05 13:02:43 +02:00
# path override as 1st param: validate_json.py path_to_file.json
if len(sys.argv) > 1:
2023-06-16 12:13:08 +02:00
instance_path = sys.argv[1]
2022-04-05 13:02:43 +02:00
schema_path = "tools/schema/atlas.json"
2023-04-25 08:19:52 +02:00
# schema override as 2nd param: validate_json.py [...] path_to_schema.json
if len(sys.argv) > 2:
2023-06-16 12:13:08 +02:00
schema_path = sys.argv[2]
2023-04-25 08:19:52 +02:00
2023-06-16 12:13:08 +02:00
relative_path = "file:" + str(PurePosixPath(Path(os.getcwd(), schema_path)))
2023-04-25 08:19:52 +02:00
2023-06-16 12:13:08 +02:00
schema = json.load(open(schema_path, "r", encoding='utf-8'))
# exit()
resolver = RefResolver(relative_path, schema)
if os.path.isdir(instance_path):
for filename in os.listdir(instance_path):
f = os.path.join(instance_path, filename)
print(f)
instance = json.load(open(f, "r", encoding='utf-8'))
validate(instance, schema, resolver=resolver)
elif os.path.isfile(instance_path):
print(instance_path)
instance = json.load(open(instance_path, "r", encoding='utf-8'))
validate(instance, schema, resolver=resolver)
2022-04-05 12:53:53 +02:00
print("JSON is valid")