Skip to content

Commit

Permalink
chore: add validate for replaces
Browse files Browse the repository at this point in the history
  • Loading branch information
l-qing committed Feb 6, 2023
1 parent e13573c commit 4c3f568
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
35 changes: 34 additions & 1 deletion regex/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ limitations under the License.
*/
package regex

import "regexp"
import (
"regexp"

"k8s.io/apimachinery/pkg/util/validation/field"
)

// Replace provides helper functions for replacing strings
type Replace struct {
Expand Down Expand Up @@ -47,3 +51,32 @@ func (rs *Replaces) ReplaceAllString(s string) string {
}
return s
}

// Validate Replace validation method
func (r *Replace) Validate(fld *field.Path) field.ErrorList {
errs := field.ErrorList{}

if r.Regex == "" {
return nil
}
_, err := regexp.Compile(r.Regex)
if err != nil {
errs = append(errs, field.Invalid(fld.Child("regex"), r.Regex, err.Error()))
}

return errs
}

// Validate Replaces validation method
func (r *Replaces) Validate(fld *field.Path) field.ErrorList {
errs := field.ErrorList{}

if r == nil {
return nil
}
for i, replace := range *r {
errs = append(errs, replace.Validate(fld.Index(i))...)
}

return errs
}
70 changes: 70 additions & 0 deletions regex/replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ package regex
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"

kvalidation "github.com/katanomi/pkg/apis/validation"
. "github.com/katanomi/pkg/testing"
)

var _ = Describe("Test.Replaces.ReplaceAllString", func() {
Expand Down Expand Up @@ -51,3 +58,66 @@ var _ = Describe("Test.Replaces.ReplaceAllString", func() {
Entry("length greater than 30", nameAsTagReplaces, "0123456789abcdefghijklmnopqrstuvwxyz/-_+ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
)
})

var _ = Describe("Test.Replaces.Validate", func() {

var (
rs Replaces
path *field.Path
errs field.ErrorList
err error
)

BeforeEach(func() {
path = field.NewPath("prefix")
rs = Replaces{}
})

JustBeforeEach(func() {
errs = rs.Validate(path)
err = kvalidation.ReturnInvalidError(schema.GroupKind{}, "kind", errs)
})

Context("empty struct", func() {
It("should NOT return validation error", func() {
Expect(err).To(BeNil(), "should NOT return an error")
})
})

Context("Lots of validation errors", func() {
BeforeEach(func() {
MustLoadYaml("testdata/replaces_validation.InvalidData.original.yaml", &rs)
})

It("should return validation error", func() {
Expect(err).ToNot(BeNil(), "should return an error")
Expect(errors.IsInvalid(err)).To(BeTrue(), "should return an invalid error")

statusErr, _ := err.(*errors.StatusError)
Expect(statusErr.ErrStatus.Details.Causes).To(ContainElements(
metav1.StatusCause{
Type: "FieldValueInvalid",
Message: "Invalid value: \"[abcd$\": error parsing regexp: missing closing ]: `[abcd$`",
Field: "prefix[0].regex",
},
metav1.StatusCause{
Type: "FieldValueInvalid",
Message: "Invalid value: \"^.0,128[.*{$\": error parsing regexp: missing closing ]: `[.*{$`",
Field: "prefix[1].regex",
},
))
Expect(statusErr.ErrStatus.Details.Causes).To(HaveLen(2))
})
})

Context("Valid", func() {
BeforeEach(func() {
MustLoadYaml("testdata/replaces_validation.Valid.original.yaml", &rs)
})

It("should not return validation error", func() {
Expect(err).To(BeNil(), "should NOT return an error")
})
})

})
3 changes: 3 additions & 0 deletions regex/testdata/replaces_validation.InvalidData.original.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- regex: "[abcd$"

- regex: "^.0,128[.*{$"
8 changes: 8 additions & 0 deletions regex/testdata/replaces_validation.Valid.original.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- regex: \+.*$

- regex: ^(.{0,128})(.*)$
replacement: ${1}

- regex: ^(.{0,128})(.*)$
replacement: ${1}

0 comments on commit 4c3f568

Please sign in to comment.