-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.py
More file actions
72 lines (55 loc) · 2.03 KB
/
validate.py
File metadata and controls
72 lines (55 loc) · 2.03 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
#!/usr/bin/env python3
"""Validate MIF documents against the MIF v2.0 JSON Schema."""
import json
import sys
from pathlib import Path
try:
import jsonschema
except ImportError:
print("Install jsonschema: pip install jsonschema")
sys.exit(1)
SCHEMA_PATH = Path(__file__).parent / "schema" / "mif-v2.schema.json"
def validate_file(filepath: str) -> bool:
schema_path = SCHEMA_PATH
if not schema_path.exists():
print(f"Schema not found: {schema_path}")
return False
with open(schema_path) as f:
schema = json.load(f)
with open(filepath) as f:
try:
document = json.load(f)
except json.JSONDecodeError as e:
print(f"FAIL {filepath}: Invalid JSON — {e}")
return False
validator = jsonschema.Draft202012Validator(schema)
errors = list(validator.iter_errors(document))
if not errors:
memory_count = len(document.get("memories", []))
has_graph = document.get("knowledge_graph") is not None
has_extensions = bool(document.get("vendor_extensions"))
print(f"PASS {filepath}")
print(f" {memory_count} memories, graph={'yes' if has_graph else 'no'}, extensions={'yes' if has_extensions else 'no'}")
return True
print(f"FAIL {filepath}: {len(errors)} validation error(s)")
for err in errors[:5]:
path = " -> ".join(str(p) for p in err.absolute_path) or "(root)"
print(f" [{path}] {err.message}")
if len(errors) > 5:
print(f" ... and {len(errors) - 5} more")
return False
def main():
if len(sys.argv) < 2:
print("Usage: python validate.py <file.mif.json> [file2.mif.json ...]")
print(" python validate.py examples/*.mif.json")
sys.exit(1)
files = sys.argv[1:]
results = []
for f in files:
results.append(validate_file(f))
passed = sum(results)
total = len(results)
print(f"\n{passed}/{total} files passed validation")
sys.exit(0 if all(results) else 1)
if __name__ == "__main__":
main()