Skip to content

Commit

Permalink
Add DNS RFC 1035 label format validator (#833)
Browse files Browse the repository at this point in the history
  • Loading branch information
0n1shi authored Jan 2, 2022
1 parent c67d01d commit ec2071b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
9 changes: 9 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ var (
"postcode_iso3166_alpha2": isPostcodeByIso3166Alpha2,
"postcode_iso3166_alpha2_field": isPostcodeByIso3166Alpha2Field,
"bic": isIsoBicFormat,
"dns_rfc1035_label": isDnsRFC1035LabelFormat,
}
)

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

return bicRegex.MatchString(bicString)
}

// isDnsRFC1035LabelFormat is the validation function
// for validating if the current field's value is
// a valid dns RFC 1035 label, defined in RFC 1035.
func isDnsRFC1035LabelFormat(fl FieldLevel) bool {
val := fl.Field().String()
return dnsRegexRFC1035Label.MatchString(val)
}
7 changes: 7 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,13 @@ More information on https://www.iso.org/standard/60390.html
Usage: bic
RFC 1035 label
This validates that a string value is a valid dns RFC 1035 label, defined in RFC 1035.
More information on https://datatracker.ietf.org/doc/html/rfc1035
Usage: dns_rfc1035_label
TimeZone
This validates that a string value is a valid time zone based on the time zone database present on the system.
Expand Down
2 changes: 2 additions & 0 deletions regexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const (
jWTRegexString = "^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]*$"
splitParamsRegexString = `'[^']*'|\S+`
bicRegexString = `^[A-Za-z]{6}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$`
dnsRegexStringRFC1035Label = "^[a-z]([-a-z0-9]*[a-z0-9]){0,62}$"
)

var (
Expand Down Expand Up @@ -102,4 +103,5 @@ var (
jWTRegex = regexp.MustCompile(jWTRegexString)
splitParamsRegex = regexp.MustCompile(splitParamsRegexString)
bicRegex = regexp.MustCompile(bicRegexString)
dnsRegexRFC1035Label = regexp.MustCompile(dnsRegexStringRFC1035Label)
)
39 changes: 39 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11343,6 +11343,45 @@ func TestBicIsoFormatValidation(t *testing.T) {
}
}

func TestRFC1035LabelFormatValidation(t *testing.T) {
tests := []struct {
value string `validate:"dns_rfc1035_label"`
tag string
expected bool
}{
{"abc", "dns_rfc1035_label", true},
{"abc-", "dns_rfc1035_label", false},
{"abc-123", "dns_rfc1035_label", true},
{"ABC", "dns_rfc1035_label", false},
{"ABC-123", "dns_rfc1035_label", false},
{"abc-abc", "dns_rfc1035_label", true},
{"ABC-ABC", "dns_rfc1035_label", false},
{"123-abc", "dns_rfc1035_label", false},
{"", "dns_rfc1035_label", false},
}

validate := New()

for i, test := range tests {
errs := validate.Var(test.value, test.tag)

if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d dns_rfc1035_label failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d dns_rfc1035_label failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "dns_rfc1035_label" {
t.Fatalf("Index: %d dns_rfc1035_label failed Error: %s", i, errs)
}
}
}
}
}

func TestPostCodeByIso3166Alpha2(t *testing.T) {
tests := map[string][]struct {
value string
Expand Down

0 comments on commit ec2071b

Please sign in to comment.