Skip to content

Commit a506a18

Browse files
refactor: improve environment name validation logic
1 parent 48624c9 commit a506a18

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

api/cluster/EnvironmentRestHandler.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ import (
4949

5050
const ENV_DELETE_SUCCESS_RESP = "Environment deleted successfully."
5151

52+
var (
53+
// Regex patterns for environment name validation
54+
envNameAlphanumericRegex = regexp.MustCompile(`^[a-z0-9-]+$`)
55+
envNameLengthRegex = regexp.MustCompile(`^.{1,16}$`)
56+
)
57+
5258
type EnvironmentRestHandler interface {
5359
Create(w http.ResponseWriter, r *http.Request)
5460
Get(w http.ResponseWriter, r *http.Request)
@@ -107,13 +113,6 @@ func NewEnvironmentRestHandlerImpl(svc request.EnvironmentService, environmentRe
107113
}
108114
}
109115

110-
var (
111-
// Regex patterns for environment name validation
112-
envNameAlphanumericRegex = regexp.MustCompile(`^[a-z0-9-]+$`)
113-
envNameNoStartEndHyphen = regexp.MustCompile(`^(?![-]).*[^-]$`)
114-
envNameLengthRegex = regexp.MustCompile(`^.{1,16}$`)
115-
)
116-
117116
// validateEnvironmentName validates the environment name against multiple regex patterns
118117
// Note: Required validation is already handled by struct validation tag
119118
func (impl EnvironmentRestHandlerImpl) validateEnvironmentName(envName string) error {
@@ -123,7 +122,7 @@ func (impl EnvironmentRestHandlerImpl) validateEnvironmentName(envName string) e
123122
}
124123

125124
// Validation 2: Cannot start/end with '-'
126-
if !envNameNoStartEndHyphen.MatchString(envName) {
125+
if strings.HasPrefix(envName, "-") || strings.HasSuffix(envName, "-") {
127126
return errors.New("Cannot start/end with '-'")
128127
}
129128

0 commit comments

Comments
 (0)