Validation for Python dictionaries
Validate configuration and script inputs using ordinary Python types. Zero required runtime dependencies.
Get startedpip install zodifyfrom zodify import validate
schema = {"port": int, "debug": bool}
data = {"port": 8080, "debug": False}
print(validate(schema, data))
{'port': 8080, 'debug': False}Invalid types and extra keys raise an error by default.
When to use zodify
Use it when you have a Python dict and want a readable, reusable check. Start with ordinary types; add nested shapes, optional keys, or explicit conversions when you need them.
| Try zodify when… | Another approach may fit when… |
|---|---|
| You want reusable checks for dictionaries. | A few direct Python checks already solve the task. |
| You validate configuration or script inputs. | Your framework already handles validation. |
| Zero required runtime dependencies matter. | You need broader typing, models, or serialization. |
Validation errors
An invalid field raises an error with its path. Here, port needs an int, but the input contains a string. Pass an integer, or choose coerce=True when string conversion is intended.
from zodify import validate
try:
validate({"port": int}, {"port": "8080"})
except ValueError as error:
print(f"ValueError: {error}")
ValueError: port: expected int, got strDefault behavior
Types are strict and extra keys are rejected. Optional keys and nullable values are separate choices. Defaults are trusted: they are inserted without validation or copying, so mutable defaults can be shared.
Plain dict schemas return dicts. Optional class declarations provide attribute access.