MCPcopy Index your code
hub / github.com/coder/coder / NameValid

Function NameValid

codersdk/name.go:44–60  ·  view source on GitHub ↗

NameValid returns whether the input string is a valid name. It is a generic validator for any name (user, workspace, template, role name, etc.).

(str string)

Source from the content-addressed store, hash-verified

42// NameValid returns whether the input string is a valid name.
43// It is a generic validator for any name (user, workspace, template, role name, etc.).
44func NameValid(str string) error {
45 if len(str) > 32 {
46 return xerrors.New("must be <= 32 characters")
47 }
48 if len(str) < 1 {
49 return xerrors.New("must be >= 1 character")
50 }
51 // Avoid conflicts with routes like /templates/new and /groups/create.
52 if str == "new" || str == "create" {
53 return xerrors.Errorf("cannot use %q as a name", str)
54 }
55 matched := UsernameValidRegex.MatchString(str)
56 if !matched {
57 return xerrors.New("must be alphanumeric with hyphens")
58 }
59 return nil
60}
61
62// TemplateVersionNameValid returns whether the input string is a valid template version name.
63func TemplateVersionNameValid(str string) error {

Callers 15

CreateUserMethod · 0.92
tasksCreateMethod · 0.92
userOIDCMethod · 0.92
TestTasksCreateFunction · 0.92
initFunction · 0.92
CreateWorkspaceFunction · 0.92
generatedWorkspaceNameFunction · 0.92
ConvertConfigFunction · 0.92
generateFromAnthropicFunction · 0.92
TestGenerateFunction · 0.92
TestGenerateFallbackFunction · 0.92
TestGenerateFromPromptFunction · 0.92

Calls 2

NewMethod · 0.65
ErrorfMethod · 0.45

Tested by 8

TestTasksCreateFunction · 0.74
TestGenerateFunction · 0.74
TestGenerateFallbackFunction · 0.74
TestGenerateFromPromptFunction · 0.74
TestUsernameValidFunction · 0.74
TestFromFunction · 0.74