|
17 | 17 | from policyengine_household_api.utils.deprecated_inputs import ( |
18 | 18 | drop_deprecated_inputs, |
19 | 19 | ) |
| 20 | +from policyengine_household_api.utils.variable_validation import ( |
| 21 | + validate_household_variables, |
| 22 | +) |
20 | 23 | from policyengine_household_api.utils.validate_country import validate_country |
21 | 24 |
|
22 | 25 |
|
@@ -127,15 +130,44 @@ def get_calculate(country_id: str, add_missing: bool = False) -> Response: |
127 | 130 |
|
128 | 131 | country = COUNTRIES.get(country_id) |
129 | 132 |
|
130 | | - # Strip deprecated inputs before validation so partners who still pass |
131 | | - # removed/renamed variables get a warning + working response instead |
132 | | - # of a `VariableNotFoundError` HTTP 500. |
133 | | - deprecation_warnings = drop_deprecated_inputs(household_json) |
134 | | - |
135 | 133 | # Validate inbound payload shape before reaching the compute layer. |
136 | 134 | try: |
137 | 135 | _validate_household_payload(country_id, household_json) |
138 | 136 | _validate_axes(household_json) |
| 137 | + except ValueError as e: |
| 138 | + return Response( |
| 139 | + json.dumps({"status": "error", "message": str(e)}), |
| 140 | + status=400, |
| 141 | + mimetype="application/json", |
| 142 | + ) |
| 143 | + |
| 144 | + variable_errors = validate_household_variables( |
| 145 | + household=household_json, |
| 146 | + system=country.tax_benefit_system, |
| 147 | + model_version=country.policyengine_bundle["model_version"], |
| 148 | + ) |
| 149 | + if variable_errors: |
| 150 | + return Response( |
| 151 | + json.dumps( |
| 152 | + { |
| 153 | + "status": "error", |
| 154 | + "message": "Invalid household variables.", |
| 155 | + "errors": [error.message for error in variable_errors], |
| 156 | + } |
| 157 | + ), |
| 158 | + status=400, |
| 159 | + mimetype="application/json", |
| 160 | + ) |
| 161 | + |
| 162 | + # Strip deprecated inputs from a copy before period validation so |
| 163 | + # partners who still pass removed/renamed variables get a warning + |
| 164 | + # working response instead of a `VariableNotFoundError` HTTP 500. |
| 165 | + deprecated_inputs = drop_deprecated_inputs(household_json) |
| 166 | + household_json = deprecated_inputs.household |
| 167 | + deprecation_warnings = deprecated_inputs.warnings |
| 168 | + |
| 169 | + # Validate inbound period data before reaching the compute layer. |
| 170 | + try: |
139 | 171 | validate_period_keys(household_json, country.tax_benefit_system) |
140 | 172 | validate_period_budgets(household_json, country.tax_benefit_system) |
141 | 173 | except ValueError as e: |
|
0 commit comments