Skip to content

fix(cron): anchor regex and accept full cron syntax#1577

Open
ahmedkamalio wants to merge 1 commit into
go-playground:masterfrom
ahmedkamalio:fix/cron-regex-anchors
Open

fix(cron): anchor regex and accept full cron syntax#1577
ahmedkamalio wants to merge 1 commit into
go-playground:masterfrom
ahmedkamalio:fix/cron-regex-anchors

Conversation

@ahmedkamalio
Copy link
Copy Markdown
Contributor

The cron validator's regex lacked ^...$ anchors, so any string containing a cron-like substring (e.g. "random text @daily more text", "prefix @every 1h suffix", "x 1 2 3 4 5 y") was accepted as a valid cron expression.

Anchoring alone wasn't sufficient — the original field pattern only recognized plain numeric forms and was getting away with it via substring matching. Several existing positive test cases (?, L, MON-FRI, #, multi-field expressions with year) only passed because the regex matched a numeric fragment of the input. The field pattern is now expanded to accept the documented cron alphabet (*, ?, digits, letters, ,, /, -, #, L, W) while still rejecting bare single letters like "x" that aren't valid in any cron dialect.

Negative test cases added to lock in the new behavior, with a note documenting remaining false positives that would require a real cron parser to reject (out-of-range values, nonsense alphabetic tokens, etc).

Fixes Or Enhances

Make sure that you've checked the boxes below before you submit PR:

  • Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

@ahmedkamalio ahmedkamalio requested a review from a team as a code owner May 15, 2026 22:16
@ahmedkamalio
Copy link
Copy Markdown
Contributor Author

Failed lint is unrelated to the code touched in this PR

Copy link
Copy Markdown
Member

@zemzale zemzale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just rebase the PR please, the lint issue has been fixed

The cron validator's regex lacked `^...$` anchors, so any string
containing a cron-like substring (e.g. "random text @daily more text",
"prefix @every 1h suffix", "x 1 2 3 4 5 y") was accepted as a valid
cron expression.

Anchoring alone wasn't sufficient — the original field pattern only
recognised plain numeric forms and was getting away with it via
substring matching. Several existing positive test cases (`?`, `L`,
`MON-FRI`, `#`, multi-field expressions with year) only passed because
the regex matched a numeric fragment of the input. The field pattern
is now expanded to accept the documented cron alphabet
(`*`, `?`, digits, letters, `,`, `/`, `-`, `#`, `L`, `W`) while still
rejecting bare single letters like "x" that aren't valid in any cron
dialect.

Negative test cases added to lock in the new behaviour, with a note
documenting remaining false positives that would require a real cron
parser to reject (out-of-range values, nonsense alphabetic tokens, etc).

Signed-off-by: Ahmed Kamal <ahmed@ahmedkamal.io>
@ahmedkamalio ahmedkamalio force-pushed the fix/cron-regex-anchors branch from 67ec40a to c31a7da Compare May 18, 2026 14:12
@ahmedkamalio ahmedkamalio requested a review from zemzale May 18, 2026 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: cron validator accepts arbitrary strings containing a cron-like substring

2 participants