|
1 | 1 | import typing |
2 | 2 |
|
3 | | -from pydantic import ValidationError, field_validator, model_validator |
| 3 | +from pydantic import Field, ValidationError, field_validator, model_validator |
4 | 4 | from pydantic_core import InitErrorDetails |
5 | 5 |
|
6 | | -from eligibility_signposting_api.model.campaign_config import ActionsMapper, Iteration, IterationRule |
7 | | -from rules_validation_api.validators.actions_mapper_validator import ActionsMapperValidator |
| 6 | +from eligibility_signposting_api.model.campaign_config import ( |
| 7 | + ActionsMapper, |
| 8 | + Iteration, |
| 9 | + IterationCohort, |
| 10 | + IterationRule, |
| 11 | + RuleType, |
| 12 | +) |
| 13 | +from rules_validation_api.validators.actions_mapper_validator import ActionsMapperValidation |
| 14 | +from rules_validation_api.validators.iteration_cohort_validator import IterationCohortValidation |
8 | 15 | from rules_validation_api.validators.iteration_rules_validator import IterationRuleValidation |
9 | 16 |
|
10 | 17 |
|
11 | 18 | class IterationValidation(Iteration): |
12 | | - @classmethod |
| 19 | + iteration_cohorts: list[IterationCohort] = Field(..., alias="IterationCohorts") |
| 20 | + iteration_rules: list[IterationRule] = Field(..., alias="IterationRules") |
| 21 | + actions_mapper: ActionsMapper = Field(..., alias="ActionsMapper") |
| 22 | + |
13 | 23 | @field_validator("iteration_rules") |
14 | | - def validate_iterations(cls, iteration_rules: list[IterationRule]) -> list[IterationRuleValidation]: |
| 24 | + @classmethod |
| 25 | + def validate_iteration_rules(cls, iteration_rules: list[IterationRule]) -> list[IterationRuleValidation]: |
15 | 26 | return [IterationRuleValidation(**i.model_dump()) for i in iteration_rules] |
16 | 27 |
|
| 28 | + @field_validator("iteration_cohorts") |
17 | 29 | @classmethod |
| 30 | + def validate_iteration_cohorts(cls, iteration_cohorts: list[IterationCohort]) -> list[IterationCohortValidation]: |
| 31 | + return [IterationCohortValidation(**i.model_dump()) for i in iteration_cohorts] |
| 32 | + |
18 | 33 | @field_validator("actions_mapper", mode="after") |
| 34 | + @classmethod |
19 | 35 | def transform_actions_mapper(cls, action_mapper: ActionsMapper) -> ActionsMapper: |
20 | | - ActionsMapperValidator.model_validate(action_mapper.model_dump()) |
| 36 | + ActionsMapperValidation.model_validate(action_mapper.model_dump()) |
21 | 37 | return action_mapper |
22 | 38 |
|
23 | 39 | @model_validator(mode="after") |
| 40 | + def action_mapper_validation(self) -> typing.Self: |
| 41 | + all_errors = [] |
| 42 | + |
| 43 | + for validator in [ |
| 44 | + self.validate_default_comms_routing_in_actions_mapper, |
| 45 | + self.validate_default_not_eligible_routing_in_actions_mapper, |
| 46 | + self.validate_default_not_actionable_routing_in_actions_mapper, |
| 47 | + self.validate_iteration_rules_against_actions_mapper, |
| 48 | + ]: |
| 49 | + try: |
| 50 | + validator() |
| 51 | + except ValidationError as ve: |
| 52 | + all_errors.extend(ve.errors(include_input=False)) |
| 53 | + |
| 54 | + if all_errors: |
| 55 | + raise ValidationError.from_exception_data(title="IterationValidation", line_errors=all_errors) |
| 56 | + |
| 57 | + return self |
| 58 | + |
24 | 59 | def validate_default_comms_routing_in_actions_mapper(self) -> typing.Self: |
25 | | - default_routing = self.default_comms_routing |
26 | | - actions_mapper = self.actions_mapper.root.keys() |
27 | | - |
28 | | - if default_routing and (not actions_mapper or default_routing not in actions_mapper): |
29 | | - error = InitErrorDetails( |
30 | | - type="value_error", |
31 | | - loc=("actions_mapper",), |
32 | | - input=actions_mapper, |
33 | | - ctx={"error": f"Missing entry for DefaultCommsRouting '{default_routing}' in ActionsMapper"}, |
34 | | - ) |
35 | | - raise ValidationError.from_exception_data(title="IterationValidation", line_errors=[error]) |
| 60 | + default_routes = self.default_comms_routing |
| 61 | + actions_keys = list(self.actions_mapper.root.keys()) |
| 62 | + line_errors = [] |
| 63 | + |
| 64 | + for routing in default_routes.split("|"): |
| 65 | + cleaned_routing = routing.strip() |
| 66 | + if cleaned_routing and (not actions_keys or cleaned_routing not in actions_keys): |
| 67 | + error = InitErrorDetails( |
| 68 | + type="value_error", |
| 69 | + loc=("actions_mapper",), |
| 70 | + input=actions_keys, |
| 71 | + ctx={"error": f"Missing entry for DefaultCommsRouting '{cleaned_routing}' in ActionsMapper"}, |
| 72 | + ) |
| 73 | + line_errors.append(error) |
| 74 | + |
| 75 | + if line_errors: |
| 76 | + raise ValidationError.from_exception_data(title="IterationValidation", line_errors=line_errors) |
| 77 | + |
| 78 | + return self |
| 79 | + |
| 80 | + def validate_default_not_eligible_routing_in_actions_mapper(self) -> typing.Self: |
| 81 | + default_not_eligibile_routes = self.default_not_eligible_routing |
| 82 | + actions_keys = list(self.actions_mapper.root.keys()) |
| 83 | + line_errors = [] |
| 84 | + |
| 85 | + for routing in default_not_eligibile_routes.split("|"): |
| 86 | + cleaned_routing = routing.strip() |
| 87 | + if cleaned_routing and (not actions_keys or cleaned_routing not in actions_keys): |
| 88 | + error = InitErrorDetails( |
| 89 | + type="value_error", |
| 90 | + loc=("actions_mapper",), |
| 91 | + input=actions_keys, |
| 92 | + ctx={"error": f"Missing entry for DefaultNotEligibleRouting '{cleaned_routing}' in ActionsMapper"}, |
| 93 | + ) |
| 94 | + line_errors.append(error) |
| 95 | + |
| 96 | + if line_errors: |
| 97 | + raise ValidationError.from_exception_data(title="IterationValidation", line_errors=line_errors) |
| 98 | + |
| 99 | + return self |
| 100 | + |
| 101 | + def validate_default_not_actionable_routing_in_actions_mapper(self) -> typing.Self: |
| 102 | + default_not_actionable_routes = self.default_not_actionable_routing |
| 103 | + actions_keys = list(self.actions_mapper.root.keys()) |
| 104 | + line_errors = [] |
| 105 | + |
| 106 | + for routing in default_not_actionable_routes.split("|"): |
| 107 | + cleaned_routing = routing.strip() |
| 108 | + if cleaned_routing and (not actions_keys or cleaned_routing not in actions_keys): |
| 109 | + error = InitErrorDetails( |
| 110 | + type="value_error", |
| 111 | + loc=("actions_mapper",), |
| 112 | + input=actions_keys, |
| 113 | + ctx={ |
| 114 | + "error": f"Missing entry for DefaultNotActionableRouting '{cleaned_routing}' in ActionsMapper" |
| 115 | + }, |
| 116 | + ) |
| 117 | + line_errors.append(error) |
| 118 | + |
| 119 | + if line_errors: |
| 120 | + raise ValidationError.from_exception_data(title="IterationValidation", line_errors=line_errors) |
| 121 | + |
| 122 | + return self |
| 123 | + |
| 124 | + def validate_iteration_rules_against_actions_mapper(self) -> typing.Self: |
| 125 | + actions_keys = list(self.actions_mapper.root.keys()) |
| 126 | + line_errors = [] |
| 127 | + |
| 128 | + for rule in self.iteration_rules: |
| 129 | + if ( |
| 130 | + rule.type |
| 131 | + in [ |
| 132 | + RuleType.redirect, |
| 133 | + RuleType.not_actionable_actions, |
| 134 | + RuleType.not_eligible_actions, |
| 135 | + ] |
| 136 | + and rule.comms_routing |
| 137 | + ): |
| 138 | + for routing in rule.comms_routing.split("|"): |
| 139 | + cleaned_routing = routing.strip() |
| 140 | + if cleaned_routing and (not actions_keys or cleaned_routing not in actions_keys): |
| 141 | + error = InitErrorDetails( |
| 142 | + type="value_error", |
| 143 | + loc=("iteration_rules",), |
| 144 | + input=actions_keys, |
| 145 | + ctx={"error": f"Missing entry for CommsRouting '{cleaned_routing}' in ActionsMapper"}, |
| 146 | + ) |
| 147 | + line_errors.append(error) |
| 148 | + |
| 149 | + if line_errors: |
| 150 | + raise ValidationError.from_exception_data(title="IterationValidation", line_errors=line_errors) |
36 | 151 |
|
37 | 152 | return self |
0 commit comments