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)
| 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.). |
| 44 | func 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. |
| 63 | func TemplateVersionNameValid(str string) error { |