Repository: go-playground/validator (go)

Issue:
RFC1035 DNS Labels can be more than 63 characters long
- [x] I have looked at the documentation [here](https://pkg.go.dev/github.com/go-playground/validator/v10#section-documentation) first?
- [x] I have looked at the examples provided that may showcase my question [here](/_examples)?

### Package version eg. v9, v10: 

v10

### Issue, Question or Enhancement:

The current regular expression for evaluating if a DNS label is RFC1035 compliant is:

`dnsRegexStringRFC1035Label   = "^[a-z]([-a-z0-9]*[a-z0-9]){0,62}$"`

The intention is clear (one alpha character followed by 0-62 alphanum-hypen characters followed by one alphanumeric character). However it is flawed since the max and min count accounts for everything within the parentheses. In effect this allows for unlimited length.

### Code sample, to showcase or reproduce:

The code at https://go.dev/play/p/N7bXBCE-mvl should return false but returns true.

To test it in the current main branch:

Add the following tests to TestRFC1035LabelFormatValidation in `validate_test.go`:

```
{"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk", "dns_rfc1035_label", true},
{"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl", "dns_rfc1035_label", false},
```

Run the tests. The second one will fail. It should not.

### Fix

The fix it to amend the regexp and introduce a length check in `isDnsRFC1035LabelFormat()`. Kubernetes and other projects does this exact same thing.

The regex should be:

`^[a-z]([-a-z0-9]*[a-z0-9])?$`

I'll submit a PR.

Pull request:
Fix length check on dns_rfc1035_label tag
## Fixes Or Enhances

Fix length check on dns_rfc1035_label tag.

Fixes #1213 

**Make sure that you've checked the boxes below before you submit PR:**
- [x] Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

Patch:
diff --git a/baked_in.go b/baked_in.go
index cca3e09..e8dfb28 100644
--- a/baked_in.go
+++ b/baked_in.go
@@ -1376,7 +1376,6 @@ func isEqIgnoreCase(fl FieldLevel) bool {
 	param := fl.Param()
 
 	switch field.Kind() {
-
 	case reflect.String:
 		return strings.EqualFold(field.String(), param)
 	}
@@ -1606,7 +1605,6 @@ func isImage(fl FieldLevel) bool {
 	case reflect.String:
 		filePath := field.String()
 		fileInfo, err := os.Stat(filePath)
-
 		if err != nil {
 			return false
 		}
@@ -1635,7 +1633,6 @@ func isImage(fl FieldLevel) bool {
 
 // isFilePath is the validation function for validating if the current field's value is a valid file path.
 func isFilePath(fl FieldLevel) bool {
-
 	var exists bool
 	var err error
 
@@ -2227,7 +2224,6 @@ func isGt(fl FieldLevel) bool {
 	case reflect.Struct:
 
 		if field.Type().ConvertibleTo(timeType) {
-
 			return field.Convert(timeType).Interface().(time.Time).After(time.Now().UTC())
 		}
 	}
@@ -2464,7 +2460,6 @@ func isLt(fl FieldLevel) bool {
 	case reflect.Struct:
 
 		if field.Type().ConvertibleTo(timeType) {
-
 			return field.Convert(timeType).Interface().(time.Time).Before(time.Now().UTC())
 		}
 	}
@@ -2644,7 +2639,6 @@ func isDir(fl FieldLevel) bool {
 
 // isDirPath is the validation function for validating if the current field's value is a valid directory.
 func isDirPath(fl FieldLevel) bool {
-
 	var exists bool
 	var err error
 
@@ -2957,6 +2951,12 @@ func isCveFormat(fl FieldLevel) bool {
 // a valid dns RFC 1035 label, defined in RFC 1035.
 func isDnsRFC1035LabelFormat(fl FieldLevel) bool {
 	val := fl.Field().String()
+
+	size := len(val)
+	if size > 63 {
+		return false
+	}
+
 	return dnsRegexRFC1035Label().MatchString(val)
 }
 
diff --git a/regexes.go b/regexes.go
index 871cf7d..b6d2af2 100644
--- a/regexes.go
+++ b/regexes.go
@@ -69,7 +69,7 @@ const (
 	splitParamsRegexString           = `'[^']*'|\S+`
 	bicRegexString                   = `^[A-Za-z]{6}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$`
 	semverRegexString                = `^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$` // numbered capture groups https://semver.org/
-	dnsRegexStringRFC1035Label       = "^[a-z]([-a-z0-9]*[a-z0-9]){0,62}$"
+	dnsRegexStringRFC1035Label       = "^[a-z]([-a-z0-9]*[a-z0-9])?$"
 	cveRegexString                   = `^CVE-(1999|2\d{3})-(0[^0]\d{2}|0\d[^0]\d{1}|0\d{2}[^0]|[1-9]{1}\d{3,})$` // CVE Format Id https://cve.mitre.org/cve/identifiers/syntaxchange.html
 	mongodbIdRegexString             = "^[a-f\\d]{24}$"
 	mongodbConnStringRegexString     = "^mongodb(\\+srv)?:\\/\\/(([a-zA-Z\\d]+):([a-zA-Z\\d$:\\/?#\\[\\]@]+)@)?(([a-z\\d.-]+)(:[\\d]+)?)((,(([a-z\\d.-]+)(:(\\d+))?))*)?(\\/[a-zA-Z-_]{1,64})?(\\?(([a-zA-Z]+)=([a-zA-Z\\d]+))(&(([a-zA-Z\\d]+)=([a-zA-Z\\d]+))?)*)?$"


Test patch:
diff --git a/validator_test.go b/validator_test.go
index af05d19..2f76dc2 100644
--- a/validator_test.go
+++ b/validator_test.go
@@ -13243,6 +13243,8 @@ func TestRFC1035LabelFormatValidation(t *testing.T) {
 		{"ABC-ABC", "dns_rfc1035_label", false},
 		{"123-abc", "dns_rfc1035_label", false},
 		{"", "dns_rfc1035_label", false},
+		{"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk", "dns_rfc1035_label", true},
+		{"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl", "dns_rfc1035_label", false},
 	}
 
 	validate := New()