Add DNS RFC 1035 label format validator (#833)

pull/875/head
Kazuki Onishi 3 years ago committed by GitHub
parent c67d01d610
commit ec2071b383
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      baked_in.go
  2. 7
      doc.go
  3. 2
      regexes.go
  4. 39
      validator_test.go

@ -198,6 +198,7 @@ var (
"postcode_iso3166_alpha2": isPostcodeByIso3166Alpha2, "postcode_iso3166_alpha2": isPostcodeByIso3166Alpha2,
"postcode_iso3166_alpha2_field": isPostcodeByIso3166Alpha2Field, "postcode_iso3166_alpha2_field": isPostcodeByIso3166Alpha2Field,
"bic": isIsoBicFormat, "bic": isIsoBicFormat,
"dns_rfc1035_label": isDnsRFC1035LabelFormat,
} }
) )
@ -2413,3 +2414,11 @@ func isIsoBicFormat(fl FieldLevel) bool {
return bicRegex.MatchString(bicString) 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)
}

@ -1255,6 +1255,13 @@ More information on https://www.iso.org/standard/60390.html
Usage: bic 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 TimeZone
This validates that a string value is a valid time zone based on the time zone database present on the system. This validates that a string value is a valid time zone based on the time zone database present on the system.

@ -51,6 +51,7 @@ const (
jWTRegexString = "^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]*$" jWTRegexString = "^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]*$"
splitParamsRegexString = `'[^']*'|\S+` splitParamsRegexString = `'[^']*'|\S+`
bicRegexString = `^[A-Za-z]{6}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$` 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 ( var (
@ -102,4 +103,5 @@ var (
jWTRegex = regexp.MustCompile(jWTRegexString) jWTRegex = regexp.MustCompile(jWTRegexString)
splitParamsRegex = regexp.MustCompile(splitParamsRegexString) splitParamsRegex = regexp.MustCompile(splitParamsRegexString)
bicRegex = regexp.MustCompile(bicRegexString) bicRegex = regexp.MustCompile(bicRegexString)
dnsRegexRFC1035Label = regexp.MustCompile(dnsRegexStringRFC1035Label)
) )

@ -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) { func TestPostCodeByIso3166Alpha2(t *testing.T) {
tests := map[string][]struct { tests := map[string][]struct {
value string value string

Loading…
Cancel
Save