Skip to content

Commit a9d635a

Browse files
authored
fix: validate cron expression without regex (#1669)
1 parent d4ba9c7 commit a9d635a

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

pkg/validator/validator.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ import (
99
"github.com/Masterminds/semver/v3"
1010
"github.com/go-playground/validator/v10"
1111
"github.com/google/uuid"
12+
"github.com/robfig/cron/v3"
1213

1314
"github.com/hatchet-dev/hatchet/internal/cel"
1415
"github.com/hatchet-dev/hatchet/pkg/client/types"
1516
)
1617

1718
var NameRegex = regexp.MustCompile("^[a-zA-Z0-9\\.\\-_]+$") //nolint:gosimple
1819

19-
var CronRegex = regexp.MustCompile(`(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|µs|ms|s|m|h))+)|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})`) //nolint:gosimple
20-
2120
func newValidator() *validator.Validate {
2221
validate := validator.New()
2322

2423
celParser := cel.NewCELParser()
24+
cronParser := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
2525

2626
_ = validate.RegisterValidation("hatchetName", func(fl validator.FieldLevel) bool {
2727
return NameRegex.MatchString(fl.Field().String())
@@ -36,7 +36,9 @@ func newValidator() *validator.Validate {
3636
})
3737

3838
_ = validate.RegisterValidation("cron", func(fl validator.FieldLevel) bool {
39-
return CronRegex.MatchString(fl.Field().String())
39+
_, err := cronParser.Parse(fl.Field().String())
40+
41+
return err == nil
4042
})
4143

4244
_ = validate.RegisterValidation("actionId", func(fl validator.FieldLevel) bool {

0 commit comments

Comments
 (0)