Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
1d7d6c6f
B
beego
项目概览
二次元的罪恶王冠
/
beego
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
beego
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1d7d6c6f
编写于
7月 27, 2013
作者:
A
astaxie
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #109 from miraclesu/valid
Add some validate functions
上级
0e748c68
f4901412
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
150 addition
and
0 deletion
+150
-0
validation/README.md
validation/README.md
+4
-0
validation/validation.go
validation/validation.go
+17
-0
validation/validation_test.go
validation/validation_test.go
+62
-0
validation/validators.go
validation/validators.go
+67
-0
未找到文件。
validation/README.md
浏览文件 @
1d7d6c6f
...
...
@@ -92,6 +92,10 @@ Struct Tag Functions:
Email
IP
Base64
Mobile
Tel
Phone
ZipCode
## LICENSE
...
...
validation/validation.go
浏览文件 @
1d7d6c6f
...
...
@@ -144,6 +144,23 @@ func (v *Validation) Base64(str string, key string) *ValidationResult {
return
v
.
apply
(
Base64
{
Match
{
Regexp
:
base64Pattern
},
key
},
str
)
}
func
(
v
*
Validation
)
Mobile
(
str
string
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Mobile
{
Match
{
Regexp
:
mobilePattern
},
key
},
str
)
}
func
(
v
*
Validation
)
Tel
(
str
string
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Tel
{
Match
{
Regexp
:
telPattern
},
key
},
str
)
}
func
(
v
*
Validation
)
Phone
(
str
string
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Phone
{
Mobile
{
Match
:
Match
{
Regexp
:
mobilePattern
}},
Tel
{
Match
:
Match
{
Regexp
:
telPattern
}},
key
},
str
)
}
func
(
v
*
Validation
)
ZipCode
(
str
string
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
ZipCode
{
Match
{
Regexp
:
zipCodePattern
},
key
},
str
)
}
func
(
v
*
Validation
)
apply
(
chk
Validator
,
obj
interface
{})
*
ValidationResult
{
if
chk
.
IsSatisfied
(
obj
)
{
return
&
ValidationResult
{
Ok
:
true
}
...
...
validation/validation_test.go
浏览文件 @
1d7d6c6f
...
...
@@ -218,6 +218,68 @@ func TestBase64(t *testing.T) {
}
}
func
TestMobile
(
t
*
testing
.
T
)
{
valid
:=
Validation
{}
if
valid
.
Mobile
(
"19800008888"
,
"mobile"
)
.
Ok
{
t
.
Error
(
"
\"
19800008888
\"
is a valid mobile phone number should be false"
)
}
if
!
valid
.
Mobile
(
"18800008888"
,
"mobile"
)
.
Ok
{
t
.
Error
(
"
\"
18800008888
\"
is a valid mobile phone number should be true"
)
}
if
!
valid
.
Mobile
(
"18000008888"
,
"mobile"
)
.
Ok
{
t
.
Error
(
"
\"
18000008888
\"
is a valid mobile phone number should be true"
)
}
if
!
valid
.
Mobile
(
"8618300008888"
,
"mobile"
)
.
Ok
{
t
.
Error
(
"
\"
8618300008888
\"
is a valid mobile phone number should be true"
)
}
if
!
valid
.
Mobile
(
"+8614700008888"
,
"mobile"
)
.
Ok
{
t
.
Error
(
"
\"
+8614700008888
\"
is a valid mobile phone number should be true"
)
}
}
func
TestTel
(
t
*
testing
.
T
)
{
valid
:=
Validation
{}
if
valid
.
Tel
(
"222-00008888"
,
"telephone"
)
.
Ok
{
t
.
Error
(
"
\"
222-00008888
\"
is a valid telephone number should be false"
)
}
if
!
valid
.
Tel
(
"022-70008888"
,
"telephone"
)
.
Ok
{
t
.
Error
(
"
\"
022-70008888
\"
is a valid telephone number should be true"
)
}
if
!
valid
.
Tel
(
"02270008888"
,
"telephone"
)
.
Ok
{
t
.
Error
(
"
\"
02270008888
\"
is a valid telephone number should be true"
)
}
if
!
valid
.
Tel
(
"70008888"
,
"telephone"
)
.
Ok
{
t
.
Error
(
"
\"
70008888
\"
is a valid telephone number should be true"
)
}
}
func
TestPhone
(
t
*
testing
.
T
)
{
valid
:=
Validation
{}
if
valid
.
Phone
(
"222-00008888"
,
"phone"
)
.
Ok
{
t
.
Error
(
"
\"
222-00008888
\"
is a valid phone number should be false"
)
}
if
!
valid
.
Mobile
(
"+8614700008888"
,
"phone"
)
.
Ok
{
t
.
Error
(
"
\"
+8614700008888
\"
is a valid phone number should be true"
)
}
if
!
valid
.
Tel
(
"02270008888"
,
"phone"
)
.
Ok
{
t
.
Error
(
"
\"
02270008888
\"
is a valid phone number should be true"
)
}
}
func
TestZipCode
(
t
*
testing
.
T
)
{
valid
:=
Validation
{}
if
valid
.
ZipCode
(
""
,
"zipcode"
)
.
Ok
{
t
.
Error
(
"
\"
00008888
\"
is a valid zipcode should be false"
)
}
if
!
valid
.
ZipCode
(
"536000"
,
"zipcode"
)
.
Ok
{
t
.
Error
(
"
\"
536000
\"
is a valid zipcode should be true"
)
}
}
func
TestValid
(
t
*
testing
.
T
)
{
type
user
struct
{
Id
int
...
...
validation/validators.go
浏览文件 @
1d7d6c6f
...
...
@@ -353,3 +353,70 @@ func (b Base64) DefaultMessage() string {
func
(
b
Base64
)
GetKey
()
string
{
return
b
.
Key
}
// just for chinese mobile phone number
var
mobilePattern
=
regexp
.
MustCompile
(
"^((
\\
+86)|(86))?(1(([35][0-9])|(47)|[8][01236789]))
\\
d{8}$"
)
type
Mobile
struct
{
Match
Key
string
}
func
(
m
Mobile
)
DefaultMessage
()
string
{
return
fmt
.
Sprint
(
"Must be valid mobile number"
)
}
func
(
m
Mobile
)
GetKey
()
string
{
return
m
.
Key
}
// just for chinese telephone number
var
telPattern
=
regexp
.
MustCompile
(
"^(0
\\
d{2,3}(
\\
-)?)?
\\
d{7,8}$"
)
type
Tel
struct
{
Match
Key
string
}
func
(
t
Tel
)
DefaultMessage
()
string
{
return
fmt
.
Sprint
(
"Must be valid telephone number"
)
}
func
(
t
Tel
)
GetKey
()
string
{
return
t
.
Key
}
// just for chinese telephone or mobile phone number
type
Phone
struct
{
Mobile
Tel
Key
string
}
func
(
p
Phone
)
IsSatisfied
(
obj
interface
{})
bool
{
return
p
.
Mobile
.
IsSatisfied
(
obj
)
||
p
.
Tel
.
IsSatisfied
(
obj
)
}
func
(
p
Phone
)
DefaultMessage
()
string
{
return
fmt
.
Sprint
(
"Must be valid telephone or mobile phone number"
)
}
func
(
p
Phone
)
GetKey
()
string
{
return
p
.
Key
}
// just for chinese zipcode
var
zipCodePattern
=
regexp
.
MustCompile
(
"^[1-9]
\\
d{5}$"
)
type
ZipCode
struct
{
Match
Key
string
}
func
(
z
ZipCode
)
DefaultMessage
()
string
{
return
fmt
.
Sprint
(
"Must be valid zipcode"
)
}
func
(
z
ZipCode
)
GetKey
()
string
{
return
z
.
Key
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录