Skip to content

Commit

Permalink
Fixed Cron.ValidUntil using incorrect ISO8601 parsing format
Browse files Browse the repository at this point in the history
Signed-off-by: Venera <31911811+venera-program@users.noreply.github.com>
  • Loading branch information
venera-program committed Sep 8, 2023
1 parent 4afc5f3 commit d414306
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.19
require (
github.com/go-playground/validator/v10 v10.11.1
github.com/pkg/errors v0.9.1
github.com/relvacode/iso8601 v1.3.0
github.com/senseyeio/duration v0.0.0-20180430131211-7c2a214ada46
github.com/stretchr/testify v1.8.0
gopkg.in/yaml.v3 v3.0.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/relvacode/iso8601 v1.3.0 h1:HguUjsGpIMh/zsTczGN3DVJFxTU/GX+MMmzcKoMO7ko=
github.com/relvacode/iso8601 v1.3.0/go.mod h1:FlNp+jz+TXpyRqgmM7tnzHHzBnz776kmAH2h3sZCn0I=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
Expand Down
2 changes: 1 addition & 1 deletion model/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ type Cron struct {
Expression string `json:"expression" validate:"required"`
// Specific date and time (ISO 8601 format) when the cron expression is no longer valid.
// +optional
ValidUntil string `json:"validUntil,omitempty" validate:"omitempty,iso8601duration"`
ValidUntil string `json:"validUntil,omitempty" validate:"omitempty,iso8601datetime"`
}

type cronUnmarshal Cron
Expand Down
17 changes: 17 additions & 0 deletions validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"strconv"

"github.com/relvacode/iso8601"
"github.com/senseyeio/duration"
"k8s.io/apimachinery/pkg/util/intstr"

Expand All @@ -41,6 +42,11 @@ func init() {
panic(err)
}

err = validate.RegisterValidationCtx("iso8601datetime", validateISO8601DatetimeFunc)
if err != nil {
panic(err)
}

err = validate.RegisterValidation("oneofkind", oneOfKind)
if err != nil {
panic(err)
Expand All @@ -63,6 +69,17 @@ func validateISO8601TimeDurationFunc(_ context.Context, fl validator.FieldLevel)
return err == nil
}

// ValidateISO8601Datetime validate the string is iso8601 Datetime format
func ValidateISO8601Datetime(s string) error {
_, err := iso8601.ParseString(s)
return err
}

func validateISO8601DatetimeFunc(_ context.Context, fl validator.FieldLevel) bool {
err := ValidateISO8601Datetime(fl.Field().String())
return err == nil
}

func oneOfKind(fl validator.FieldLevel) bool {
if val, ok := fl.Field().Interface().(Kind); ok {
for _, value := range val.KindValues() {
Expand Down
48 changes: 48 additions & 0 deletions validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,54 @@ func TestValidateISO8601TimeDuration(t *testing.T) {
}
}

func TestValidateISO8601Timestamp(t *testing.T) {
type testCase struct {
desp string
s string
err string
}
testCases := []testCase{
{
desp: "workflow_spec_example",
s: "2021-11-05T08:15:30-05:00",
err: ``,
},
{
desp: "datetime",
s: "2023-09-08T20:15:46+00:00",
err: ``,
},
{
desp: "date",
s: "2023-09-08",
err: ``,
},
{
desp: "time",
s: "13:15:33.074-07:00",
err: "iso8601: Unexpected character `:`",
},
{
desp: "empty value",
s: "",
err: `iso8601: Cannot parse "": month 0 is not in range 1-12`,
},
}
for _, tc := range testCases {
t.Run(tc.desp, func(t *testing.T) {
err := ValidateISO8601Datetime(tc.s)

if tc.err != "" {
assert.Error(t, err)
assert.Regexp(t, tc.err, err)
return
}

assert.NoError(t, err)
})
}
}

type testKind string

func (k testKind) KindValues() []string {
Expand Down

0 comments on commit d414306

Please sign in to comment.