babylon.engine.observers.schema_validator

JSON Schema validation for observer outputs (Sprint 3.2).

Provides schema validation for NarrativeFrame and other observer JSON outputs. Uses Draft 2020-12 JSON Schema with referencing registry for $ref resolution.

Usage:

from babylon.engine.observers.schema_validator import validate_narrative_frame

frame = {"pattern": "SHOCK_DOCTRINE", "causal_graph": {...}}
errors = validate_narrative_frame(frame)
if errors:
    for error in errors:
        print(f"Validation error: {error}")

Functions

is_valid_narrative_frame(frame)

Check if a NarrativeFrame is valid against the JSON schema.

iter_validation_errors(frame)

Iterate over validation errors for a NarrativeFrame.

validate_narrative_frame(frame)

Validate a NarrativeFrame against the JSON schema.

babylon.engine.observers.schema_validator.validate_narrative_frame(frame)[source]

Validate a NarrativeFrame against the JSON schema.

Parameters:

frame (dict[str, Any]) – Dictionary representing the NarrativeFrame to validate.

Return type:

list[str]

Returns:

List of validation error messages. Empty list if valid.

Example

>>> frame = {"pattern": "TEST", "causal_graph": {"nodes": [], "edges": []}}
>>> errors = validate_narrative_frame(frame)
>>> # Returns errors because nodes must have minItems: 1
babylon.engine.observers.schema_validator.is_valid_narrative_frame(frame)[source]

Check if a NarrativeFrame is valid against the JSON schema.

Convenience method that returns a boolean instead of error list.

Parameters:

frame (dict[str, Any]) – Dictionary representing the NarrativeFrame to validate.

Return type:

bool

Returns:

True if valid, False otherwise.

Example

>>> frame = {
...     "pattern": "SHOCK_DOCTRINE",
...     "causal_graph": {
...         "nodes": [{"id": "n1", "type": "ECONOMIC_SHOCK", "tick": 0}],
...         "edges": []
...     }
... }
>>> is_valid_narrative_frame(frame)
True
babylon.engine.observers.schema_validator.iter_validation_errors(frame)[source]

Iterate over validation errors for a NarrativeFrame.

Generator version of validate_narrative_frame for memory efficiency when processing many frames.

Parameters:

frame (dict[str, Any]) – Dictionary representing the NarrativeFrame to validate.

Yields:

Validation error messages.

Return type:

Iterator[str]