PLAIN PYTHON · ZERO RUNTIME DEPS
Validate a configuration dict
Catch missing keys and misspelled configuration, with an explicit strip alternative.
Validate once at the configuration boundary
Keep strict validation for configuration that already has Python values. Reject unknown keys to catch misspellings. Insert a simple immutable default only for an intentionally optional setting.
from zodify import Optional, validate
schema = {"host": str, "port": int, "debug": Optional(bool, False)}
config = {"host": "localhost", "port": 8080}
assert validate(schema, config)["debug"] is False
with_typo = dict(config, debgu=True)
try:
validate(schema, with_typo)
except ValueError:
pass # Reject unknown keys: catch misspelled configuration.
else:
raise AssertionError("Expected unknown-key failure")
clean = validate(schema, with_typo, unknown_keys="strip")
assert "debgu" not in clean # Explicitly drops data; does not repair the typo.
assert "debgu" in with_typo # Original input is unchanged here.
print("config-validation: passed")
Expected output: config-validation: passed
Make data loss an explicit choice
unknown_keys='strip' removes unknown fields from the returned shape; it does not fix misspellings. A type-only port check does not enforce the valid network-port range. Add a reviewed predicate if your application needs that constraint.
This recipe validates a dict you already have. It does not load .env files, manage secrets, or infer configuration precedence.