|
| 1 | +/* |
| 2 | +Copyright 2018 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package validation |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "regexp" |
| 22 | +) |
| 23 | + |
| 24 | +// This file's code was modified from "k8s.io/apimachinery/pkg/util/validation" |
| 25 | +// to avoid package dependencies. In case of additional functionality from |
| 26 | +// "k8s.io/apimachinery" is needed, re-consider whether to add the dependency. |
| 27 | + |
| 28 | +const ( |
| 29 | + dns1123LabelFmt string = "[a-z0-9]([-a-z0-9]*[a-z0-9])?" |
| 30 | + dns1123SubdomainFmt string = dns1123LabelFmt + "(\\." + dns1123LabelFmt + ")*" |
| 31 | + dns1035LabelFmt string = "[a-z]([-a-z0-9]*[a-z0-9])?" |
| 32 | +) |
| 33 | + |
| 34 | +type dnsValidationConfig struct { |
| 35 | + format string |
| 36 | + maxLen int |
| 37 | + re *regexp.Regexp |
| 38 | + errMsg string |
| 39 | + examples []string |
| 40 | +} |
| 41 | + |
| 42 | +var dns1123LabelConfig = dnsValidationConfig{ |
| 43 | + format: dns1123LabelFmt, |
| 44 | + maxLen: 56, // = 63 - len("-system") |
| 45 | + re: regexp.MustCompile("^" + dns1123LabelFmt + "$"), |
| 46 | + errMsg: "a DNS-1123 label must consist of lower case alphanumeric characters or '-'", |
| 47 | + examples: []string{"example.com"}, |
| 48 | +} |
| 49 | + |
| 50 | +var dns1123SubdomainConfig = dnsValidationConfig{ |
| 51 | + format: dns1123SubdomainFmt, |
| 52 | + maxLen: 253, // a subdomain's max length in DNS (RFC 1123). |
| 53 | + re: regexp.MustCompile("^" + dns1123SubdomainFmt + "$"), |
| 54 | + errMsg: "a DNS-1123 subdomain must consist of lower case alphanumeric characters, " + |
| 55 | + "'-' or '.', and must start and end with an alphanumeric character", |
| 56 | + examples: []string{"my-name", "abc-123"}, |
| 57 | +} |
| 58 | + |
| 59 | +var dns1035LabelConfig = dnsValidationConfig{ |
| 60 | + format: dns1035LabelFmt, |
| 61 | + maxLen: 63, // a label's max length in DNS (RFC 1035). |
| 62 | + re: regexp.MustCompile("^" + dns1035LabelFmt + "$"), |
| 63 | + errMsg: "a DNS-1035 label must consist of lower case alphanumeric characters or '-', " + |
| 64 | + "start with an alphabetic character, and end with an alphanumeric character", |
| 65 | + examples: []string{"my-name", "123-abc"}, |
| 66 | +} |
| 67 | + |
| 68 | +func (c dnsValidationConfig) check(value string) (errs []string) { |
| 69 | + if len(value) > c.maxLen { |
| 70 | + errs = append(errs, maxLenError(c.maxLen)) |
| 71 | + } |
| 72 | + if !c.re.MatchString(value) { |
| 73 | + errs = append(errs, regexError(c.errMsg, c.format, c.examples...)) |
| 74 | + } |
| 75 | + return errs |
| 76 | +} |
| 77 | + |
| 78 | +// IsDNS1123Subdomain tests for a string that conforms to the definition of a |
| 79 | +// subdomain in DNS (RFC 1123). |
| 80 | +func IsDNS1123Subdomain(value string) []string { |
| 81 | + return dns1123SubdomainConfig.check(value) |
| 82 | +} |
| 83 | + |
| 84 | +// IsDNS1123Label tests for a string that conforms to the definition of a label in DNS (RFC 1123). |
| 85 | +func IsDNS1123Label(value string) []string { |
| 86 | + return dns1123LabelConfig.check(value) |
| 87 | +} |
| 88 | + |
| 89 | +// IsDNS1035Label tests for a string that conforms to the definition of a label in DNS (RFC 1035). |
| 90 | +func IsDNS1035Label(value string) []string { |
| 91 | + return dns1035LabelConfig.check(value) |
| 92 | +} |
| 93 | + |
| 94 | +// maxLenError returns a string explanation of a "string too long" validation |
| 95 | +// failure. |
| 96 | +func maxLenError(length int) string { |
| 97 | + return fmt.Sprintf("must be no more than %d characters", length) |
| 98 | +} |
| 99 | + |
| 100 | +// regexError returns a string explanation of a regex validation failure. |
| 101 | +func regexError(msg string, fmt string, examples ...string) string { |
| 102 | + if len(examples) == 0 { |
| 103 | + return msg + " (regex used for validation is '" + fmt + "')" |
| 104 | + } |
| 105 | + msg += " (e.g. " |
| 106 | + for i := range examples { |
| 107 | + if i > 0 { |
| 108 | + msg += " or " |
| 109 | + } |
| 110 | + msg += "'" + examples[i] + "', " |
| 111 | + } |
| 112 | + msg += "regex used for validation is '" + fmt + "')" |
| 113 | + return msg |
| 114 | +} |
0 commit comments