From 22b3c4dd422d3122039a40a2747f71205082daa5 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Thu, 13 Jan 2022 20:19:41 +0800 Subject: [PATCH] feat: add validator functions, IsAllUpper, IsAllLower, ContainUpper, ContainLower, ContainLetter, IsJSON and IsPort --- validator/validator.go | 95 +++++++++++++++++++++++++-------- validator/validator_test.go | 102 ++++++++++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+), 22 deletions(-) diff --git a/validator/validator.go b/validator/validator.go index 8b6128e..bd1e1b8 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -5,17 +5,72 @@ package validator import ( + "encoding/json" "net" "regexp" "strconv" + "strings" "unicode" ) var isAlphaRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`) // IsAlpha checks if the string contains only letters (a-zA-Z) -func IsAlpha(s string) bool { - return isAlphaRegexMatcher.MatchString(s) +func IsAlpha(str string) bool { + return isAlphaRegexMatcher.MatchString(str) +} + +// IsAllUpper check if the string is all upper case letters A-Z +func IsAllUpper(str string) bool { + for _, r := range str { + if !unicode.IsUpper(r) { + return false + } + } + return str != "" +} + +// IsAllLower check if the string is all lower case letters a-z +func IsAllLower(str string) bool { + for _, r := range str { + if !unicode.IsLower(r) { + return false + } + } + return str != "" +} + +// ContainUpper check if the string contain at least one upper case letter A-Z +func ContainUpper(str string) bool { + for _, r := range str { + if unicode.IsUpper(r) && unicode.IsLetter(r) { + return true + } + } + return false +} + +// ContainLower check if the string contain at least one lower case letter A-Z +func ContainLower(str string) bool { + for _, r := range str { + if unicode.IsLower(r) && unicode.IsLetter(r) { + return true + } + } + return false +} + +var containLetterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`) + +// ContainLetter check if the string contain at least one letter +func ContainLetter(str string) bool { + return containLetterRegexMatcher.MatchString(str) +} + +// Is checks if the string is valid JSON +func IsJSON(str string) bool { + var js json.RawMessage + return json.Unmarshal([]byte(str), &js) == nil } // IsNumberStr check if the string can convert to a number. @@ -24,16 +79,16 @@ func IsNumberStr(s string) bool { } // IsFloatStr check if the string can convert to a float. -func IsFloatStr(s string) bool { - _, e := strconv.ParseFloat(s, 64) +func IsFloatStr(str string) bool { + _, e := strconv.ParseFloat(str, 64) return e == nil } var isIntStrRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`) // IsIntStr check if the string can convert to a integer. -func IsIntStr(s string) bool { - return isIntStrRegexMatcher.MatchString(s) +func IsIntStr(str string) bool { + return isIntStrRegexMatcher.MatchString(str) } // IsIp check if the string is a ip address. @@ -48,13 +103,7 @@ func IsIpV4(ipstr string) bool { if ip == nil { return false } - for i := 0; i < len(ipstr); i++ { - switch ipstr[i] { - case '.': - return true - } - } - return false + return strings.Contains(ipstr, ".") } // IsIpV6 check if the string is a ipv6 address. @@ -63,11 +112,13 @@ func IsIpV6(ipstr string) bool { if ip == nil { return false } - for i := 0; i < len(ipstr); i++ { - switch ipstr[i] { - case ':': - return true - } + return strings.Contains(ipstr, ":") +} + +// IsPort check if the string is a valid net port. +func IsPort(str string) bool { + if i, err := strconv.ParseInt(str, 10, 64); err == nil && i > 0 && i < 65536 { + return true } return false } @@ -130,14 +181,14 @@ func IsBase64(base64 string) bool { } // IsEmptyString check if the string is empty. -func IsEmptyString(s string) bool { - return len(s) == 0 +func IsEmptyString(str string) bool { + return len(str) == 0 } // IsRegexMatch check if the string match the regexp -func IsRegexMatch(s, regex string) bool { +func IsRegexMatch(str, regex string) bool { reg := regexp.MustCompile(regex) - return reg.MatchString(s) + return reg.MatchString(str) } // IsStrongPassword check if the string is strong password, if len(password) is less than the length param, return false diff --git a/validator/validator_test.go b/validator/validator_test.go index c500199..6d07bb6 100644 --- a/validator/validator_test.go +++ b/validator/validator_test.go @@ -6,6 +6,96 @@ import ( "github.com/duke-git/lancet/internal" ) +func TestIsAllUpper(t *testing.T) { + assert := internal.NewAssert(t, "TestIsAllUpper") + + assert.Equal(true, IsAllUpper("ABC")) + assert.Equal(false, IsAllUpper("")) + assert.Equal(false, IsAllUpper("abc")) + assert.Equal(false, IsAllUpper("aBC")) + assert.Equal(false, IsAllUpper("1BC")) + assert.Equal(false, IsAllUpper("1bc")) + assert.Equal(false, IsAllUpper("123")) + assert.Equal(false, IsAllUpper("你好")) + assert.Equal(false, IsAllUpper("A&")) + assert.Equal(false, IsAllUpper("&@#$%^&*")) +} + +func TestIsAllLower(t *testing.T) { + assert := internal.NewAssert(t, "TestIsAllLower") + + assert.Equal(true, IsAllLower("abc")) + assert.Equal(false, IsAllLower("ABC")) + assert.Equal(false, IsAllLower("")) + assert.Equal(false, IsAllLower("aBC")) + assert.Equal(false, IsAllLower("1BC")) + assert.Equal(false, IsAllLower("1bc")) + assert.Equal(false, IsAllLower("123")) + assert.Equal(false, IsAllLower("你好")) + assert.Equal(false, IsAllLower("A&")) + assert.Equal(false, IsAllLower("&@#$%^&*")) +} + +func TestContainLower(t *testing.T) { + assert := internal.NewAssert(t, "TestContainLower") + + assert.Equal(true, ContainLower("abc")) + assert.Equal(true, ContainLower("aBC")) + assert.Equal(true, ContainLower("1bc")) + assert.Equal(true, ContainLower("a&")) + + assert.Equal(false, ContainLower("ABC")) + assert.Equal(false, ContainLower("")) + assert.Equal(false, ContainLower("1BC")) + assert.Equal(false, ContainLower("123")) + assert.Equal(false, ContainLower("你好")) + assert.Equal(false, ContainLower("&@#$%^&*")) +} + +func TestContainUpper(t *testing.T) { + assert := internal.NewAssert(t, "TestContainUpper") + + assert.Equal(true, ContainUpper("ABC")) + assert.Equal(true, ContainUpper("aBC")) + assert.Equal(true, ContainUpper("1BC")) + assert.Equal(true, ContainUpper("A&")) + + assert.Equal(false, ContainUpper("abc")) + assert.Equal(false, ContainUpper("")) + assert.Equal(false, ContainUpper("1bc")) + assert.Equal(false, ContainUpper("123")) + assert.Equal(false, ContainUpper("你好")) + assert.Equal(false, ContainUpper("&@#$%^&*")) +} + +func TestContainLetter(t *testing.T) { + assert := internal.NewAssert(t, "TestContainLetter") + + assert.Equal(true, ContainLetter("ABC")) + assert.Equal(true, ContainLetter("1Bc")) + assert.Equal(true, ContainLetter("1ab")) + assert.Equal(true, ContainLetter("A&")) + + assert.Equal(false, ContainLetter("")) + assert.Equal(false, ContainLetter("123")) + assert.Equal(false, ContainLetter("你好")) + assert.Equal(false, ContainLetter("&@#$%^&*")) +} + +func TestIsJSON(t *testing.T) { + assert := internal.NewAssert(t, "TestIsJSON") + + assert.Equal(true, IsJSON("{}")) + assert.Equal(true, IsJSON("{\"name\": \"test\"}")) + assert.Equal(true, IsJSON("[]")) + assert.Equal(true, IsJSON("123")) + + assert.Equal(false, IsJSON("")) + assert.Equal(false, IsJSON("abc")) + assert.Equal(false, IsJSON("你好")) + assert.Equal(false, IsJSON("&@#$%^&*")) +} + func TestIsNumberStr(t *testing.T) { assert := internal.NewAssert(t, "TestIsNumberStr") @@ -35,6 +125,18 @@ func TestIsIntStr(t *testing.T) { assert.Equal(false, IsIntStr("abc")) } +func TestIsPort(t *testing.T) { + assert := internal.NewAssert(t, "TestIsPort") + + assert.Equal(true, IsPort("1")) + assert.Equal(true, IsPort("65535")) + assert.Equal(false, IsPort("abc")) + assert.Equal(false, IsPort("123abc")) + assert.Equal(false, IsPort("")) + assert.Equal(false, IsPort("-1")) + assert.Equal(false, IsPort("65536")) +} + func TestIsIp(t *testing.T) { assert := internal.NewAssert(t, "TestIsIntStr")