Spaces:
Sleeping
Sleeping
Rajan Sharma
commited on
Create validators/schema_validator.py
Browse files
validators/schema_validator.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from jsonschema import validate, Draft202012Validator
|
| 3 |
+
|
| 4 |
+
def assert_valid(data: dict, schema_path: str):
|
| 5 |
+
with open(schema_path, "r", encoding="utf-8") as f:
|
| 6 |
+
schema = json.load(f)
|
| 7 |
+
validator = Draft202012Validator(schema)
|
| 8 |
+
errors = sorted(validator.iter_errors(data), key=lambda e: e.path)
|
| 9 |
+
if errors:
|
| 10 |
+
details = "\n".join([f"- {e.message} @ {'/'.join(map(str, e.path))}" for e in errors])
|
| 11 |
+
raise ValueError(f"Schema validation failed:\n{details}")
|