Core Concepts
iron-monk rests on a small philosophy and four pillars built on top of it. Read this page top-to-bottom for the why; click into each pillar for the deep-dive.
Philosophy
Most validation libraries do too much. They coerce "123" into 123, force you to inherit from a base class, and crash on the first error so you debug your payload one field at a time.
iron-monk flips all three:
- Zero coercion. If a string is not the right format, that is an error. Your data is never silently rewritten.
- Zero base classes. A
@monkclass is still a plain Pythondataclass. NoBaseModel, no metaclass tricks, no IDE slowdown. - Zero fail-fast. Every field is checked. You get every error in one shot — no playing whack-a-mole.
- Annotation is the schema. Constraints live inside
typing.Annotated. There is no separate model layer to keep in sync with your types.
Each pillar below reflects one of these choices.
The Four Pillars
-
One decorator. Three surfaces — classes, functions, methods. On classes it returns a regular
dataclass; the rest of your code does not need to knowmonkexists. -
2 · Constraints
Plain classes with a single
validate(value) -> Nonemethod. Stack them insidetyping.Annotated. Compose withAnyOf,Each,Ref. Define your own in three lines. -
3 · Validation
Three entry points matched to data shape:
validate()for instances,validate_dict()for raw dicts,validate_stream()for iterables. Plusvalidate_value()for one-shot inline checks. -
4 · Errors
Aggregated, not fail-fast. Three bad fields → three errors in one
ValidationError. Includes.flatten()and.to_rfc7807()for HTTP APIs.
Reading order
If this is your first time:
- The Decorator — bind
@monkto a class or function. - Constraints — write business rules with
Annotated. - Validation — pick the entry point that matches your data.
- Errors — collect every failure in one shot.
Each pillar is short (~50-100 lines) and self-contained. Skip ahead if a topic is already familiar.
Next Steps
- Constraints Toolkit — every built-in constraint with examples.
- Cross-Field Validation —
Refand the__monk_validate__hook. - Customization — defining your own constraints in three lines.
- Settings & Type Metadata — unwrappers, type-keyed metadata, env vars for framework integration.