Add util to validate namespaced names

This commit is contained in:
Tim Hockin
2014-11-25 00:16:23 -08:00
parent 4845e524af
commit 5ecce5d60c
3 changed files with 58 additions and 13 deletions

View File

@@ -18,6 +18,7 @@ package util
import (
"regexp"
"strings"
)
// IsDNSLabel tests for a string that conforms to the definition of a label in
@@ -82,3 +83,23 @@ func IsCIdentifier(value string) bool {
func IsValidPortNum(port int) bool {
return 0 < port && port < 65536
}
// IsQualifiedName tests whether a string fits the "optionally-namespaced
// name" pattern: [ DNS_SUBDOMAIN "/" ] DNS_LABEL
func IsQualifiedName(value string) bool {
var n, ns string
parts := strings.Split(value, "/")
switch len(parts) {
case 1:
n = parts[0]
case 2:
ns = parts[0]
n = parts[1]
default:
return false
}
if (ns != "" && !IsDNSSubdomain(ns)) || !IsDNSLabel(n) {
return false
}
return true
}