diff --git a/src/validators/cron.py b/src/validators/cron.py index a8449b6a..c2b02482 100644 --- a/src/validators/cron.py +++ b/src/validators/cron.py @@ -8,6 +8,16 @@ def _validate_cron_component(component: str, min_val: int, max_val: int): if component == "*": return True + # A comma-separated list must be split first, so each element can itself be + # a range (e.g. "1-5"), a step (e.g. "*/2") or a single value. Evaluating + # the "-" or "/" branches before this one caused valid expressions such as + # "1-5,10-20" to be rejected. + if "," in component: + for item in component.split(","): + if not _validate_cron_component(item, min_val, max_val): + return False + return True + if component.isdecimal(): return min_val <= int(component) <= max_val @@ -26,15 +36,6 @@ def _validate_cron_component(component: str, min_val: int, max_val: int): start, end = int(parts[0]), int(parts[1]) return min_val <= start <= max_val and min_val <= end <= max_val and start <= end - if "," in component: - for item in component.split(","): - if not _validate_cron_component(item, min_val, max_val): - return False - return True - # return all( - # _validate_cron_component(item, min_val, max_val) for item in component.split(",") - # ) # throws type error. why? - return False diff --git a/tests/test_cron.py b/tests/test_cron.py index e0b2a199..b78edde8 100644 --- a/tests/test_cron.py +++ b/tests/test_cron.py @@ -23,6 +23,10 @@ "*/15 0,6,12,18 * * *", "0 12 * * 0", "*/61 * * * *", + "1-5,10-20 * * * *", + "1-5,30 * * * *", + "10,20-30 * * * *", + "1-5,10-20,30-40 * * * *", # "5-10/2 * * * *", # this is valid, but not supported yet ], )