mail-address-semantics/mas_modes/__init__.py

46 lines
1.3 KiB
Python
Raw Normal View History

"""MAS mode contract v0.1: email hands a case to another mode, or works
inside a control flow it does not own (`mas-modes-v0.1-spec/CONTRACT.md`).
`schema/` and `conformance/` are vendored unchanged from the bundle.
"""
from __future__ import annotations
import json
from functools import lru_cache
from importlib import resources
CONTRACT_VERSION = "0.1"
def _load(name: str):
return json.loads(resources.files(__name__).joinpath(name).read_text(encoding="utf-8"))
@lru_cache(maxsize=None)
def _validator(schema_file: str):
from jsonschema import Draft202012Validator
return Draft202012Validator(_load(f"schema/{schema_file}"))
def message_errors(msg: dict) -> list[str]:
"""Schema errors for a contract message; empty if valid."""
return [e.message for e in _validator("mas-modes-v0.1.schema.json").iter_errors(msg)]
def event_errors(event: dict) -> list[str]:
"""Schema errors for a normalized gateway event; empty if valid."""
return [e.message for e in _validator("mas-event-v0.1.schema.json").iter_errors(event)]
def transitions() -> dict:
return _load("conformance/transitions.json")
def message_fixtures() -> dict:
return _load("conformance/messages.json")
def flow_fixtures() -> list[dict]:
return _load("conformance/flows.json")["flows"]