-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validator): 添加 LanguageTag 和 Timezone
- Loading branch information
Showing
5 changed files
with
99 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -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]")) | ||
|
@@ -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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |