Skip to content

fixed validation of cron expression#1531

Open
thejus-r wants to merge 1 commit intogo-playground:masterfrom
thejus-r:fix-cron-validation
Open

fixed validation of cron expression#1531
thejus-r wants to merge 1 commit intogo-playground:masterfrom
thejus-r:fix-cron-validation

Conversation

@thejus-r
Copy link

Fixes Or Enhances

// regexes.go
cronRegexString = `^(@(annually|yearly|monthly|weekly|daily|hourly|reboot)|@every (\d+(ns|us|µs|ms|s|m|h))+|(((\d+(-\d+)?|\*|\?|\d*L(?:W)?|\d*W|\d+#\d+|[A-Z]{3}(-[A-Z]{3})?)(\/\d+)?)(,(\d+(-\d+)?|\*|\?|\d*L(?:W)?|\d*W|\d+#\d+|[A-Z]{3}(-[A-Z]{3})?)(\/\d+)?)* ?){5,7})$`

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

@thejus-r thejus-r requested a review from a team as a code owner February 10, 2026 14:17
@coveralls
Copy link

Coverage Status

coverage: 73.832%. remained the same
when pulling 4c289a8 on thejus-r:fix-cron-validation
into dede341 on go-playground:master.

Copy link
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.

IMO this is better than what we have right now since it fixes the issues.

But this is still not perfect since as it would allow:

* 9-25 * * * 

Maybe update the test with something like this to have it documented?

	t.Skip("FIXME: validation on minute/hour/day/month/weekday range is not working")

	failingTests := []struct {
		value    string `validate:"cron"`
		tag      string
		expected bool
	}{
		{"60 * * * *", "cron", false},
		{"* 25 * * *", "cron", false},
		{"* * 32 * *", "cron", false},
		{"* * * 13 *", "cron", false},
		{"* * * * 8", "cron", false},
	}

	for i, test := range failingTests {
		errs := validate.Var(test.value, test.tag)
		if test.expected {
			if !IsEqual(errs, nil) {
				t.Fatalf(`Index: %d cron "%s" failed Error: %s`, i, test.value, errs)
			}
		} else {
			if IsEqual(errs, nil) {
				t.Fatalf(`Index: %d cron "%s" should have errs`, i, test.value)
			}
		}
	}

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

3 participants