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
dcdfaf36
authored
2013-07-28 16:59:35 +0800
by
miraclesu
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Accept parameters more types
1 parent
ae7e3171
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
30 deletions
validation/validation.go
validation/validation_test.go
validation/validators.go
validation/validation.go
View file @
dcdfaf3
...
...
@@ -84,16 +84,19 @@ func (v *Validation) Required(obj interface{}, key string) *ValidationResult {
return
v
.
apply
(
Required
{
key
},
obj
)
}
func
(
v
*
Validation
)
Min
(
n
int
,
min
int
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Min
{
min
,
key
},
n
)
// Test that the obj is greater than min if obj's type is int
func
(
v
*
Validation
)
Min
(
obj
interface
{},
min
int
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Min
{
min
,
key
},
obj
)
}
func
(
v
*
Validation
)
Max
(
n
int
,
max
int
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Max
{
max
,
key
},
n
)
// Test that the obj is less than max if obj's type is int
func
(
v
*
Validation
)
Max
(
obj
interface
{},
max
int
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Max
{
max
,
key
},
obj
)
}
func
(
v
*
Validation
)
Range
(
n
,
min
,
max
int
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Range
{
Min
{
Min
:
min
},
Max
{
Max
:
max
},
key
},
n
)
// Test that the obj is between mni and max if obj's type is int
func
(
v
*
Validation
)
Range
(
obj
interface
{},
min
,
max
int
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Range
{
Min
{
Min
:
min
},
Max
{
Max
:
max
},
key
},
obj
)
}
func
(
v
*
Validation
)
MinSize
(
obj
interface
{},
min
int
,
key
string
)
*
ValidationResult
{
...
...
@@ -120,45 +123,45 @@ func (v *Validation) AlphaNumeric(obj interface{}, key string) *ValidationResult
return
v
.
apply
(
AlphaNumeric
{
key
},
obj
)
}
func
(
v
*
Validation
)
Match
(
str
string
,
regex
*
regexp
.
Regexp
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Match
{
regex
,
key
},
str
)
func
(
v
*
Validation
)
Match
(
obj
interface
{}
,
regex
*
regexp
.
Regexp
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Match
{
regex
,
key
},
obj
)
}
func
(
v
*
Validation
)
NoMatch
(
str
string
,
regex
*
regexp
.
Regexp
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
NoMatch
{
Match
{
Regexp
:
regex
},
key
},
str
)
func
(
v
*
Validation
)
NoMatch
(
obj
interface
{}
,
regex
*
regexp
.
Regexp
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
NoMatch
{
Match
{
Regexp
:
regex
},
key
},
obj
)
}
func
(
v
*
Validation
)
AlphaDash
(
str
string
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
AlphaDash
{
NoMatch
{
Match
:
Match
{
Regexp
:
alphaDashPattern
}},
key
},
str
)
func
(
v
*
Validation
)
AlphaDash
(
obj
interface
{}
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
AlphaDash
{
NoMatch
{
Match
:
Match
{
Regexp
:
alphaDashPattern
}},
key
},
obj
)
}
func
(
v
*
Validation
)
Email
(
str
string
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Email
{
Match
{
Regexp
:
emailPattern
},
key
},
str
)
func
(
v
*
Validation
)
Email
(
obj
interface
{}
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Email
{
Match
{
Regexp
:
emailPattern
},
key
},
obj
)
}
func
(
v
*
Validation
)
IP
(
str
string
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
IP
{
Match
{
Regexp
:
ipPattern
},
key
},
str
)
func
(
v
*
Validation
)
IP
(
obj
interface
{}
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
IP
{
Match
{
Regexp
:
ipPattern
},
key
},
obj
)
}
func
(
v
*
Validation
)
Base64
(
str
string
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Base64
{
Match
{
Regexp
:
base64Pattern
},
key
},
str
)
func
(
v
*
Validation
)
Base64
(
obj
interface
{}
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Base64
{
Match
{
Regexp
:
base64Pattern
},
key
},
obj
)
}
func
(
v
*
Validation
)
Mobile
(
str
string
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Mobile
{
Match
{
Regexp
:
mobilePattern
},
key
},
str
)
func
(
v
*
Validation
)
Mobile
(
obj
interface
{}
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Mobile
{
Match
{
Regexp
:
mobilePattern
},
key
},
obj
)
}
func
(
v
*
Validation
)
Tel
(
str
string
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Tel
{
Match
{
Regexp
:
telPattern
},
key
},
str
)
func
(
v
*
Validation
)
Tel
(
obj
interface
{}
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Tel
{
Match
{
Regexp
:
telPattern
},
key
},
obj
)
}
func
(
v
*
Validation
)
Phone
(
str
string
,
key
string
)
*
ValidationResult
{
func
(
v
*
Validation
)
Phone
(
obj
interface
{}
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
Phone
{
Mobile
{
Match
:
Match
{
Regexp
:
mobilePattern
}},
Tel
{
Match
:
Match
{
Regexp
:
telPattern
}},
key
},
str
)
Tel
{
Match
:
Match
{
Regexp
:
telPattern
}},
key
},
obj
)
}
func
(
v
*
Validation
)
ZipCode
(
str
string
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
ZipCode
{
Match
{
Regexp
:
zipCodePattern
},
key
},
str
)
func
(
v
*
Validation
)
ZipCode
(
obj
interface
{}
,
key
string
)
*
ValidationResult
{
return
v
.
apply
(
ZipCode
{
Match
{
Regexp
:
zipCodePattern
},
key
},
obj
)
}
func
(
v
*
Validation
)
apply
(
chk
Validator
,
obj
interface
{})
*
ValidationResult
{
...
...
validation/validation_test.go
View file @
dcdfaf3
...
...
@@ -61,10 +61,10 @@ func TestRange(t *testing.T) {
valid
:=
Validation
{}
if
valid
.
Range
(
-
1
,
0
,
1
,
"range0_1"
)
.
Ok
{
t
.
Error
(
"-1 is bet
t
ween 0 and 1 should be false"
)
t
.
Error
(
"-1 is between 0 and 1 should be false"
)
}
if
!
valid
.
Range
(
1
,
0
,
1
,
"range0_1"
)
.
Ok
{
t
.
Error
(
"1 is bet
t
ween 0 and 1 should be true"
)
t
.
Error
(
"1 is between 0 and 1 should be true"
)
}
}
...
...
validation/validators.go
View file @
dcdfaf3
...
...
@@ -264,8 +264,7 @@ type Match struct {
}
func
(
m
Match
)
IsSatisfied
(
obj
interface
{})
bool
{
str
:=
obj
.
(
string
)
return
m
.
Regexp
.
MatchString
(
str
)
return
m
.
Regexp
.
MatchString
(
fmt
.
Sprintf
(
"%v"
,
obj
))
}
func
(
m
Match
)
DefaultMessage
()
string
{
...
...
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