import json
import os
from pathlib import Path
from typing import Any, Dict
import jsonschema
# Load schemas relative to this file
_SCHEMA_DIR = Path(__file__).parent.parent / "schemas"
def _load_schema(name: str) -> Dict[str, Any]:
path = _SCHEMA_DIR / name
if not path.exists():
raise FileNotFoundError(f"Schema not found: {path}")
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
REQUEST_SCHEMA = _load_schema("rlm.solve.request.v1.json")
RESULT_SCHEMA = _load_schema("rlm.solve.result.v1.json")
def validate_request(instance: Dict[str, Any]) -> None:
"""Validate a request against rlm.solve.request.v1.json. Raises ValidationError on failure."""
jsonschema.validate(instance, REQUEST_SCHEMA)
def validate_result(instance: Dict[str, Any]) -> None:
"""Validate a result against rlm.solve.result.v1.json. Raises ValidationError on failure."""
jsonschema.validate(instance, RESULT_SCHEMA)
def is_valid_result(instance: Dict[str, Any]) -> bool:
"""Return True if result matches schema, False otherwise."""
try:
validate_result(instance)
return True
except Exception:
return False