"""A small JSON Schema validator for the subset report-v1.json uses (no jsonschema dependency).""" from __future__ import annotations import json from importlib import resources from typing import Any _TYPES: dict[str, tuple[type, ...]] = { "object": (dict,), "array": (list,), "string": (str,), "integer": (int,), "boolean": (bool,), "null": (type(None),), } def load_schema() -> dict[str, Any]: text = resources.files("greenproof").joinpath("schema/report-v1.json").read_text() schema = json.loads(text) assert isinstance(schema, dict) return schema def _type_ok(value: Any, name: str) -> bool: if name == "integer" and isinstance(value, bool): return False return isinstance(value, _TYPES[name]) def validate(value: Any, schema: dict[str, Any], path: str = "$") -> list[str]: errors: list[str] = [] if "const" in schema and value != schema["const"]: errors.append(f"{path}: expected {schema['const']!r}, got {value!r}") if "enum" in schema and value not in schema["enum"]: errors.append(f"{path}: {value!r} not in {schema['enum']}") if "type" in schema: names = schema["type"] if isinstance(schema["type"], list) else [schema["type"]] if not any(_type_ok(value, n) for n in names): errors.append(f"{path}: expected {names}, got {type(value).__name__}") return errors if isinstance(value, int) and not isinstance(value, bool): if "minimum" in schema and value < schema["minimum"]: errors.append(f"{path}: {value} < minimum {schema['minimum']}") if "maximum" in schema and value > schema["maximum"]: errors.append(f"{path}: {value} > maximum {schema['maximum']}") if isinstance(value, dict): props = schema.get("properties", {}) for key in schema.get("required", []): if key not in value: errors.append(f"{path}: missing required key {key!r}") for key, item in value.items(): if key in props: errors.extend(validate(item, props[key], f"{path}.{key}")) elif schema.get("additionalProperties") is False: errors.append(f"{path}: unexpected key {key!r}") if isinstance(value, list) and "items" in schema: for i, item in enumerate(value): errors.extend(validate(item, schema["items"], f"{path}[{i}]")) return errors