-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.py
More file actions
64 lines (48 loc) · 1.68 KB
/
Copy pathvalidate.py
File metadata and controls
64 lines (48 loc) · 1.68 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
#!/usr/bin/env python3
"""Validate example JSON files against the operation semantics schema.
Validates against the preferred x-intentops-semantics schema by default.
Use --legacy to validate against the original x-intent schema.
"""
import json
import sys
from pathlib import Path
try:
import jsonschema
except ImportError:
print("Missing dependency: pip install jsonschema")
sys.exit(1)
def main():
repo_root = Path(__file__).parent
use_legacy = "--legacy" in sys.argv
if use_legacy:
schema_path = repo_root / "schemas" / "x-intent" / "v1" / "schema.json"
print("Using legacy x-intent schema\n")
else:
schema_path = repo_root / "schemas" / "x-intentops-semantics" / "v1" / "schema.json"
print("Using x-intentops-semantics schema\n")
examples_dir = repo_root / "examples"
with open(schema_path) as f:
schema = json.load(f)
validator = jsonschema.Draft202012Validator(schema)
examples = sorted(examples_dir.glob("*.json"))
if not examples:
print("No examples found")
sys.exit(1)
failed = []
for example_path in examples:
with open(example_path) as f:
example = json.load(f)
errors = list(validator.iter_errors(example))
if errors:
failed.append((example_path.name, errors))
print(f"FAIL {example_path.name}")
for err in errors:
print(f" - {err.message}")
else:
print(f"OK {example_path.name}")
if failed:
print(f"\n{len(failed)} file(s) failed validation")
sys.exit(1)
print(f"\nAll {len(examples)} examples valid")
if __name__ == "__main__":
main()