Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
f4901412
authored
2013-07-27 20:40:15 +0800
by
miraclesu
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Add some validate functions
1 parent
0e748c68
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
150 additions
and
0 deletions
validation/README.md
validation/validation.go
validation/validation_test.go
validation/validators.go
validation/README.md
View file @
f490141
...
...
@@ -92,6 +92,10 @@ Struct Tag Functions:
Email
IP
Base64
Mobile
Tel
Phone
ZipCode
## LICENSE
...
...
validation/validation.go
View file @
f490141
...
...
@@ -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 @
f490141
...
...
@@ -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 @
f490141
...
...
@@ -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
Styling with
Markdown
is supported
Attach a file
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 post a comment