Skip to content

Commit

Permalink
feat: add ContainNumber for validator (#97)
Browse files Browse the repository at this point in the history
Co-authored-by: sunyaoyao <sunyaoyao@kezaihui.com>
  • Loading branch information
googoo-s and sunyaoyao committed May 19, 2023
1 parent 3beb769 commit 78aa679
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
7 changes: 7 additions & 0 deletions validator/validator.go
Expand Up @@ -18,6 +18,7 @@ import (
var (
alphaMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
letterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`)
numberRegexMatcher *regexp.Regexp = regexp.MustCompile(`\d`)
intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
urlMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`)
dnsMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`)
Expand Down Expand Up @@ -111,6 +112,12 @@ func ContainLetter(str string) bool {
return letterRegexMatcher.MatchString(str)
}


// ContainLetter check if the string contain at least one number.
func ContainNumber(input string) bool {
return numberRegexMatcher.MatchString(input)
}

// IsJSON checks if the string is valid JSON.
// Play: https://go.dev/play/p/8Kip1Itjiil
func IsJSON(str string) bool {
Expand Down
18 changes: 18 additions & 0 deletions validator/validator_example_test.go
Expand Up @@ -36,6 +36,24 @@ func ExampleContainLetter() {
// true
}

func ExampleContainNumber() {
result1 := ContainNumber("你好")
result2 := ContainNumber("&@#$%^&*")
result3 := ContainNumber("ab1")
result4 := ContainNumber("1234")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)

// Output:
// false
// false
// true
// true
}

func ExampleContainLower() {
result1 := ContainLower("abc")
result2 := ContainLower("aBC")
Expand Down
17 changes: 17 additions & 0 deletions validator/validator_test.go
Expand Up @@ -86,6 +86,23 @@ func TestContainLetter(t *testing.T) {
assert.Equal(false, ContainLetter("&@#$%^&*"))
}

func TestContainNumber(t *testing.T) {
assert := internal.NewAssert(t, "TestContainNumber")

assert.Equal(true, ContainNumber("123"))
assert.Equal(true, ContainNumber("1Bc"))
assert.Equal(true, ContainNumber("a2c"))
assert.Equal(true, ContainNumber("ab3"))
assert.Equal(true, ContainNumber("a23"))
assert.Equal(true, ContainNumber("a23c"))
assert.Equal(true, ContainNumber("1%%%"))

assert.Equal(false, ContainNumber("ABC"))
assert.Equal(false, ContainNumber(""))
assert.Equal(false, ContainNumber("你好"))
assert.Equal(false, ContainNumber("&@#$%^&*"))
}

func TestIsJSON(t *testing.T) {
assert := internal.NewAssert(t, "TestIsJSON")

Expand Down

0 comments on commit 78aa679

Please sign in to comment.