#!/usr/bin/env python3 """Валидация справочного слоя data/reference/*.json по схеме ReferenceExtraction. python scripts/validate_reference.py [файл] Exit 1 при ошибке — для CI и самопроверки агентов. """ import json import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) # корень в путь from pydantic import ValidationError # noqa: E402 from rag import config # noqa: E402 from rag.extract.reference import ReferenceExtraction # noqa: E402 REFERENCE_DIR = config.DATA / "reference" def validate_file(path: Path) -> bool: try: data = json.loads(path.read_text(encoding="utf-8")) core = {k: v for k, v in data.items() if not k.startswith("_")} r = ReferenceExtraction.model_validate(core) except (json.JSONDecodeError, ValidationError) as e: print(f"✗ {path.name}: {e}") return False by_type = {} for c in r.cards: by_type[c.type] = by_type.get(c.type, 0) + 1 print(f"✓ {path.name}: {len(r.cards)} справок " f"({', '.join(f'{t}: {n}' for t, n in sorted(by_type.items()))})") return True def main() -> int: files = ([Path(sys.argv[1])] if len(sys.argv) > 1 else sorted(REFERENCE_DIR.glob("*.json"))) if not files: print(f"Нет файлов в {REFERENCE_DIR}") return 1 return 0 if all([validate_file(f) for f in files]) else 1 if __name__ == "__main__": raise SystemExit(main())