Adds schema and validation script, for checking otuput

This commit is contained in:
Alicia Sykes
2023-04-16 13:06:59 +01:00
parent be908c4e5a
commit 179d0a5090
2 changed files with 147 additions and 0 deletions

35
lib/validate.py Normal file
View File

@@ -0,0 +1,35 @@
import json
import os
import sys
from jsonschema import validate, ValidationError
def load_json_file(file_path):
with open(file_path, 'r') as file:
return json.load(file)
def main():
try:
script_dir = os.path.dirname(os.path.abspath(__file__))
schema_file = os.path.join(script_dir, '..', 'Schema.json')
templates_file = os.path.join(script_dir, '..', 'templates.json')
schema = load_json_file(schema_file)
templates = load_json_file(templates_file)
validate(instance=templates, schema=schema)
print('✅ templates.json is valid against the schema')
except ValidationError as ve:
print('Validation error:', ve.message)
sys.exit(1)
except FileNotFoundError as fnfe:
print(f'File not found error: {fnfe}')
sys.exit(1)
except json.JSONDecodeError as jde:
print(f'JSON decoding error: {jde}')
sys.exit(1)
if __name__ == '__main__':
main()