Skip to content

Commit 5a23a17

Browse files
Fixed Cron.ValidUntil using incorrect ISO8601 parsing format (#187)
Signed-off-by: Venera <31911811+venera-program@users.noreply.github.com>
1 parent 6d5de06 commit 5a23a17

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.19
55
require (
66
github.com/go-playground/validator/v10 v10.11.1
77
github.com/pkg/errors v0.9.1
8+
github.com/relvacode/iso8601 v1.3.0
89
github.com/senseyeio/duration v0.0.0-20180430131211-7c2a214ada46
910
github.com/stretchr/testify v1.8.0
1011
gopkg.in/yaml.v3 v3.0.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
4545
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
4646
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4747
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
48+
github.com/relvacode/iso8601 v1.3.0 h1:HguUjsGpIMh/zsTczGN3DVJFxTU/GX+MMmzcKoMO7ko=
49+
github.com/relvacode/iso8601 v1.3.0/go.mod h1:FlNp+jz+TXpyRqgmM7tnzHHzBnz776kmAH2h3sZCn0I=
4850
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
4951
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
5052
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=

model/workflow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ type Cron struct {
372372
Expression string `json:"expression" validate:"required"`
373373
// Specific date and time (ISO 8601 format) when the cron expression is no longer valid.
374374
// +optional
375-
ValidUntil string `json:"validUntil,omitempty" validate:"omitempty,iso8601duration"`
375+
ValidUntil string `json:"validUntil,omitempty" validate:"omitempty,iso8601datetime"`
376376
}
377377

378378
type cronUnmarshal Cron

validator/validator.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"strconv"
2020

21+
"github.com/relvacode/iso8601"
2122
"github.com/senseyeio/duration"
2223
"k8s.io/apimachinery/pkg/util/intstr"
2324

@@ -41,6 +42,11 @@ func init() {
4142
panic(err)
4243
}
4344

45+
err = validate.RegisterValidationCtx("iso8601datetime", validateISO8601DatetimeFunc)
46+
if err != nil {
47+
panic(err)
48+
}
49+
4450
err = validate.RegisterValidation("oneofkind", oneOfKind)
4551
if err != nil {
4652
panic(err)
@@ -63,6 +69,17 @@ func validateISO8601TimeDurationFunc(_ context.Context, fl validator.FieldLevel)
6369
return err == nil
6470
}
6571

72+
// ValidateISO8601Datetime validate the string is iso8601 Datetime format
73+
func ValidateISO8601Datetime(s string) error {
74+
_, err := iso8601.ParseString(s)
75+
return err
76+
}
77+
78+
func validateISO8601DatetimeFunc(_ context.Context, fl validator.FieldLevel) bool {
79+
err := ValidateISO8601Datetime(fl.Field().String())
80+
return err == nil
81+
}
82+
6683
func oneOfKind(fl validator.FieldLevel) bool {
6784
if val, ok := fl.Field().Interface().(Kind); ok {
6885
for _, value := range val.KindValues() {

validator/validator_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,54 @@ func TestValidateISO8601TimeDuration(t *testing.T) {
5959
}
6060
}
6161

62+
func TestValidateISO8601Timestamp(t *testing.T) {
63+
type testCase struct {
64+
desp string
65+
s string
66+
err string
67+
}
68+
testCases := []testCase{
69+
{
70+
desp: "workflow_spec_example",
71+
s: "2021-11-05T08:15:30-05:00",
72+
err: ``,
73+
},
74+
{
75+
desp: "datetime",
76+
s: "2023-09-08T20:15:46+00:00",
77+
err: ``,
78+
},
79+
{
80+
desp: "date",
81+
s: "2023-09-08",
82+
err: ``,
83+
},
84+
{
85+
desp: "time",
86+
s: "13:15:33.074-07:00",
87+
err: "iso8601: Unexpected character `:`",
88+
},
89+
{
90+
desp: "empty value",
91+
s: "",
92+
err: `iso8601: Cannot parse "": month 0 is not in range 1-12`,
93+
},
94+
}
95+
for _, tc := range testCases {
96+
t.Run(tc.desp, func(t *testing.T) {
97+
err := ValidateISO8601Datetime(tc.s)
98+
99+
if tc.err != "" {
100+
assert.Error(t, err)
101+
assert.Regexp(t, tc.err, err)
102+
return
103+
}
104+
105+
assert.NoError(t, err)
106+
})
107+
}
108+
}
109+
62110
type testKind string
63111

64112
func (k testKind) KindValues() []string {

0 commit comments

Comments
 (0)