Skip to content

Commit 3fc9b39

Browse files
committed
fix(cron): validate comma-separated lists of ranges and steps
The comma-list branch in _validate_cron_component was evaluated after the '-' and '/' branches, so any list element that was itself a range (e.g. '1-5') or step (e.g. '*/2') short-circuited and the whole expression was rejected. Move the comma-list split to run first, so each element is validated as a range/step/single value. Adds regression cases for list-of-ranges, list-with-single, and multi-list expressions.
1 parent 70de324 commit 3fc9b39

2 files changed

Lines changed: 14 additions & 9 deletions

File tree

src/validators/cron.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ def _validate_cron_component(component: str, min_val: int, max_val: int):
88
if component == "*":
99
return True
1010

11+
# A comma-separated list must be split first, so each element can itself be
12+
# a range (e.g. "1-5"), a step (e.g. "*/2") or a single value. Evaluating
13+
# the "-" or "/" branches before this one caused valid expressions such as
14+
# "1-5,10-20" to be rejected.
15+
if "," in component:
16+
for item in component.split(","):
17+
if not _validate_cron_component(item, min_val, max_val):
18+
return False
19+
return True
20+
1121
if component.isdecimal():
1222
return min_val <= int(component) <= max_val
1323

@@ -26,15 +36,6 @@ def _validate_cron_component(component: str, min_val: int, max_val: int):
2636
start, end = int(parts[0]), int(parts[1])
2737
return min_val <= start <= max_val and min_val <= end <= max_val and start <= end
2838

29-
if "," in component:
30-
for item in component.split(","):
31-
if not _validate_cron_component(item, min_val, max_val):
32-
return False
33-
return True
34-
# return all(
35-
# _validate_cron_component(item, min_val, max_val) for item in component.split(",")
36-
# ) # throws type error. why?
37-
3839
return False
3940

4041

tests/test_cron.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
"*/15 0,6,12,18 * * *",
2424
"0 12 * * 0",
2525
"*/61 * * * *",
26+
"1-5,10-20 * * * *",
27+
"1-5,30 * * * *",
28+
"10,20-30 * * * *",
29+
"1-5,10-20,30-40 * * * *",
2630
# "5-10/2 * * * *", # this is valid, but not supported yet
2731
],
2832
)

0 commit comments

Comments
 (0)