diff --git a/app/core/schema_loader.py b/app/core/schema_loader.py new file mode 100644 index 0000000..019755f --- /dev/null +++ b/app/core/schema_loader.py @@ -0,0 +1,22 @@ +from __future__ import annotations +import json +import os +from functools import lru_cache +from jsonschema import Draft202012Validator, RefResolver + +SCHEMAS_DIR = os.getenv("SCHEMAS_DIR", os.path.join(os.path.dirname(os.path.dirname(__file__)), "..", "schemas")) + +@lru_cache(maxsize=16) +def load_schema(name: str) -> dict: + # name: "note.schema.json" | "chunk.schema.json" | "edge.schema.json" + path = os.path.join(SCHEMAS_DIR, name) + if not os.path.isfile(path): + raise FileNotFoundError(f"Schema not found: {path}") + with open(path, "r", encoding="utf-8") as f: + return json.load(f) + +@lru_cache(maxsize=16) +def get_validator(name: str) -> Draft202012Validator: + schema = load_schema(name) + resolver = RefResolver.from_schema(schema) + return Draft202012Validator(schema, resolver=resolver)