This page collects small examples for common validation patterns with dictify.Field.

For Model classes, prefer Python annotations for base field types and use Field(...) for defaults and extra validators.

For text validation, match() and search() work well with regular expressions.

Instance of a Type

Field().instance(str)
Field().instance(int)

Boolean

Field().instance(bool)

Numbers in a Range

Field().instance((int, float)).verify(lambda value: 0 <= value <= 10)

Subset of Allowed Values

Field().listof(str).verify(
    lambda dates: set(dates) <= {"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"}
)

Choices

Field().instance(str).verify(lambda value: value in ["android", "ios"])

Email

Field().instance(str).match(r".+@.+")

ISO Datetime String

from datetime import datetime

Field().instance(str).func(datetime.fromisoformat)
# parses the string and stores the resulting datetime

UUID String

import uuid

Field().instance(str).verify(lambda value: uuid.UUID(value))

Nested Model

from dictify import Field, Model


class Money(Model):
    unit: str = Field(required=True).verify(lambda value: value in ["USD", "GBP"])
    amount: int | float = Field(required=True)


class Payment(Model):
    id: str = Field(required=True)
    money: Money = Field(required=True)


payment = Payment(
    {
        "id": "pay-1",
        "money": {"unit": "USD", "amount": 10.0},
    }
)

List of Models

from dictify import Field, Model


class Contact(Model):
    type: str = Field(required=True)
    value: str = Field(required=True)


contacts = Field().listof(Contact)
contacts.value = [
    {"type": "email", "value": "user@example.com"},
    {"type": "phone", "value": "111-800-0000"},
]