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.
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
validatordecorator is used to add custom validation logic to model fields. - The
orderparameter 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
preformatandpostformatdecorators 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
orderparameter 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
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ca403978bd1ece42463c2636571b62b0a211bc290a9292bc19a95ed67a79b89
|
|
| MD5 |
35f696581cc240cf94933a262a5c3504
|
|
| BLAKE2b-256 |
403e933cc1ae17cd53eb10efe2d17ddeaab1536c096851c43596880a839cb37b
|
Provenance
The following attestation bundles were made for formax_py-0.10.1.tar.gz:
Publisher:
python-publish.yml on nshaibu/formax
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
formax_py-0.10.1.tar.gz -
Subject digest:
3ca403978bd1ece42463c2636571b62b0a211bc290a9292bc19a95ed67a79b89 - Sigstore transparency entry: 1145439245
- Sigstore integration time:
-
Permalink:
nshaibu/formax@a1300db060c599f01ee94fd65dd7cdec03626593 -
Branch / Tag:
refs/tags/v0.10.1 - Owner: https://github.com/nshaibu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@a1300db060c599f01ee94fd65dd7cdec03626593 -
Trigger Event:
release
-
Statement type: