Medica_DecisionSupportAI / validators /schema_validator.py
Rajan Sharma
Create validators/schema_validator.py
cc29ed9 verified
raw
history blame contribute delete
493 Bytes
import json
from jsonschema import validate, Draft202012Validator
def assert_valid(data: dict, schema_path: str):
with open(schema_path, "r", encoding="utf-8") as f:
schema = json.load(f)
validator = Draft202012Validator(schema)
errors = sorted(validator.iter_errors(data), key=lambda e: e.path)
if errors:
details = "\n".join([f"- {e.message} @ {'/'.join(map(str, e.path))}" for e in errors])
raise ValueError(f"Schema validation failed:\n{details}")