Skip to content

Commit

Permalink
feat(validator): 添加 LanguageTag 和 Timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed May 27, 2024
1 parent 4bb74ee commit 4fa10cb
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 55 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/issue9/assert/v4 v4.3.0
github.com/issue9/version v1.0.8
github.com/issue9/web v0.96.1
golang.org/x/text v0.15.0
)

require (
Expand All @@ -28,6 +29,5 @@ require (
golang.org/x/mod v0.17.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
)
7 changes: 7 additions & 0 deletions validator/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/google/uuid"
"golang.org/x/text/language"

"github.com/issue9/webfilter/gb11643"
"github.com/issue9/webfilter/gb32100"
Expand Down Expand Up @@ -177,3 +178,9 @@ func UUID(val string) bool { return uuid.Validate(val) == nil }

// Empty 字符串是否为空
func Empty(val string) bool { return val == "" }

// LanguageTag 是否能解析为 [language.Tag] 变量
func LanguageTag(val string) bool {
_, err := language.Parse(val)
return err == nil
}
114 changes: 60 additions & 54 deletions validator/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@ import (
func TestHexColor(t *testing.T) {
a := assert.New(t, false)

a.True(HexColor("#123"))
a.True(HexColor("#fff"))
a.True(HexColor("#f0f0f0"))
a.True(HexColor("#fafafa"))
a.True(HexColor("#224422"))

a.False(HexColor("#2244"))
a.False(HexColor("#hhh"))
a.False(HexColor("ffff"))
a.False(HexColor("#asdf"))
a.False(HexColor("#ffff"))
a.True(HexColor("#123")).
True(HexColor("#fff")).
True(HexColor("#f0f0f0")).
True(HexColor("#fafafa")).
True(HexColor("#224422"))

a.False(HexColor("#2244")).
False(HexColor("#hhh")).
False(HexColor("ffff")).
False(HexColor("#asdf")).
False(HexColor("#ffff"))
}

func TestIP(t *testing.T) {
a := assert.New(t, false)

a.True(IP("fe80:0000:0000:0000:0204:61ff:fe9d:f156"))
a.True(IP("fe80:0:0:0:204:61ff:fe9d:f156"))
a.True(IP("0.0.0.0"))
a.True(IP("255.255.255.255"))
a.True(IP("255.0.3.255"))
a.True(IP("fe80:0000:0000:0000:0204:61ff:fe9d:f156")).
True(IP("fe80:0:0:0:204:61ff:fe9d:f156")).
True(IP("0.0.0.0")).
True(IP("255.255.255.255")).
True(IP("255.0.3.255"))

a.False(IP("255.0:3.255"))
a.False(IP("275.0.3.255"))
a.False(IP("255.0:3.255")).
False(IP("275.0.3.255"))
}

func TestIP6(t *testing.T) {
Expand All @@ -56,24 +56,24 @@ func TestIP6(t *testing.T) {
func TestIP4(t *testing.T) {
a := assert.New(t, false)

a.True(IP4("0.0.0.0"))
a.True(IP4("255.255.255.255"))
a.True(IP4("255.0.3.255"))
a.True(IP4("127.10.0.1"))
a.True(IP4("27.1.0.1"))
a.True(IP4("0.0.0.0")).
True(IP4("255.255.255.255")).
True(IP4("255.0.3.255")).
True(IP4("127.10.0.1")).
True(IP4("27.1.0.1"))

a.False(IP4("1127.01.0.1"))
}

func TestEmail(t *testing.T) {
a := assert.New(t, false)

a.True(Email("[email protected]"))
a.True(Email("[email protected]"))
a.True(Email("[email protected]"))
a.True(Email("[email protected]"))
a.True(Email("[email protected]"))
a.True(Email("em2il@email"))
a.True(Email("[email protected]")).
True(Email("[email protected]")).
True(Email("[email protected]")).
True(Email("[email protected]")).
True(Email("[email protected]")).
True(Email("em2il@email"))

// 2个@
a.False(Email("em@[email protected]"))
Expand All @@ -84,44 +84,50 @@ func TestEmail(t *testing.T) {
func TestURL(t *testing.T) {
a := assert.New(t, false)

a.True(URL("http://www.example.com"))
a.True(URL("http://www.example.com/path/?a=b"))
a.True(URL("https://www.example.com:88/path1/path2"))
a.True(URL("ftp://pwd:[email protected]/index.go?a=b"))
a.True(URL("pwd:[email protected]/path/"))
a.True(URL("https://[fe80:0:0:0:204:61ff:fe9d:f156]/path/"))
a.True(URL("https://127.0.0.1/path//index.go?arg1=val1&arg2=val/2"))
a.True(URL("https://[::1]/path/index.go?arg1=val1"))
a.True(URL("http://www.example.com")).
True(URL("http://www.example.com/path/?a=b")).
True(URL("https://www.example.com:88/path1/path2")).
True(URL("ftp://pwd:[email protected]/index.go?a=b")).
True(URL("pwd:[email protected]/path/")).
True(URL("https://[fe80:0:0:0:204:61ff:fe9d:f156]/path/")).
True(URL("https://127.0.0.1/path//index.go?arg1=val1&arg2=val/2")).
True(URL("https://[::1]/path/index.go?arg1=val1"))
}

func TestASCII(t *testing.T) {
a := assert.New(t, false)

a.True(ASCII("abc"))
a.False(ASCII("\u1000"))
a.True(ASCII("abc")).
False(ASCII("\u1000"))
}

func TestAlpha(t *testing.T) {
a := assert.New(t, false)

a.False(Alpha("12345"))
a.True(Alpha("abc"))
a.False(Alpha("abc12"))
a.False(Alpha("12345")).
True(Alpha("abc")).
False(Alpha("abc12"))
}

func TestCNMobile(t *testing.T) {
a := assert.New(t, false)

a.True(CNMobile("15011111111"))
a.True(CNMobile("015011111111"))
a.True(CNMobile("8615011111111"))
a.True(CNMobile("+8615011111111"))
a.True(CNMobile("+8619911111111"))

a.False(CNMobile("+86150111111112")) // 尾部多个2
a.False(CNMobile("50111111112")) // 开头少1
a.False(CNMobile("+8650111111112")) // 开头少1
a.False(CNMobile("8650111111112")) // 开头少1
a.False(CNMobile("154111111112")) // 不存在的前缀154
a.True(CNMobile("15411111111"))
a.True(CNMobile("15011111111")).
True(CNMobile("015011111111")).
True(CNMobile("8615011111111")).
True(CNMobile("+8615011111111")).
True(CNMobile("+8619911111111"))

a.False(CNMobile("+86150111111112")). // 尾部多个2
False(CNMobile("50111111112")). // 开头少1
False(CNMobile("+8650111111112")). // 开头少1
False(CNMobile("8650111111112")). // 开头少1
False(CNMobile("154111111112")). // 不存在的前缀154
True(CNMobile("15411111111"))
}

func TestLanguageTag(t *testing.T) {
a := assert.New(t, false)

a.True(LanguageTag("zh-CN")).False(LanguageTag("xxxx"))
}
13 changes: 13 additions & 0 deletions validator/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2024 caixw
//
// SPDX-License-Identifier: MIT

package validator

import "time"

// Timezone 是否为一个正确的时区变量
func Timezone(tz string) bool {
_, err := time.LoadLocation(tz)
return err == nil
}
18 changes: 18 additions & 0 deletions validator/time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-FileCopyrightText: 2024 caixw
//
// SPDX-License-Identifier: MIT

package validator

import (
"testing"

"github.com/issue9/assert/v4"
)

func TestTimezone(t *testing.T) {
a := assert.New(t, false)
a.True(Timezone("Local")).
True(Timezone("America/New_York")).
False(Timezone("zzzz"))
}

0 comments on commit 4fa10cb

Please sign in to comment.