Files
llm/scripts/validate_1c_bsl_modules.py
T

60 lines
2.2 KiB
Python

from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
DEFAULT_PATH = ROOT / "plugins" / "1c" / "metadata" / "examples" / "bsl-modules.example.json"
def validate(data: object, path: Path) -> list[str]:
errors: list[str] = []
if not isinstance(data, dict):
return [f"{path}: snapshot must be a JSON object"]
if data.get("schema_version") != 1:
errors.append(f"{path}: schema_version must be 1")
if not isinstance(data.get("source"), dict) or not data["source"].get("name"):
errors.append(f"{path}: source.name is required")
if not data.get("created_at"):
errors.append(f"{path}: created_at is required")
modules = data.get("modules")
if not isinstance(modules, list):
errors.append(f"{path}: modules must be a list")
return errors
for index, module in enumerate(modules):
prefix = f"{path}: modules[{index}]"
if not isinstance(module, dict):
errors.append(f"{prefix}: module must be an object")
continue
for field in ("module_id", "object_name", "module_type", "content"):
if not module.get(field):
errors.append(f"{prefix}.{field} is required")
for routine_kind in ("procedures", "functions"):
for routine_index, routine in enumerate(module.get(routine_kind) or []):
if not isinstance(routine, dict) or not routine.get("name"):
errors.append(f"{prefix}.{routine_kind}[{routine_index}].name is required")
return errors
def main() -> int:
parser = argparse.ArgumentParser(description="Validate a 1C BSL module snapshot.")
parser.add_argument("path", nargs="?", type=Path, default=DEFAULT_PATH)
args = parser.parse_args()
with args.path.open("r", encoding="utf-8") as handle:
data = json.load(handle)
errors = validate(data, args.path)
if errors:
print("1C BSL module snapshot validation failed:", file=sys.stderr)
for error in errors:
print(f"- {error}", file=sys.stderr)
return 1
print(f"Validated 1C BSL module snapshot: {args.path}")
return 0
if __name__ == "__main__":
raise SystemExit(main())