1
0
forked from baron/baron-sso
Files
baron-sso/backend/internal/utils/slug.go
2026-02-19 15:21:47 +09:00

78 lines
1.6 KiB
Go

package utils
import (
"regexp"
"strings"
)
var (
slugRegex = regexp.MustCompile(`^[a-z0-9-]+$`)
reservedSlugs = map[string]bool{
"admin": true,
"api": true,
"auth": true,
"system": true,
"root": true,
"super": true,
"public": true,
"internal": true,
"baron": true,
"sso": true,
"login": true,
"logout": true,
"signup": true,
"register": true,
"tenant": true,
"user": true,
"dev": true,
"stage": true,
"prod": true,
"test": true,
"static": true,
"assets": true,
"image": true,
"img": true,
"mail": true,
"smtp": true,
"pop": true,
"imap": true,
"ns": true,
"mx": true,
"webmaster": true,
"security": true,
"support": true,
"help": true,
"billing": true,
"account": true,
"config": true,
"status": true,
"health": true,
"metrics": true,
"grafana": true,
"prometheus": true,
}
)
// ValidateSlug checks if a slug meets requirements and is not reserved.
func ValidateSlug(slug string) (bool, string) {
s := strings.ToLower(strings.TrimSpace(slug))
if len(s) < 3 || len(s) > 32 {
return false, "slug must be between 3 and 32 characters"
}
if !slugRegex.MatchString(s) {
return false, "slug can only contain lowercase letters, numbers, and hyphens"
}
if strings.HasPrefix(s, "-") || strings.HasSuffix(s, "-") {
return false, "slug cannot start or end with a hyphen"
}
if reservedSlugs[s] {
return false, "slug is a reserved keyword"
}
return true, ""
}