Skip to content

Commit

Permalink
regex set as global variable, test cases added
Browse files Browse the repository at this point in the history
Signed-off-by: Mattia Lavacca <[email protected]>
  • Loading branch information
mlavacca committed Sep 27, 2022
1 parent b8734a1 commit 8aab82d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
8 changes: 4 additions & 4 deletions apis/v1alpha2/util/validation/gatewayclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import (
gatewayv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
)

// IsControllerNameValid checks that the provided controllerName complaints with the expected
var controllerNameRegex = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*\/[A-Za-z0-9\/\-._~%!$&'()*+,;=:]+$`)

// IsControllerNameValid checks that the provided controllerName complies with the expected
// format. It must be a non-empty domain prefixed path.
func IsControllerNameValid(controllerName gatewayv1a2.GatewayController) bool {
if controllerName == "" {
return false
}

re := regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*\/[A-Za-z0-9\/\-._~%!$&'()*+,;=:]+$`)
return re.Match([]byte(controllerName))
return controllerNameRegex.Match([]byte(controllerName))
}
12 changes: 11 additions & 1 deletion apis/v1alpha2/util/validation/gatewayclass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,20 @@ func TestIsControllerNameValid(t *testing.T) {
isvalid: false,
},
{
name: "invalid controller name",
name: "invalid controller name 1",
controllerName: "example.com",
isvalid: false,
},
{
name: "invalid controller name 2",
controllerName: "example*com",
isvalid: false,
},
{
name: "invalid controller name 3",
controllerName: "example/@bar",
isvalid: false,
},
{
name: "valid controller name",
controllerName: "example.com/bar",
Expand Down
7 changes: 4 additions & 3 deletions apis/v1beta1/util/validation/gatewayclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (
gatewayv1b1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)

// IsControllerNameValid checks that the provided controllerName complaints with the expected
var controllerNameRegex = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*\/[A-Za-z0-9\/\-._~%!$&'()*+,;=:]+$`)

// IsControllerNameValid checks that the provided controllerName complies with the expected
// format. It must be a non-empty domain prefixed path.
func IsControllerNameValid(controllerName gatewayv1b1.GatewayController) bool {
if controllerName == "" {
return false
}
re := regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*\/[A-Za-z0-9\/\-._~%!$&'()*+,;=:]+$`)
return re.Match([]byte(controllerName))
return controllerNameRegex.Match([]byte(controllerName))
}
12 changes: 11 additions & 1 deletion apis/v1beta1/util/validation/gatewayclass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,20 @@ func TestIsControllerNameValid(t *testing.T) {
isvalid: false,
},
{
name: "invalid controller name",
name: "invalid controller name 1",
controllerName: "example.com",
isvalid: false,
},
{
name: "invalid controller name 2",
controllerName: "example*com",
isvalid: false,
},
{
name: "invalid controller name 3",
controllerName: "example/@bar",
isvalid: false,
},
{
name: "valid controller name",
controllerName: "example.com/bar",
Expand Down

0 comments on commit 8aab82d

Please sign in to comment.