Make IsDNS1123Subdomain return error strings

This commit is contained in:
Tim Hockin 2015-12-16 21:58:09 -08:00
parent 77eff06a53
commit 5862a60ae7
6 changed files with 29 additions and 19 deletions

View File

@ -37,8 +37,8 @@ func ValidateEvent(event *api.Event) field.ErrorList {
event.Namespace != event.InvolvedObject.Namespace {
allErrs = append(allErrs, field.Invalid(field.NewPath("involvedObject", "namespace"), event.InvolvedObject.Namespace, "does not match involvedObject"))
}
if !validation.IsDNS1123Subdomain(event.Namespace) {
allErrs = append(allErrs, field.Invalid(field.NewPath("namespace"), event.Namespace, ""))
for _, msg := range validation.IsDNS1123Subdomain(event.Namespace) {
allErrs = append(allErrs, field.Invalid(field.NewPath("namespace"), event.Namespace, msg))
}
return allErrs
}

View File

@ -58,7 +58,6 @@ func InclusiveRangeErrorMsg(lo, hi int) string {
return fmt.Sprintf(`must be between %d and %d, inclusive`, lo, hi)
}
var DNSSubdomainErrorMsg string = fmt.Sprintf(`must be a DNS subdomain (at most %d characters, matching regex %s): e.g. "example.com"`, validation.DNS1123SubdomainMaxLength, validation.DNS1123SubdomainFmt)
var DNS952LabelErrorMsg string = fmt.Sprintf(`must be a DNS 952 label (at most %d characters, matching regex %s): e.g. "my-name"`, validation.DNS952LabelMaxLength, validation.DNS952LabelFmt)
var pdPartitionErrorMsg string = InclusiveRangeErrorMsg(1, 255)
var PortRangeErrorMsg string = InclusiveRangeErrorMsg(1, 65535)
@ -241,10 +240,7 @@ func NameIsDNSSubdomain(name string, prefix bool) []string {
if prefix {
name = maskTrailingDash(name)
}
if validation.IsDNS1123Subdomain(name) {
return nil
}
return []string{DNSSubdomainErrorMsg}
return validation.IsDNS1123Subdomain(name)
}
// NameIsDNSLabel is a ValidateNameFunc for names that must be a DNS 1123 label.
@ -3014,7 +3010,7 @@ func ValidateLoadBalancerStatus(status *api.LoadBalancerStatus, fldPath *field.P
}
}
if len(ingress.Hostname) > 0 {
for _, msg := range NameIsDNSSubdomain(ingress.Hostname, false) {
for _, msg := range validation.IsDNS1123Subdomain(ingress.Hostname) {
allErrs = append(allErrs, field.Invalid(idxPath.Child("hostname"), ingress.Hostname, msg))
}
if isIP := (net.ParseIP(ingress.Hostname) != nil); isIP {

View File

@ -4691,7 +4691,7 @@ func TestValidateLimitRange(t *testing.T) {
},
"invalid-name": {
api.LimitRange{ObjectMeta: api.ObjectMeta{Name: "^Invalid", Namespace: "foo"}, Spec: api.LimitRangeSpec{}},
DNSSubdomainErrorMsg,
"must match the regex",
},
"invalid-namespace": {
api.LimitRange{ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: "^Invalid"}, Spec: api.LimitRangeSpec{}},
@ -5015,7 +5015,7 @@ func TestValidateResourceQuota(t *testing.T) {
},
"invalid Name": {
api.ResourceQuota{ObjectMeta: api.ObjectMeta{Name: "^Invalid", Namespace: "foo"}, Spec: spec},
DNSSubdomainErrorMsg,
"must match the regex",
},
"invalid Namespace": {
api.ResourceQuota{ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: "^Invalid"}, Spec: spec},
@ -5618,7 +5618,7 @@ func TestValidateEndpoints(t *testing.T) {
"invalid name": {
endpoints: api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "-_Invliad^&Characters", Namespace: "namespace"}},
errorType: "FieldValueInvalid",
errorDetail: DNSSubdomainErrorMsg,
errorDetail: "must match the regex",
},
"empty addresses": {
endpoints: api.Endpoints{

View File

@ -353,7 +353,7 @@ func validateIngressRules(IngressRules []extensions.IngressRule, fldPath *field.
if len(ih.Host) > 0 {
// TODO: Ports and ips are allowed in the host part of a url
// according to RFC 3986, consider allowing them.
for _, msg := range apivalidation.NameIsDNSSubdomain(ih.Host, false) {
for _, msg := range validation.IsDNS1123Subdomain(ih.Host) {
allErrs = append(allErrs, field.Invalid(fldPath.Index(i).Child("host"), ih.Host, msg))
}
if isIP := (net.ParseIP(ih.Host) != nil); isIP {

View File

@ -47,8 +47,8 @@ func IsQualifiedName(value string) []string {
prefix, name = parts[0], parts[1]
if len(prefix) == 0 {
errs = append(errs, "prefix part "+EmptyError())
} else if !IsDNS1123Subdomain(prefix) {
errs = append(errs, fmt.Sprintf("prefix part must be a DNS subdomain (e.g. 'example.com')"))
} else if msgs := IsDNS1123Subdomain(prefix); len(msgs) != 0 {
errs = append(errs, prefixEach(msgs, "prefix part ")...)
}
default:
return append(errs, RegexError(qualifiedNameFmt, "MyName", "my.name", "123-abc")+
@ -110,8 +110,15 @@ var dns1123SubdomainRegexp = regexp.MustCompile("^" + DNS1123SubdomainFmt + "$")
// IsDNS1123Subdomain tests for a string that conforms to the definition of a
// subdomain in DNS (RFC 1123).
func IsDNS1123Subdomain(value string) bool {
return len(value) <= DNS1123SubdomainMaxLength && dns1123SubdomainRegexp.MatchString(value)
func IsDNS1123Subdomain(value string) []string {
var errs []string
if len(value) > DNS1123SubdomainMaxLength {
errs = append(errs, MaxLenError(DNS1123SubdomainMaxLength))
}
if !dns1123SubdomainRegexp.MatchString(value) {
errs = append(errs, RegexError(DNS1123SubdomainFmt, "example.com"))
}
return errs
}
const DNS952LabelFmt string = "[a-z]([-a-z0-9]*[a-z0-9])?"
@ -239,3 +246,10 @@ func RegexError(fmt string, examples ...string) string {
func EmptyError() string {
return "must be non-empty"
}
func prefixEach(msgs []string, prefix string) []string {
for i := range msgs {
msgs[i] = prefix + msgs[i]
}
return msgs
}

View File

@ -60,8 +60,8 @@ func TestIsDNS1123Subdomain(t *testing.T) {
strings.Repeat("a", 253),
}
for _, val := range goodValues {
if !IsDNS1123Subdomain(val) {
t.Errorf("expected true for '%s'", val)
if msgs := IsDNS1123Subdomain(val); len(msgs) != 0 {
t.Errorf("expected true for '%s': %v", val, msgs)
}
}
@ -80,7 +80,7 @@ func TestIsDNS1123Subdomain(t *testing.T) {
strings.Repeat("a", 254),
}
for _, val := range badValues {
if IsDNS1123Subdomain(val) {
if msgs := IsDNS1123Subdomain(val); len(msgs) == 0 {
t.Errorf("expected false for '%s'", val)
}
}