41 lines
1.5 KiB
Python
41 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Validate all listings against the frozen v1 schemas.
|
|
|
|
Used by CI and locally: python3 scripts/validate.py
|
|
Exits non-zero on any violation. jsonschema required (pip install jsonschema).
|
|
"""
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from jsonschema import Draft202012Validator
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
SOLUTION = Draft202012Validator(json.loads((ROOT / "schemas/solution.schema.json").read_text()))
|
|
LISTING = Draft202012Validator(json.loads((ROOT / "schemas/listing.schema.json").read_text()))
|
|
|
|
def main() -> int:
|
|
failures = []
|
|
for d in sorted((ROOT / "listings").iterdir()):
|
|
if not d.is_dir():
|
|
continue
|
|
for name, validator in (("listing.json", LISTING), ("solution.json", SOLUTION)):
|
|
f = d / name
|
|
if not f.exists():
|
|
failures.append(f"{d.name}: missing {name}")
|
|
continue
|
|
errors = sorted(validator.iter_errors(json.loads(f.read_text())), key=str)
|
|
failures += [f"{d.name}/{name}: {e.json_path}: {e.message}" for e in errors]
|
|
if (d / "listing.json").exists():
|
|
listing = json.loads((d / "listing.json").read_text())
|
|
if listing.get("listing_id") != d.name:
|
|
failures.append(f"{d.name}: directory name != listing_id")
|
|
if failures:
|
|
print("VALIDATION FAILED:")
|
|
print("\n".join(f" - {f}" for f in failures))
|
|
return 1
|
|
print("all listings valid")
|
|
return 0
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|