PLAIN PYTHON · ZERO RUNTIME DEPS
Schemas and predicates
The released dict, list, union, and callable schema grammar.
Describe a shape with Python values
A dict maps field names to schemas. A one-element list describes each item in a list. A Python type requires that exact type. PEP 604 unions combine types. Optional wraps an optional field.
Predicates receive the original value and accept it when they return a truthy result. They do not transform values. A predicate should check its own input type before comparing or indexing it.
from zodify import validate
schema = {"users": [{"name": str}], "enabled": bool}
assert validate(schema, {"users": [{"name": "Ada"}], "enabled": True})["users"][0]["name"] == "Ada"
# A predicate accepts the original value when it returns a truthy result.
assert validate({"port": lambda x: type(x) is int and 1 <= x <= 65535}, {"port": 8080}) == {"port": 8080}
print("schemas: passed")
Expected output: schemas: passed
Keep schema boundaries explicit
Top-level data must be a dict (or a supported Schema declaration at the schema boundary). Lists with zero or multiple schema items are invalid. Malformed schemas may raise TypeError. Validation has a max_depth option, defaulting to 32; cyclic models and arbitrary typing expressions are not promised.
Dict-shaped and list-shaped validation creates result containers, but leaf objects and defaults can remain shared. Validation is not a universal deep copy.