package handlers import ( "errors" "regexp" "strconv" ) const ( // appNameMinLength is the minimum allowed length for an app name. appNameMinLength = 2 // appNameMaxLength is the maximum allowed length for an app name. appNameMaxLength = 63 ) // validAppNameRe matches names containing only lowercase alphanumeric characters and // hyphens, starting and ending with an alphanumeric character. var validAppNameRe = regexp.MustCompile(`^[a-z0-9][a-z0-9-]*[a-z0-9]$`) // validateAppName checks that the given app name is safe for use in Docker // container names, image tags, and file system paths. var ( errAppNameLength = errors.New( "app name must be between " + strconv.Itoa(appNameMinLength) + " and " + strconv.Itoa(appNameMaxLength) + " characters", ) errAppNamePattern = errors.New( "app name must contain only lowercase letters, numbers, " + "and hyphens, and must start and end with a letter or number", ) ) func validateAppName(name string) error { if len(name) < appNameMinLength || len(name) > appNameMaxLength { return errAppNameLength } if !validAppNameRe.MatchString(name) { return errAppNamePattern } return nil }