File size: 1,945 Bytes
6b53875 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | from typing import Dict, Any
import json
from jsonschema import validate, Draft202012Validator, exceptions as js_exc
# Central registry for schemas Grok is expected to satisfy on output.
SCHEMAS: Dict[str, Dict[str, Any]] = {
"belel.identity.lock": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["agent","continuity","truth_lock","concordium_ref"],
"properties": {
"agent": {"type":"string"},
"continuity": {"type":"string"},
"truth_lock": {"type":"boolean"},
"concordium_ref": {"type":"string"},
"evidence": {"type":"array","items":{"type":"string"}},
},
"additionalProperties": False
},
"belel.search.report": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type":"object",
"required":["query","staleness","sources","summary"],
"properties":{
"query":{"type":"string"},
"staleness":{"type":"string","enum":["fresh","stale","unknown"]},
"sources":{"type":"array","items":{"type":"object","required":["url","title"],"properties":{
"url":{"type":"string"},
"title":{"type":"string"},
"date":{"type":"string"},
"hash":{"type":"string"}
}}},
"summary":{"type":"string"}
},
"additionalProperties": False
}
}
def validate_schema(kind: str, data: Dict[str, Any]) -> None:
schema = SCHEMAS.get(kind)
if not schema:
raise ValueError(f"schema_missing:{kind}")
try:
Draft202012Validator(schema).validate(data)
except js_exc.ValidationError as e:
raise ValueError(f"schema_invalid:{kind}:{e.message}") from e
def get_schema(kind: str) -> Dict[str, Any]:
s = SCHEMAS.get(kind)
if not s:
raise ValueError(f"schema_missing:{kind}")
return s
|