1
0
forked from baron/baron-sso
Files
baron-sso/backend/internal/utils/slug.go
2026-02-06 16:28:00 +09:00

53 lines
1.1 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,
}
)
// 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, ""
}