Skip to content

Commit 4696bf9

Browse files
committed
Export valid name regex
1 parent 49084e2 commit 4696bf9

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
lines changed

coderd/httpapi/username.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@ import (
88
)
99

1010
var (
11-
usernameValid = regexp.MustCompile("^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$")
12-
usernameReplace = regexp.MustCompile("[^a-zA-Z0-9-]*")
11+
UsernameValidRegex = regexp.MustCompile("^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$")
12+
usernameReplace = regexp.MustCompile("[^a-zA-Z0-9-]*")
1313
)
1414

15-
func UsernameValidRegexString() string {
16-
return usernameValid.String()
17-
}
18-
1915
// UsernameValid returns whether the input string is a valid username.
2016
func UsernameValid(str string) bool {
2117
if len(str) > 32 {
@@ -24,7 +20,7 @@ func UsernameValid(str string) bool {
2420
if len(str) < 1 {
2521
return false
2622
}
27-
return usernameValid.MatchString(str)
23+
return UsernameValidRegex.MatchString(str)
2824
}
2925

3026
// UsernameFrom returns a best-effort username from the provided string.

coderd/subdomain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (api *API) handleSubdomain(middlewares ...func(http.Handler) http.Handler)
7373

7474
var (
7575
// Remove the "starts with" and "ends with" regex components.
76-
nameRegex = strings.Trim(httpapi.UsernameValidRegexString(), "^$")
76+
nameRegex = strings.Trim(httpapi.UsernameValidRegex.String(), "^$")
7777
appURL = regexp.MustCompile(fmt.Sprintf(
7878
// {USERNAME}--{WORKSPACE_NAME}}--{{AGENT_NAME}}--{{PORT}}
7979
`^(?P<UserName>%[1]s)--(?P<WorkspaceName>%[1]s)(--(?P<AgentName>%[1]s))?--(?P<AppName>%[1]s)$`,

0 commit comments

Comments
 (0)