Skip to content

Commit ec2071b

Browse files
authored
Add DNS RFC 1035 label format validator (#833)
1 parent c67d01d commit ec2071b

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

baked_in.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ var (
198198
"postcode_iso3166_alpha2": isPostcodeByIso3166Alpha2,
199199
"postcode_iso3166_alpha2_field": isPostcodeByIso3166Alpha2Field,
200200
"bic": isIsoBicFormat,
201+
"dns_rfc1035_label": isDnsRFC1035LabelFormat,
201202
}
202203
)
203204

@@ -2413,3 +2414,11 @@ func isIsoBicFormat(fl FieldLevel) bool {
24132414

24142415
return bicRegex.MatchString(bicString)
24152416
}
2417+
2418+
// isDnsRFC1035LabelFormat is the validation function
2419+
// for validating if the current field's value is
2420+
// a valid dns RFC 1035 label, defined in RFC 1035.
2421+
func isDnsRFC1035LabelFormat(fl FieldLevel) bool {
2422+
val := fl.Field().String()
2423+
return dnsRegexRFC1035Label.MatchString(val)
2424+
}

doc.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,13 @@ More information on https://www.iso.org/standard/60390.html
12551255
12561256
Usage: bic
12571257
1258+
RFC 1035 label
1259+
1260+
This validates that a string value is a valid dns RFC 1035 label, defined in RFC 1035.
1261+
More information on https://datatracker.ietf.org/doc/html/rfc1035
1262+
1263+
Usage: dns_rfc1035_label
1264+
12581265
TimeZone
12591266
12601267
This validates that a string value is a valid time zone based on the time zone database present on the system.

regexes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const (
5151
jWTRegexString = "^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]*$"
5252
splitParamsRegexString = `'[^']*'|\S+`
5353
bicRegexString = `^[A-Za-z]{6}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$`
54+
dnsRegexStringRFC1035Label = "^[a-z]([-a-z0-9]*[a-z0-9]){0,62}$"
5455
)
5556

5657
var (
@@ -102,4 +103,5 @@ var (
102103
jWTRegex = regexp.MustCompile(jWTRegexString)
103104
splitParamsRegex = regexp.MustCompile(splitParamsRegexString)
104105
bicRegex = regexp.MustCompile(bicRegexString)
106+
dnsRegexRFC1035Label = regexp.MustCompile(dnsRegexStringRFC1035Label)
105107
)

validator_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11343,6 +11343,45 @@ func TestBicIsoFormatValidation(t *testing.T) {
1134311343
}
1134411344
}
1134511345

11346+
func TestRFC1035LabelFormatValidation(t *testing.T) {
11347+
tests := []struct {
11348+
value string `validate:"dns_rfc1035_label"`
11349+
tag string
11350+
expected bool
11351+
}{
11352+
{"abc", "dns_rfc1035_label", true},
11353+
{"abc-", "dns_rfc1035_label", false},
11354+
{"abc-123", "dns_rfc1035_label", true},
11355+
{"ABC", "dns_rfc1035_label", false},
11356+
{"ABC-123", "dns_rfc1035_label", false},
11357+
{"abc-abc", "dns_rfc1035_label", true},
11358+
{"ABC-ABC", "dns_rfc1035_label", false},
11359+
{"123-abc", "dns_rfc1035_label", false},
11360+
{"", "dns_rfc1035_label", false},
11361+
}
11362+
11363+
validate := New()
11364+
11365+
for i, test := range tests {
11366+
errs := validate.Var(test.value, test.tag)
11367+
11368+
if test.expected {
11369+
if !IsEqual(errs, nil) {
11370+
t.Fatalf("Index: %d dns_rfc1035_label failed Error: %s", i, errs)
11371+
}
11372+
} else {
11373+
if IsEqual(errs, nil) {
11374+
t.Fatalf("Index: %d dns_rfc1035_label failed Error: %s", i, errs)
11375+
} else {
11376+
val := getError(errs, "", "")
11377+
if val.Tag() != "dns_rfc1035_label" {
11378+
t.Fatalf("Index: %d dns_rfc1035_label failed Error: %s", i, errs)
11379+
}
11380+
}
11381+
}
11382+
}
11383+
}
11384+
1134611385
func TestPostCodeByIso3166Alpha2(t *testing.T) {
1134711386
tests := map[string][]struct {
1134811387
value string

0 commit comments

Comments
 (0)