-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnested_schemas.py
More file actions
49 lines (41 loc) · 1.19 KB
/
nested_schemas.py
File metadata and controls
49 lines (41 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Nested schema composition with Optional keys and a nested union."""
from zodify import Optional, validate
def main() -> None:
print("=== Success: nested composition ===")
db_schema = {
"host": str,
"port": int,
}
service_schema = {
"name": str,
"mode": str | int,
}
config_schema = {
"db": db_schema,
"service": service_schema,
"debug": Optional(bool, False),
"region": Optional(str),
}
config_data = {
"db": {"host": "localhost", "port": "5432"},
"service": {"name": "api", "mode": 1},
}
print(validate(config_schema, config_data, coerce=True))
print()
print("=== Error: nested missing key ===")
try:
validate(
config_schema,
{"db": {"host": "localhost"}, "service": {"name": "api", "mode": 1}},
)
except ValueError as error:
print(error)
print()
if __name__ == "__main__":
main()
# Expected output:
# === Success: nested composition ===
# {'db': {'host': 'localhost', 'port': 5432}, 'service': {'name': 'api', 'mode': 1}, 'debug': False}
#
# === Error: nested missing key ===
# db.port: missing required key