-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfrom_dict_validation.py
More file actions
96 lines (78 loc) · 2.69 KB
/
Copy pathfrom_dict_validation.py
File metadata and controls
96 lines (78 loc) · 2.69 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from __future__ import annotations
from typing import Any
from models.errors import SchemaError
def require_dict(raw: Any, *, model: str, field: str) -> dict[str, Any]:
"""Raise SchemaError when raw is not a dict; return raw for chaining."""
if not isinstance(raw, dict):
raise SchemaError(
model,
field,
hint=f"expected object, got {type(raw).__name__}",
)
return raw
def require_key(raw: dict[str, Any], key: str, *, model: str) -> None:
"""Raise SchemaError when a required key is absent."""
if key not in raw:
raise SchemaError(model, key)
def require_non_empty_str(value: Any, *, model: str, field: str) -> str:
"""Validate a caller-supplied id (workspace_id, composer_id, bubble_id)."""
if not isinstance(value, str) or not value:
raise SchemaError(
model,
field,
hint=f"expected non-empty str, got {type(value).__name__}",
)
return value
def require_non_empty_str_field(raw: dict[str, Any], key: str, *, model: str) -> str:
"""Validate a non-empty string field read from raw."""
value = raw.get(key)
if not isinstance(value, str) or not value:
raise SchemaError(
model,
key,
hint=f"expected non-empty str, got {type(value).__name__}",
)
return value
def require_non_empty_str_fields(
raw: dict[str, Any],
keys: tuple[str, ...],
*,
model: str,
) -> None:
"""Validate multiple non-empty string fields in raw (ExportEntry pattern)."""
for key in keys:
value = raw.get(key)
if not isinstance(value, str) or value == "":
raise SchemaError(
model,
key,
hint=f"expected non-empty str, got {type(value).__name__}",
)
def require_truthy(value: Any, *, model: str, field: str) -> Any:
"""Raise missing-field SchemaError when value is falsy (absent or empty)."""
if not value:
raise SchemaError(model, field)
return value
def require_type(
value: Any,
expected: type[Any] | tuple[type[Any], ...],
*,
model: str,
field: str,
hint: str | None = None,
) -> Any:
if not isinstance(value, expected):
raise SchemaError(
model,
field,
hint=hint or f"expected {expected!r}, got {type(value).__name__}",
)
return value
def require_optional_str(value: Any, *, model: str, field: str) -> str | None:
if value is not None and not isinstance(value, str):
raise SchemaError(
model,
field,
hint=f"expected str or None, got {type(value).__name__}",
)
return value