Dictify
Dictify
Lightweight validation for Python mappings and JSON-like documents.
dictify is a lightweight validation library for standalone fields and mapping-shaped models. It is designed for small schema layers, partial validation, and annotation-first models with explicit dict-like behavior.
Why Dictify?¶
- Validate a single value with
Field(...)without defining a full model - Define annotation-first
Modelclasses for dict-shaped documents - Keep mapping access and attribute access together
- Handle unknown keys and public attributes explicitly with
_strict - Convert back to plain Python data with
dict(model)andmodel.dict()
Install¶
pip install dictify
Quick Example¶
from datetime import UTC, datetime
from typing import Annotated
from dictify import Field, Model
class Note(Model):
title: Annotated[
str,
Field(required=True).verify(
lambda value: len(value) <= 300,
"Title must be 300 characters or fewer",
),
]
content: Annotated[str, Field()]
timestamp: Annotated[
datetime,
"creation time",
Field(default=lambda: datetime.now(UTC)),
]
Create and update validated data:
note = Note(title="Dictify", content="dictify is easy")
note.content = "Updated content"
note["content"] = "Updated again"
# These raise Model.Error.
note.title = 0
note["title"] = 0
Strict Mode¶
Model is strict by default.
_strict=Truerejects undeclared keys and undeclared public attributes_strict=Falsestores undeclared keys and attributes as extra model data
note = Note({"title": "Dictify"}, _strict=False)
note.category = "docs"
assert note["category"] == "docs"
assert dict(note)["category"] == "docs"
Native Conversion¶
Use explicit conversion when you need plain Python data.
import json
note_dict = dict(note) # shallow dict conversion
note_native = note.dict() # recursive dict/list conversion
note_json = json.dumps(note.dict())