Skip to content

Commit cc6079d

Browse files
committed
refactor: use getattr to allow for specialized validators
Signed-off-by: heitorlessa <[email protected]>
1 parent 6087741 commit cc6079d

File tree

1 file changed

+15
-4
lines changed
  • aws_lambda_powertools/utilities/feature_flags

1 file changed

+15
-4
lines changed

aws_lambda_powertools/utilities/feature_flags/schema.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,16 @@ def validate_condition_value(condition: Dict[str, Any], rule_name: str):
350350
raise SchemaValidationError(f"'value' key must not be null, rule={rule_name}")
351351
action = condition.get(CONDITION_ACTION, "")
352352

353+
# maintenance: we may need a new design if we end up with more exceptions like datetime/time range
354+
# e.g., visitor pattern, registry etc.
355+
validator = getattr(
356+
ConditionsValidator,
357+
f"_validate_{action.lower()}_value",
358+
ConditionsValidator._validate_noop_value,
359+
)
360+
361+
validator(value, rule_name)
362+
353363
# time actions need to be parsed to make sure date and time format is valid and timezone is recognized
354364
if action == RuleAction.SCHEDULE_BETWEEN_TIME_RANGE.value:
355365
ConditionsValidator._validate_schedule_between_time_and_datetime_ranges(
@@ -365,12 +375,13 @@ def validate_condition_value(condition: Dict[str, Any], rule_name: str):
365375
action,
366376
ConditionsValidator._validate_datetime_value,
367377
)
368-
elif action == RuleAction.SCHEDULE_BETWEEN_DAYS_OF_WEEK.value:
369-
ConditionsValidator._validate_schedule_between_days_of_week(value, rule_name)
370-
# modulo range condition needs validation on base, start, and end attributes
371378
elif action == RuleAction.MODULO_RANGE.value:
372379
ConditionsValidator._validate_modulo_range(value, rule_name)
373380

381+
@staticmethod
382+
def _validate_noop_value(*args, **kwargs):
383+
return True
384+
374385
@staticmethod
375386
def _validate_datetime_value(datetime_str: str, rule_name: str):
376387
date = None
@@ -408,7 +419,7 @@ def _validate_time_value(time: str, rule_name: str):
408419
)
409420

410421
@staticmethod
411-
def _validate_schedule_between_days_of_week(value: Any, rule_name: str):
422+
def _validate_schedule_between_days_of_week_value(value: Any, rule_name: str):
412423
error_str = f"condition with a CURRENT_DAY_OF_WEEK action must have a condition value dictionary with 'DAYS' and 'TIMEZONE' (optional) keys, rule={rule_name}" # noqa: E501
413424
if not isinstance(value, dict):
414425
raise SchemaValidationError(error_str)

0 commit comments

Comments
 (0)