Skip to main content

High-performance Python model builder and validation engine with zero dependencies

Project description

Formax

Formax — Fast, Flexible, Fully Validated Python Models.

High-performance Python model builder and validation engine with configurable performance tiers.
Formax is designed for developers who want the flexibility of modern validation frameworks with the speed of lightweight data models.


Build Status Code style: black Status Latest PyV codecov PRs Welcome

Quick Example

from formax import BaseModel

class User(BaseModel):
    id: int
    name: str
    active: bool

user = User(id=1, name="Alice", active=True)
print(user.name)  # Output: Alice

Features

  • ⚡ High-performance model construction
  • 🧩 Configurable validation tiers
  • 🏗 Class builder with schema enforcement
  • 🔍 Type validation and constraint support
  • 🔌 Extensible serialization formatters
  • 🧠 Descriptor-based field specialization
  • 📦 Zero runtime dependencies

Performance

Formax is optimized for speed and minimal overhead.

Library Mean init time
Formax (fast mode) ~585 ns
Dataclasses ~680 ns
Pydantic ~1.5 µs

Benchmarks run on Python 3.10.


Installation

pip install formax-py

Model Features

from formax import BaseModel, Attrib, MiniAnnotated

class Person(BaseModel):
    name: str
    age: MiniAnnotated[int, Attrib(gt=0, lt=120)]
    email: MiniAnnotated[str, Attrib(pattern=r"^\S+@\S+\.\S+$")]

Person(name="Alice", age=30, email="alice@example.com")  # ✅ Valid
Person(name="Bob", age=-1, email="bob@example")    # ❌ Raises ValidationError

Validation Hooks

from formax import BaseModel, validator

class User(BaseModel):
    password: str
    confirm_password: str
    
    @validator(["password", "confirm_password"], order=1)
    def validate_password_length(self, value):
        if len(value) < 8:
            raise ValueError("Password must be at least 8 characters long")
    
    @validator(["confirm_password"], order=10)
    def validate_passwords_match(self, value):
        if value != self.password:
            raise ValueError("Passwords do not match")
  • The validator decorator is used to add custom validation logic to model fields.
  • The order parameter determines the order in which the validators are executed.

The model above can also be expressed without using the decorators as below:

from formax import BaseModel, MiniAnnotated, Attrib

def confirm_password(instance, value):
    if value != instance.password:
        raise ValueError( "Passwords do not match"

class User(BaseModel):
    password: MiniAnnotated[str, Attrib(min_length=8)]
    confirm_password: MiniAnnotated[str, Attrib(min_length=8, validators=[confirm_password])]

Formatting Hooks

from datetime import datetime
from formax import BaseModel, preformat, postformat

class Address(BaseModel):
    city: str
    timestamp: float
    
    @preformat(["city"], order=1)
    def format_city(self, value) -> str:
        return value.title()
    
    @preformat(["timestamp"], order=1)
    def format_timestamp(self, value: datetime) -> float:
        return value.timestamp()
    
    @postformat(["timestamp"], order=1)
    def postformat_timestamp(self, value: float) -> datetime:
        return datetime.fromtimestamp(value)
    
addr = Address(city="london", timestamp=datetime.now())
print(addr.city)  # Output: London
print(addr.timestamp)  # Output: datetime object
  • The preformat and postformat decorators are used to format and postprocess model fields.
  • They can be used to transform data before it is assigned to the field or to transform data after it is retrieved from the field.
  • The order parameter determines the order in which the formatting hooks are executed.

The model above can also be expressed without using the decorators as below:

from datetime import datetime
from formax import BaseModel, MiniAnnotated, Attrib

class Address(BaseModel):
    city: MiniAnnotated[str, Attrib(pre_formatter=lambda instance, value: value.title())]
    timestamp: MiniAnnotated[float, Attrib(pre_formatter=lambda instance, value: value.timestamp(), post_formatter=lambda instance, value: datetime.fromtimestamp(value)]

Configurable Performance Tiers

Formax allows validation to be tuned at class definition time.

from formax import BaseModel, ValidationFlags, InitStrategy

class User(BaseModel):
    id: int

    class Config:
        validation = ValidationFlags.TYPECHECK
        init_strategy = InitStrategy.FAST

Balance speed and validation strictness.


Comparison

Feature Formax Pydantic Dataclasses
Validation
Configurable performance
Zero dependencies
Extensible formatting

Philosophy

Formax focuses on:

  • Predictable performance
  • Explicit validation control
  • Minimal runtime overhead

Contributing

Contributions are welcome! Please open an issue or submit a pull request.


License

GPLv3

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

formax_py-0.10.1.tar.gz (77.9 kB view details)

Uploaded Source

File details

Details for the file formax_py-0.10.1.tar.gz.

File metadata

  • Download URL: formax_py-0.10.1.tar.gz
  • Upload date:
  • Size: 77.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for formax_py-0.10.1.tar.gz
Algorithm Hash digest
SHA256 3ca403978bd1ece42463c2636571b62b0a211bc290a9292bc19a95ed67a79b89
MD5 35f696581cc240cf94933a262a5c3504
BLAKE2b-256 403e933cc1ae17cd53eb10efe2d17ddeaab1536c096851c43596880a839cb37b

See more details on using hashes here.

Provenance

The following attestation bundles were made for formax_py-0.10.1.tar.gz:

Publisher: python-publish.yml on nshaibu/formax

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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