{ D }
V3.2.0

Validation Recipes

This page contains example of field validation on many type of data which could give enough idea about how to use dictify.Field

For text validation, you can use match() or search() with appropriate Regular Expression patterns.

Instance of ...

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

True / False

Field().instance(bool)

Numbers

Using instance() and verify() with Python logical and compare operators can verify numbers in many ways.

# 1. number is instance of int or float.
# 2. number is in range [0,10].
Field()\
    .instance((int, float))\
    .verify(lambda value: 0 <= value <= 10)

Subset

Field contains list of days.

Field()\
    .listof(str)\
    .verify(lambda dates:
        set(dates) <= set(['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su']))

Choices

Field()\
    .instance(str)\
    .verify(lambda os:
        os in ['android', 'ios'])

Email

Field().match('.+@.+')

Time in ISO format

from datetime import datetime

Field().verify(lambda dt: datetime.fromisoformat(dt))

Images

import io
from PIL import Image

Field()\
    .instance(io.BytesIO)\
    .verify(lambda img:
        Image.open(img).format == 'PNG' or 'JPEG' or 'WEBP' or 'GIF')

UUID (Universally Unique Identifier)

import uuid

Field().instance(uuid.UUID)

# UUID in `str` instance.
Field()\
    .instance(str)\
    .verify(lambda value: uuid.UUID(value))