Repository navigation
Expand file tree
/
Copy pathvalidate.py
More file actions
116 lines (90 loc) · 3.52 KB
/
Copy pathvalidate.py
File metadata and controls
116 lines (90 loc) · 3.52 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
"""
Structural validator for traust-contracts artifacts.
Usage:
python validate.py --schema <name> FILE...
python validate.py --list
Exits 0 if all artifacts valid, 1 if any fail.
"""
import json
import sys
from pathlib import Path
from jsonschema import Draft202012Validator
from referencing import Registry, Resource
from referencing.exceptions import Unresolvable
SCHEMA_DIR = Path(__file__).parent / "schemas" / "v1"
def build_registry() -> Registry:
"""Build $ref registry across all schemas for cross-file references."""
resources = {}
for sf in sorted(SCHEMA_DIR.glob("*.schema.json")):
doc = json.loads(sf.read_text(encoding="utf-8"))
resource = Resource.from_contents(doc)
# Register by both $id and filename for compatibility
if "$id" in doc:
resources[doc["$id"]] = resource
resources[sf.name] = resource
return Registry(resources=resources)
def validate_artifact(schema_name: str, artifact_path: Path) -> bool:
"""Validate artifact against a schema basename and optional JSON Pointer fragment."""
basename, separator, fragment = schema_name.partition("#")
schema_file = SCHEMA_DIR / f"{basename}.schema.json"
if not schema_file.exists():
print(f"{artifact_path}: schema not found: {basename}.schema.json")
return False
try:
schema = json.loads(schema_file.read_text(encoding="utf-8"))
artifact = json.loads(artifact_path.read_text(encoding="utf-8"))
except json.JSONDecodeError as e:
print(f"{artifact_path}: JSON parse error: {e}")
return False
registry = build_registry()
if separator:
reference = schema["$id"] + "#" + fragment
try:
registry.resolver().lookup(reference)
except Unresolvable as error:
print(f"{artifact_path}: invalid schema selector {schema_name!r}: {error}")
return False
schema = {"$schema": schema["$schema"], "$ref": reference}
validator = Draft202012Validator(schema, registry=registry)
valid = True
for error in validator.iter_errors(artifact):
valid = False
path_str = (
".".join(str(p) for p in error.absolute_path) if error.absolute_path else "<root>"
)
print(f"{artifact_path}: {path_str}: {error.message}")
return valid
def list_schemas():
"""Print available schemas."""
schemas = sorted(sf.stem for sf in SCHEMA_DIR.glob("*.schema.json"))
print(f"\nAvailable schemas ({len(schemas)}):\n")
for schema in schemas:
print(f" {schema}")
def main():
if len(sys.argv) < 2:
print("Usage: python validate.py --schema <name> FILE...")
print(" python validate.py --list")
sys.exit(1)
if sys.argv[1] == "--list":
list_schemas()
return 0
if sys.argv[1] == "--schema":
if len(sys.argv) < 4:
print("Usage: python validate.py --schema <name> FILE...")
sys.exit(1)
schema_name = sys.argv[2]
artifact_paths = [Path(p) for p in sys.argv[3:]]
all_valid = True
for artifact_path in artifact_paths:
if not artifact_path.exists():
print(f"{artifact_path}: file not found")
all_valid = False
elif not validate_artifact(schema_name, artifact_path):
all_valid = False
sys.exit(0 if all_valid else 1)
else:
print(f"Unknown option: {sys.argv[1]}")
sys.exit(1)
if __name__ == "__main__":
sys.exit(main())