pull/998/head
aryehlev 2 years ago
parent 0669d169a7
commit 2369657f8b
  1. 2
      regexes.go
  2. 51
      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

Loading…
Cancel
Save