Skip to main content

Documents schema and data validation

Project description

Dictify

Schema and data validation for Python mappings and JSON-like documents.

dictify is a lightweight library for standalone field validation and mapping-shaped models with annotated fields.

  • Python 3.12+
  • Use Field(...) for defaults, required fields, and extra validators
  • Use Python annotations to declare Model field types
  • Access model data with either attributes or mapping syntax

Install

pip install dictify

Quick Example

from datetime import UTC, datetime
from typing import Annotated

from dictify import Field, Model


class Note(Model):
    title: str = Field(required=True).verify(
        lambda value: len(value) <= 300,
        "Title must be 300 characters or fewer",
    )
    content: str = Field()
    timestamp: Annotated[datetime, "creation time"] = Field(
        default=lambda: datetime.now(UTC)
    )


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=True rejects undeclared keys and undeclared public attributes
  • strict=False stores 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())

Standalone Fields

Field.instance(...) still works well for standalone validation.

email = Field(required=True).instance(str).match(r".+@.+")
email.value = "user@example.com"

Typing Status

The annotation-first model API is fully supported at runtime.

Static type checker support for declarations like email: str = Field(...) is still limited and may require cast(Any, Field(...)) depending on the checker and editor.

Documentation

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

dictify-4.0.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dictify-4.0.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file dictify-4.0.0.tar.gz.

File metadata

  • Download URL: dictify-4.0.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for dictify-4.0.0.tar.gz
Algorithm Hash digest
SHA256 f4f41480e65e0e4e3cc2466dc3823862c8b7f9cff63604d36ecda8b7e2278cd1
MD5 cbdb38040145ded8030150925c5592cd
BLAKE2b-256 cad697115c6b02a91d943d488734da4ff2ba5c1462d9ca3e25e1f586cc396119

See more details on using hashes here.

File details

Details for the file dictify-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: dictify-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for dictify-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 748bee09fda5dff102910466cb0b5b84ab232b1931afd3309bb11d68620f6dce
MD5 bcda06f1b8f8e456497c13681cb7182f
BLAKE2b-256 32ef1104b2c380bf83c21813f155d1cd9aa30f4a3ddf64bea6b798d4bcb932a8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page