Skip to content

Commit 815f3f2

Browse files
committed
Refactor axes validation helpers
1 parent 395e6e1 commit 815f3f2

1 file changed

Lines changed: 34 additions & 20 deletions

File tree

policyengine_household_api/endpoints/household.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,40 @@ def _validate_axes(household_json: dict) -> None:
6565
)
6666

6767
for i, entry in enumerate(axes):
68-
# Each entry is itself a list of axis specifications (supports
69-
# nested/crossed scans in policyengine-core).
70-
entries = entry if isinstance(entry, list) else [entry]
71-
for axis in entries:
72-
if not isinstance(axis, dict):
73-
raise ValueError(
74-
f"'axes[{i}]' must be an object or list of objects"
75-
)
76-
count = axis.get("count")
77-
if count is None:
78-
continue
79-
try:
80-
count_int = int(count)
81-
except (TypeError, ValueError):
82-
raise ValueError(f"'axes[{i}].count' must be an integer")
83-
if count_int < 1 or count_int > MAX_AXES_COUNT:
84-
raise ValueError(
85-
f"'axes[{i}].count' must be between 1 and "
86-
f"{MAX_AXES_COUNT}; got {count_int}"
87-
)
68+
for axis in _axes_entry_specs(entry, i):
69+
_validate_axis_count(axis, i)
70+
71+
72+
def _axes_entry_specs(entry, index: int) -> list[dict]:
73+
# Each entry may itself be a list of axis specifications, which
74+
# supports nested/crossed scans in policyengine-core.
75+
axes = entry if isinstance(entry, list) else [entry]
76+
for axis in axes:
77+
if not isinstance(axis, dict):
78+
raise ValueError(
79+
f"'axes[{index}]' must be an object or list of objects"
80+
)
81+
return axes
82+
83+
84+
def _validate_axis_count(axis: dict, index: int) -> None:
85+
count = axis.get("count")
86+
if count is None:
87+
return
88+
89+
count_int = _parse_axis_count(count, index)
90+
if count_int < 1 or count_int > MAX_AXES_COUNT:
91+
raise ValueError(
92+
f"'axes[{index}].count' must be between 1 and "
93+
f"{MAX_AXES_COUNT}; got {count_int}"
94+
)
95+
96+
97+
def _parse_axis_count(count, index: int) -> int:
98+
try:
99+
return int(count)
100+
except (TypeError, ValueError):
101+
raise ValueError(f"'axes[{index}].count' must be an integer")
88102

89103

90104
@validate_country

0 commit comments

Comments
 (0)