Skip to main content

A library for definition, validation and serialisation of Python objects based on JSONSchema specifications.

Project description

Momoa

A library for definition, validation and serialisation of models based on JSON Schema specifications.

Documentation Status Build Status

Basic Usage

StathamEngine (default)

from datetime import datetime
from momoa import Schema
from momoa.engines.statham import UNDEFINED

schema = Schema.from_uri("file://path/to/schema.json")
PersonModel = schema.model

person = PersonModel(firstName="Boris", lastName="Harrison")

# Unset optional fields return UNDEFINED
assert person.age is UNDEFINED

# Fields can be set after instantiation
person.age = 53
person.birthday = datetime(1969, 11, 23)

assert person.age == 53
assert person.serialize()["birthday"] == "1969-11-23"

PydanticEngine

from momoa import Schema
from momoa.engines.pydantic import PydanticEngine

schema = Schema.from_uri("file://path/to/schema.json", engine=PydanticEngine())
PersonModel = schema.model  # a pydantic.BaseModel subclass

person = PersonModel(firstName="Boris", lastName="Harrison", age=53)

# Unset optional fields return None
assert person.birthday is None

# Serialize omits unset fields
serialized = person.serialize()
assert serialized["firstName"] == "Boris"
assert "birthday" not in serialized

# Pydantic's native API is also available
person.model_dump(exclude_unset=True)
PersonModel.model_validate({"firstName": "Alice", "lastName": "Smith"})

Engine Selection

Momoa compiles schemas through a pluggable engine. The default is StathamEngine. To use Pydantic v2 BaseModel subclasses instead, pass engine= explicitly:

from momoa import Schema
from momoa.engines.pydantic import PydanticEngine

schema = Schema.from_file("path/to/schema.json", engine=PydanticEngine())
person = schema.deserialize({"firstName": "Alice", "lastName": "Smith"})
print(person.model_dump())  # standard Pydantic API also available

The active engine can also be set via the MOMOA_DEFAULT_ENGINE environment variable (statham or pydantic), which is useful for switching without changing code.

Compatibility

Momoa supports two engines with different JSON Schema draft coverage:

Engine Backend JSON Schema drafts
StathamEngine (default) Statham Draft 6
PydanticEngine datamodel-code-generator + Pydantic v2 Draft 4 – 2020-12

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

momoa-0.4.0.tar.gz (83.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

momoa-0.4.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file momoa-0.4.0.tar.gz.

File metadata

  • Download URL: momoa-0.4.0.tar.gz
  • Upload date:
  • Size: 83.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for momoa-0.4.0.tar.gz
Algorithm Hash digest
SHA256 9ac01af5c54ba3d2a81fceaddb32485144737f4c488509dfb7e03b16409e2deb
MD5 6a6d53573a7a299f6ab8ee1c262c27f4
BLAKE2b-256 e0eef003aba6d6371bf379a9d71b9bd55494c76c4a8d32e9680722b3751e7c01

See more details on using hashes here.

File details

Details for the file momoa-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: momoa-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for momoa-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 888476379e82f35d7ba7aab1a1892422c47ae766093f41e0f1172ea5a181a5f8
MD5 83769d1d6c58975761ec67e2f1506efb
BLAKE2b-256 90f3a8f73afb45073049bb64226c813053ff3d3704b2dceb33d48a096908950e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page