@@ -99,34 +99,45 @@ def transform_actions_mapper(cls, action_mapper: ActionsMapper) -> ActionsMapper
9999
100100 @model_validator (mode = "after" )
101101 def validate_rule_cohort_labels_against_iteration_cohorts (self ) -> typing .Self :
102- allowed_labels = {cohort .cohort_label for cohort in self .iteration_cohorts }
102+ allowed_labels = {c .cohort_label for c in self .iteration_cohorts }
103103 line_errors : list [InitErrorDetails ] = []
104104
105+ # Pre‑compute allowed label string once
106+ allowed_str = ", " .join (sorted (allowed_labels )) if allowed_labels else None
107+
105108 for idx , rule in enumerate (self .iteration_rules ):
106- if rule .cohort_label is None :
109+ if not rule .cohort_label :
107110 continue
108111
109112 for label in rule .parsed_cohort_labels :
110- if label not in allowed_labels :
111- if allowed_labels :
112- allowed_str = ", " .join (sorted (allowed_labels ))
113- error_message = (
114- f"Invalid cohort_label value '{ label } '. Allowed values: { allowed_str } ."
115- )
116- else :
117- error_message = (
118- f"Invalid cohort_label value '{ label } '. "
119- "No iteration cohorts are defined, so no cohort labels are allowed."
120- )
121- error = InitErrorDetails (
113+ if label in allowed_labels :
114+ continue
115+
116+ # Build error message
117+ error_message = (
118+ f"Invalid cohort_label value '{ label } '. "
119+ f"Allowed values: { allowed_str } ."
120+ if allowed_str
121+ else (
122+ f"Invalid cohort_label value '{ label } '. "
123+ "No iteration cohorts are defined, so no labels are allowed."
124+ )
125+ )
126+
127+ line_errors .append (
128+ InitErrorDetails (
122129 type = "value_error" ,
123130 loc = ("iteration_rules" , idx , "cohort_label" ),
124131 input = rule .cohort_label ,
125132 ctx = {"error" : error_message },
126133 )
127- line_errors .append (error )
134+ )
135+
128136 if line_errors :
129- raise ValidationError .from_exception_data (title = "IterationValidation" , line_errors = line_errors )
137+ raise ValidationError .from_exception_data (
138+ title = "IterationValidation" ,
139+ line_errors = line_errors
140+ )
130141
131142 return self
132143
0 commit comments