2
0

dns_label.go 630 B

1234567891011121314151617181920212223
  1. package utils
  2. import "github.com/gosimple/slug"
  3. const maxLength = 63
  4. // ValidDNSLabel converts a string to a valid DNS label (RFC 1123)
  5. // "a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character"
  6. func ValidDNSLabel(value string) string {
  7. slug.CustomSub = map[string]string{
  8. "_": "-",
  9. }
  10. slug.Lowercase = true
  11. // As per slug docs, Minus sign and underscore characters will never appear at the beginning or the end of the returned string
  12. slug := slug.Make(value)
  13. if len(slug) > maxLength {
  14. slug = slug[:maxLength]
  15. }
  16. return slug
  17. }