pull/998/head
aryehlev 2 years ago
parent 0669d169a7
commit 2369657f8b
  1. 2
      regexes.go
  2. 95
      validator_test.go

@ -64,7 +64,7 @@ const (
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})?$`
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/ 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]){0,62}$"
domainRegexString = `^[a-z0-9-\.]{0,61}\.[a-z]{2,}$` domainRegexString = "^[a-z0-9-\\.]{1,61}\\.[a-z]{2,}$"
) )
var ( var (

@ -7793,6 +7793,57 @@ func TestUrl(t *testing.T) {
PanicMatches(t, func() { _ = validate.Var(i, "url") }, "Bad field type int") PanicMatches(t, func() { _ = validate.Var(i, "url") }, "Bad field type int")
} }
func TestDomain(t *testing.T) {
tests := []struct {
param string
expected bool
}{
{"http://foo.bar.com", false},
{"foobar.com", true},
{"foobar.coffee", true},
{"foobar.org", true},
{"foobar.ru", true},
{"http://user:pass@www.foobar.com/", false},
{"http://127.0.0.1/", false},
{"duckduckgo.com", true},
{"localhost", false},
{"http://foobar.com/?foo=bar#baz=qux", false},
{"", false},
{"a.co", true},
{"invalid.", false},
{".com", false},
{"b.co.il", true},
{"hwierd@hotmain.com.gz", false},
{"mailto:someone@example.com", false},
{"irc.server.org", true},
{"#channel@network", false},
{"/abs/test/dir", false},
{"./rel/test/dir", false},
}
validate := New()
for i, test := range tests {
errs := validate.Var(test.param, "domain")
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d Domain failed Error: %v", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d Domain failed Error: %v", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "domain" {
t.Fatalf("Index: %d Domain failed Error: %v", i, errs)
}
}
}
}
}
func TestUri(t *testing.T) { func TestUri(t *testing.T) {
tests := []struct { tests := []struct {
param string param string
@ -12264,25 +12315,25 @@ func TestCreditCardFormatValidation(t *testing.T) {
} }
func TestMultiOrOperatorGroup(t *testing.T) { func TestMultiOrOperatorGroup(t *testing.T) {
tests := []struct { tests := []struct {
Value int `validate:"eq=1|gte=5,eq=1|lt=7"` Value int `validate:"eq=1|gte=5,eq=1|lt=7"`
expected bool expected bool
}{ }{
{1, true}, {2, false}, {5, true}, {6, true}, {8, false}, {1, true}, {2, false}, {5, true}, {6, true}, {8, false},
} }
validate := New() validate := New()
for i, test := range tests { for i, test := range tests {
errs := validate.Struct(test) errs := validate.Struct(test)
if test.expected { if test.expected {
if !IsEqual(errs, nil) { if !IsEqual(errs, nil) {
t.Fatalf("Index: %d multi_group_of_OR_operators failed Error: %s", i, errs) t.Fatalf("Index: %d multi_group_of_OR_operators failed Error: %s", i, errs)
} }
} else { } else {
if IsEqual(errs, nil) { if IsEqual(errs, nil) {
t.Fatalf("Index: %d multi_group_of_OR_operators should have errs", i) t.Fatalf("Index: %d multi_group_of_OR_operators should have errs", i)
} }
} }
} }
} }

Loading…
Cancel
Save