Skip to content

momoa

source package momoa

Basic class to parse a schema and prepare the model class.

Classes

  • EngineResult Result of compiling a schema spec into model classes.

  • ModelEngine Compiles a normalised schema spec into model classes.

  • SchemaError Generic JSON Schema error.

  • UnknownEngineError Error when an unknown engine name is requested.

  • Schema Basic class to parse the schema and prepare the model class.

Functions

  • resolve_engine Look up a registered engine by name, raising UnknownEngineError for unknown names.

source dataclass EngineResult(models: tuple[type, ...])

Result of compiling a schema spec into model classes.

Attributes

  • model : type The primary (root) model class.

source property EngineResult.model: type

The primary (root) model class.

source class ModelEngine()

Bases : Protocol

Compiles a normalised schema spec into model classes.

Attributes

  • output_format : str 'statham', 'pydantic', 'dataclass', etc.

Methods

  • compile Returns models built from the spec. Pure: same input -> equivalent output.

  • context_labeller Returns a context_labeller for json_ref_dict.materialize, or None.

source property ModelEngine.output_format: str

'statham', 'pydantic', 'dataclass', etc.

source method ModelEngine.compile(spec: dict[str, Any], *, root_name: str | None = None)EngineResult

Returns models built from the spec. Pure: same input -> equivalent output.

source method ModelEngine.context_labeller()Callable[[str], tuple[str, Any]] | None

Returns a context_labeller for json_ref_dict.materialize, or None.

Engines that need schema nodes labelled during URI materialisation (e.g. to derive names for anonymous nested objects) return a callable here. Engines that work from plain schema dicts return None.

source resolve_engine(name: str)ModelEngine

Look up a registered engine by name, raising UnknownEngineError for unknown names.

Raises

source class SchemaError()

Bases : Exception

Generic JSON Schema error.

source class UnknownEngineError(name: str, valid: str)

Bases : SchemaError

Error when an unknown engine name is requested.

source class Schema(schema: dict[str, Any], *, engine: ModelEngine | None = None)

Basic class to parse the schema and prepare the model class.

Constructs the Schema class instance.

Parameters

  • schema : dict[str, Any] A Python dict representation of the JSONSchema specification.

  • engine : ModelEngine | None A ModelEngine instance to compile the schema. If None, uses the engine specified by MOMOA_DEFAULT_ENGINE env var, or StathamEngine.

Attributes

  • model : type Retrieves the top model class of the schema.

Methods

  • from_uri Instantiates the Schema from a URI to the schema document.

  • from_file Helper to instantiate the Schema from a local file path.

  • deserialize Converts raw data to the Model instance, validating it in the process.

source classmethod Schema.from_uri(input_uri: str, engine: ModelEngine | None = None)Schema

Instantiates the Schema from a URI to the schema document.

For local files use the file:// scheme. This method also dereferences the internal $ref links.

Parameters

  • input_uri : str String representation of the URI to the schema.

  • engine : ModelEngine | None Optional ModelEngine to use for compilation.

Returns

source classmethod Schema.from_file(file_path: Path | str, engine: ModelEngine | None = None)Schema

Helper to instantiate the Schema from a local file path.

Note: This method will not dereference any internal $ref links.

Parameters

  • file_path : Path | str Either a simple string path or a pathlib.Path object.

  • engine : ModelEngine | None Optional ModelEngine to use for compilation.

Returns

source property Schema.model: type

Retrieves the top model class of the schema.

Returns Model subclass.

source method Schema.deserialize(raw_data: Mapping[str, Any] | str)Any

Converts raw data to the Model instance, validating it in the process.

Parameters

  • raw_data : Mapping[str, Any] | str The raw data to deserialize. Can be either a JSON string or a preloaded Python mapping object.

Returns

  • Any An instance of the Model class.