-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathvalidate_results.py
More file actions
77 lines (66 loc) · 2.63 KB
/
Copy pathvalidate_results.py
File metadata and controls
77 lines (66 loc) · 2.63 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Script to validate JSON result files against a defined schema.
This script iterates through all .json files in the specified RESULTS_DIR
and validates their structure against the schema defined in SCHEMA_FILE
using the jsonschema library. It prints the validation status for each file.
"""
import json
import os
from jsonschema import validate, ValidationError
# --- Constants ---
RESULTS_DIR = "results" # Directory containing result files to validate
SCHEMA_FILE = "output_schema.json" # Path to the JSON schema file
# --- Helper Function ---
def load_json(filepath):
"""Loads JSON data from a file."""
try:
with open(filepath, 'r', encoding='utf-8') as f:
return json.load(f)
except json.JSONDecodeError as e:
print(f"Error decoding JSON from {filepath}: {e}")
return None
except FileNotFoundError:
print(f"Error: File not found {filepath}")
return None
except Exception as e:
print(f"An unexpected error occurred loading {filepath}: {e}")
return None
def validate_results():
"""Validates all JSON files in the results directory against the schema."""
schema = load_json(SCHEMA_FILE)
if schema is None:
print(f"Could not load schema file {SCHEMA_FILE}. Exiting.")
return
if not os.path.isdir(RESULTS_DIR):
print(f"Error: Results directory '{RESULTS_DIR}' not found. Exiting.")
return
print(f"Validating files in '{RESULTS_DIR}' against '{SCHEMA_FILE}'...")
print("-" * 30)
found_files = False
invalid_files = 0
for filename in os.listdir(RESULTS_DIR):
if filename.endswith(".json"):
found_files = True
filepath = os.path.join(RESULTS_DIR, filename)
data = load_json(filepath)
if data is not None:
try:
validate(instance=data, schema=schema)
print(f"✅ {filename}: VALID")
except ValidationError as e:
invalid_files += 1
print(f"❌ {filename}: INVALID")
print(f" Error: {e.message} (Path: {'/'.join(map(str, e.path))})")
except Exception as e:
invalid_files += 1
print(f"❌ {filename}: ERROR during validation")
print(f" Unexpected error: {e}")
print("-" * 30)
if not found_files:
print(f"No JSON files found in '{RESULTS_DIR}'.")
elif invalid_files == 0:
print("All found JSON files are valid.")
else:
print(f"{invalid_files} file(s) failed validation.")
if __name__ == "__main__":
validate_results()