mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
feat: add validator functions, IsAllUpper, IsAllLower, ContainUpper, ContainLower, ContainLetter, IsJSON and IsPort
This commit is contained in:
@@ -5,17 +5,72 @@
|
|||||||
package validator
|
package validator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"net"
|
"net"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
var isAlphaRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
|
var isAlphaRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
|
||||||
|
|
||||||
// IsAlpha checks if the string contains only letters (a-zA-Z)
|
// IsAlpha checks if the string contains only letters (a-zA-Z)
|
||||||
func IsAlpha(s string) bool {
|
func IsAlpha(str string) bool {
|
||||||
return isAlphaRegexMatcher.MatchString(s)
|
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.
|
// 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.
|
// IsFloatStr check if the string can convert to a float.
|
||||||
func IsFloatStr(s string) bool {
|
func IsFloatStr(str string) bool {
|
||||||
_, e := strconv.ParseFloat(s, 64)
|
_, e := strconv.ParseFloat(str, 64)
|
||||||
return e == nil
|
return e == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var isIntStrRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
|
var isIntStrRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
|
||||||
|
|
||||||
// IsIntStr check if the string can convert to a integer.
|
// IsIntStr check if the string can convert to a integer.
|
||||||
func IsIntStr(s string) bool {
|
func IsIntStr(str string) bool {
|
||||||
return isIntStrRegexMatcher.MatchString(s)
|
return isIntStrRegexMatcher.MatchString(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsIp check if the string is a ip address.
|
// IsIp check if the string is a ip address.
|
||||||
@@ -48,13 +103,7 @@ func IsIpV4(ipstr string) bool {
|
|||||||
if ip == nil {
|
if ip == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for i := 0; i < len(ipstr); i++ {
|
return strings.Contains(ipstr, ".")
|
||||||
switch ipstr[i] {
|
|
||||||
case '.':
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsIpV6 check if the string is a ipv6 address.
|
// IsIpV6 check if the string is a ipv6 address.
|
||||||
@@ -63,11 +112,13 @@ func IsIpV6(ipstr string) bool {
|
|||||||
if ip == nil {
|
if ip == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for i := 0; i < len(ipstr); i++ {
|
return strings.Contains(ipstr, ":")
|
||||||
switch ipstr[i] {
|
}
|
||||||
case ':':
|
|
||||||
return true
|
// 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
|
return false
|
||||||
}
|
}
|
||||||
@@ -130,14 +181,14 @@ func IsBase64(base64 string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IsEmptyString check if the string is empty.
|
// IsEmptyString check if the string is empty.
|
||||||
func IsEmptyString(s string) bool {
|
func IsEmptyString(str string) bool {
|
||||||
return len(s) == 0
|
return len(str) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRegexMatch check if the string match the regexp
|
// IsRegexMatch check if the string match the regexp
|
||||||
func IsRegexMatch(s, regex string) bool {
|
func IsRegexMatch(str, regex string) bool {
|
||||||
reg := regexp.MustCompile(regex)
|
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
|
// IsStrongPassword check if the string is strong password, if len(password) is less than the length param, return false
|
||||||
|
|||||||
@@ -6,6 +6,96 @@ import (
|
|||||||
"github.com/duke-git/lancet/internal"
|
"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) {
|
func TestIsNumberStr(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestIsNumberStr")
|
assert := internal.NewAssert(t, "TestIsNumberStr")
|
||||||
|
|
||||||
@@ -35,6 +125,18 @@ func TestIsIntStr(t *testing.T) {
|
|||||||
assert.Equal(false, IsIntStr("abc"))
|
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) {
|
func TestIsIp(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestIsIntStr")
|
assert := internal.NewAssert(t, "TestIsIntStr")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user