This commit is contained in:
1 parent
d0471050fc
commit
039e5b80df
1 file changed
+22
@@ -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)
|
||||||
Reference in new issue
Block a user