From 2369657f8b0fe043c3eb9b4f3ea87e458d9d1e15 Mon Sep 17 00:00:00 2001 From: aryehlev Date: Wed, 14 Sep 2022 19:18:26 +0300 Subject: [PATCH] add test --- regexes.go | 2 +- validator_test.go | 95 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 74 insertions(+), 23 deletions(-) diff --git a/regexes.go b/regexes.go index ba2fdbf..e5a053b 100644 --- a/regexes.go +++ b/regexes.go @@ -64,7 +64,7 @@ const ( 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/ 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 ( diff --git a/validator_test.go b/validator_test.go index 1ebdea5..3bd6d0f 100644 --- a/validator_test.go +++ b/validator_test.go @@ -7793,6 +7793,57 @@ func TestUrl(t *testing.T) { 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) { tests := []struct { param string @@ -12264,25 +12315,25 @@ func TestCreditCardFormatValidation(t *testing.T) { } func TestMultiOrOperatorGroup(t *testing.T) { - tests := []struct { - Value int `validate:"eq=1|gte=5,eq=1|lt=7"` - expected bool - }{ - {1, true}, {2, false}, {5, true}, {6, true}, {8, false}, - } - - validate := New() - - for i, test := range tests { - errs := validate.Struct(test) - if test.expected { - if !IsEqual(errs, nil) { - t.Fatalf("Index: %d multi_group_of_OR_operators failed Error: %s", i, errs) - } - } else { - if IsEqual(errs, nil) { - t.Fatalf("Index: %d multi_group_of_OR_operators should have errs", i) - } - } - } - } + tests := []struct { + Value int `validate:"eq=1|gte=5,eq=1|lt=7"` + expected bool + }{ + {1, true}, {2, false}, {5, true}, {6, true}, {8, false}, + } + + validate := New() + + for i, test := range tests { + errs := validate.Struct(test) + if test.expected { + if !IsEqual(errs, nil) { + t.Fatalf("Index: %d multi_group_of_OR_operators failed Error: %s", i, errs) + } + } else { + if IsEqual(errs, nil) { + t.Fatalf("Index: %d multi_group_of_OR_operators should have errs", i) + } + } + } +}