File size: 493 Bytes
cc29ed9
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
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}")