Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
f4901412
Commit
f4901412
authored
Jul 27, 2013
by
miraclesu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add some validate functions
parent
0e748c68
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
150 additions
and
0 deletions
+150
-0
README.md
validation/README.md
+4
-0
validation.go
validation/validation.go
+17
-0
validation_test.go
validation/validation_test.go
+62
-0
validators.go
validation/validators.go
+67
-0
No files found.
validation/README.md
View file @
f4901412
...
...
@@ -92,6 +92,10 @@ Struct Tag Functions:
Email
IP
Base64
Mobile
Tel
Phone
ZipCode
## LICENSE
...
...
validation/validation.go
View file @
f4901412
...
...
@@ -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
View file @
f4901412
...
...
@@ -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
View file @
f4901412
...
...
@@ -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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment