Skip to content

Commit 32385ff

Browse files
validate attribute target
1 parent 743168b commit 32385ff

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/rules_validation_api/validators/iteration_rules_validator.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import typing
22

3-
from pydantic import model_validator
3+
from pydantic import field_validator, model_validator
44

5+
from eligibility_signposting_api.config.constants import ALLOWED_CONDITIONS
56
from eligibility_signposting_api.model.campaign_config import (
67
IterationRule,
78
RuleAttributeLevel,
@@ -11,6 +12,19 @@
1112

1213

1314
class IterationRuleValidation(IterationRule):
15+
@field_validator("attribute_target")
16+
@classmethod
17+
def validate_attribute_target(cls, value: str) -> str | None:
18+
if value is None:
19+
return value
20+
21+
allowed = ALLOWED_CONDITIONS.__args__
22+
if value not in allowed:
23+
allowed_str = ", ".join(allowed)
24+
msg = f"Invalid attribute_target value: {value}. Allowed values: {allowed_str}"
25+
raise ValueError(msg)
26+
return value
27+
1428
@model_validator(mode="after")
1529
def check_cohort_attribute_name(self) -> typing.Self:
1630
if (

tests/unit/validation/test_iteration_rules_validator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,14 @@ def test_invalid_cohort_label(self, label, valid_iteration_rule_with_only_mandat
203203
IterationRuleValidation(**data)
204204

205205
# AttributeTarget
206-
@pytest.mark.parametrize("target", ["target_value"])
206+
@pytest.mark.parametrize("target", ["RSV", "COVID"])
207207
def test_valid_attribute_target(self, target, valid_iteration_rule_with_only_mandatory_fields):
208208
data = valid_iteration_rule_with_only_mandatory_fields.copy()
209209
data["AttributeTarget"] = target
210210
result = IterationRuleValidation(**data)
211211
assert result.attribute_target == target
212212

213-
@pytest.mark.parametrize("target", [123, [], {}])
213+
@pytest.mark.parametrize("target", [123, [], {}, "RSV1"])
214214
def test_invalid_attribute_target(self, target, valid_iteration_rule_with_only_mandatory_fields):
215215
data = valid_iteration_rule_with_only_mandatory_fields.copy()
216216
data["AttributeTarget"] = target

0 commit comments

Comments
 (0)