FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

cron: accept a step over a range (e.g. 1-5/2) by itzzdev09 · Pull Request #482 · python-validators/validators · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
42 changes: 19 additions & 23 deletions src/validators/cron.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,36 @@
from .utils import validator


def _validate_cron_component(component: str, min_val: int, max_val: int):
if component == "*":
return True

if component.isdecimal():
return min_val <= int(component) <= max_val

if "/" in component:
parts = component.split("/")
if len(parts) != 2 or not parts[1].isdecimal() or int(parts[1]) < 1:
def _validate_cron_item(item: str, min_val: int, max_val: int):
# An item may carry a step, e.g. "*/5", "1-30/2" or "5/10".
if "/" in item:
base, _, step = item.partition("/")
if not step.isdecimal() or int(step) < 1:
return False
if parts[0] == "*":
return True
return parts[0].isdecimal() and min_val <= int(parts[0]) <= max_val
else:
base = item

if base == "*":
return True

if "-" in component:
parts = component.split("-")
if "-" in base:
parts = base.split("-")
if len(parts) != 2 or not parts[0].isdecimal() or not parts[1].isdecimal():
return False
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?
if base.isdecimal():
return min_val <= int(base) <= max_val

return False


def _validate_cron_component(component: str, min_val: int, max_val: int):
# A field is a comma-separated list of items, each optionally stepped.
return all(_validate_cron_item(item, min_val, max_val) for item in component.split(","))


@validator
def cron(value: str, /):
"""Return whether or not given value is a valid cron string.
Expand Down
4 changes: 3 additions & 1 deletion tests/test_cron.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"*/15 0,6,12,18 * * *",
"0 12 * * 0",
"*/61 * * * *",
# "5-10/2 * * * *", # this is valid, but not supported yet
"5-10/2 * * * *",
"1-30/2 * * * *",
"15,45 6-18/3 * * *",
],
)
def test_returns_true_on_valid_cron(value: str):
Expand Down

Back | FazBrowse Home | New Git URL