57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Validate AppBlueprint JSON against the local JSON Schema (Draft 2020-12)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
try:
|
|
import jsonschema
|
|
from jsonschema import Draft202012Validator
|
|
except ImportError:
|
|
print("请先安装: pip install jsonschema", file=sys.stderr)
|
|
sys.exit(2)
|
|
|
|
|
|
def main() -> int:
|
|
root = Path(__file__).resolve().parents[1]
|
|
parser = argparse.ArgumentParser(description="Validate AppBlueprint")
|
|
parser.add_argument(
|
|
"blueprint",
|
|
nargs="?",
|
|
default=str(root / "examples" / "inventory-ledger.blueprint.json"),
|
|
help="blueprint json path",
|
|
)
|
|
parser.add_argument(
|
|
"--schema",
|
|
default=str(root / "schema" / "app-blueprint.schema.json"),
|
|
help="json schema path",
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
schema = json.loads(Path(args.schema).read_text(encoding="utf-8"))
|
|
data = json.loads(Path(args.blueprint).read_text(encoding="utf-8"))
|
|
|
|
validator = Draft202012Validator(schema)
|
|
errors = sorted(validator.iter_errors(data), key=lambda e: list(e.path))
|
|
if errors:
|
|
print(f"FAIL: {len(errors)} error(s)")
|
|
for err in errors:
|
|
path = ".".join(str(p) for p in err.path) or "$"
|
|
print(f" - {path}: {err.message}")
|
|
return 1
|
|
|
|
print("OK: blueprint is valid")
|
|
print(f" app: {data['meta']['name']} ({data['meta']['slug']})")
|
|
print(f" entities: {len(data['entities'])}")
|
|
print(f" pages: {len(data['pages'])}")
|
|
print(f" apis: {len(data['apis']['resources'])}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|