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
2231841d
Commit
2231841d
authored
Jun 19, 2017
by
miraclesu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
validation: support int64 int32 int16 and int8 type
parent
805a6748
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
62 additions
and
18 deletions
+62
-18
util.go
validation/util.go
+20
-0
validation.go
validation/validation.go
+3
-3
validation_test.go
validation/validation_test.go
+4
-4
validators.go
validation/validators.go
+35
-11
No files found.
validation/util.go
View file @
2231841d
...
...
@@ -249,6 +249,26 @@ func parseParam(t reflect.Type, s string) (i interface{}, err error) {
switch
t
.
Kind
()
{
case
reflect
.
Int
:
i
,
err
=
strconv
.
Atoi
(
s
)
case
reflect
.
Int64
:
i
,
err
=
strconv
.
ParseInt
(
s
,
10
,
64
)
case
reflect
.
Int32
:
var
v
int64
v
,
err
=
strconv
.
ParseInt
(
s
,
10
,
32
)
if
err
==
nil
{
i
=
int32
(
v
)
}
case
reflect
.
Int16
:
var
v
int64
v
,
err
=
strconv
.
ParseInt
(
s
,
10
,
16
)
if
err
==
nil
{
i
=
int16
(
v
)
}
case
reflect
.
Int8
:
var
v
int64
v
,
err
=
strconv
.
ParseInt
(
s
,
10
,
8
)
if
err
==
nil
{
i
=
int8
(
v
)
}
case
reflect
.
String
:
i
=
s
case
reflect
.
Ptr
:
...
...
validation/validation.go
View file @
2231841d
...
...
@@ -144,17 +144,17 @@ func (v *Validation) Required(obj interface{}, key string) *Result {
}
// Min Test that the obj is greater than min if obj's type is int
func
(
v
*
Validation
)
Min
(
obj
interface
{},
min
int
,
key
string
)
*
Result
{
func
(
v
*
Validation
)
Min
(
obj
interface
{},
min
int
64
,
key
string
)
*
Result
{
return
v
.
apply
(
Min
{
min
,
key
},
obj
)
}
// Max Test that the obj is less than max if obj's type is int
func
(
v
*
Validation
)
Max
(
obj
interface
{},
max
int
,
key
string
)
*
Result
{
func
(
v
*
Validation
)
Max
(
obj
interface
{},
max
int
64
,
key
string
)
*
Result
{
return
v
.
apply
(
Max
{
max
,
key
},
obj
)
}
// Range 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
)
*
Result
{
func
(
v
*
Validation
)
Range
(
obj
interface
{},
min
,
max
int
64
,
key
string
)
*
Result
{
return
v
.
apply
(
Range
{
Min
{
Min
:
min
},
Max
{
Max
:
max
},
key
},
obj
)
}
...
...
validation/validation_test.go
View file @
2231841d
...
...
@@ -64,10 +64,10 @@ func TestRequired(t *testing.T) {
func
TestMin
(
t
*
testing
.
T
)
{
valid
:=
Validation
{}
if
valid
.
Min
(
-
1
,
0
,
"min0"
)
.
Ok
{
if
valid
.
Min
(
int64
(
-
1
),
int64
(
0
)
,
"min0"
)
.
Ok
{
t
.
Error
(
"-1 is less than the minimum value of 0 should be false"
)
}
if
!
valid
.
Min
(
1
,
0
,
"min0"
)
.
Ok
{
if
!
valid
.
Min
(
int64
(
1
),
int64
(
0
)
,
"min0"
)
.
Ok
{
t
.
Error
(
"1 is greater or equal than the minimum value of 0 should be true"
)
}
}
...
...
@@ -75,10 +75,10 @@ func TestMin(t *testing.T) {
func
TestMax
(
t
*
testing
.
T
)
{
valid
:=
Validation
{}
if
valid
.
Max
(
1
,
0
,
"max0"
)
.
Ok
{
if
valid
.
Max
(
int64
(
1
),
int64
(
0
)
,
"max0"
)
.
Ok
{
t
.
Error
(
"1 is greater than the minimum value of 0 should be false"
)
}
if
!
valid
.
Max
(
-
1
,
0
,
"max0"
)
.
Ok
{
if
!
valid
.
Max
(
int64
(
-
1
),
int64
(
0
)
,
"max0"
)
.
Ok
{
t
.
Error
(
"-1 is less or equal than the maximum value of 0 should be true"
)
}
}
...
...
validation/validators.go
View file @
2231841d
...
...
@@ -161,17 +161,29 @@ func (r Required) GetLimitValue() interface{} {
// Min check struct
type
Min
struct
{
Min
int
Min
int
64
Key
string
}
// IsSatisfied judge whether obj is valid
func
(
m
Min
)
IsSatisfied
(
obj
interface
{})
bool
{
num
,
ok
:=
obj
.
(
int
)
if
ok
{
return
num
>=
m
.
Min
var
v
int64
switch
obj
.
(
type
)
{
case
int64
:
v
=
obj
.
(
int64
)
case
int
:
v
=
int64
(
obj
.
(
int
))
case
int32
:
v
=
int64
(
obj
.
(
int32
))
case
int16
:
v
=
int64
(
obj
.
(
int16
))
case
int8
:
v
=
int64
(
obj
.
(
int8
))
default
:
return
false
}
return
false
return
v
>=
m
.
Min
}
// DefaultMessage return the default min error message
...
...
@@ -191,17 +203,29 @@ func (m Min) GetLimitValue() interface{} {
// Max validate struct
type
Max
struct
{
Max
int
Max
int
64
Key
string
}
// IsSatisfied judge whether obj is valid
func
(
m
Max
)
IsSatisfied
(
obj
interface
{})
bool
{
num
,
ok
:=
obj
.
(
int
)
if
ok
{
return
num
<=
m
.
Max
var
v
int64
switch
obj
.
(
type
)
{
case
int64
:
v
=
obj
.
(
int64
)
case
int
:
v
=
int64
(
obj
.
(
int
))
case
int32
:
v
=
int64
(
obj
.
(
int32
))
case
int16
:
v
=
int64
(
obj
.
(
int16
))
case
int8
:
v
=
int64
(
obj
.
(
int8
))
default
:
return
false
}
return
false
return
v
<=
m
.
Max
}
// DefaultMessage return the default max error message
...
...
@@ -243,7 +267,7 @@ func (r Range) GetKey() string {
// GetLimitValue return the limit value, Max
func
(
r
Range
)
GetLimitValue
()
interface
{}
{
return
[]
int
{
r
.
Min
.
Min
,
r
.
Max
.
Max
}
return
[]
int
64
{
r
.
Min
.
Min
,
r
.
Max
.
Max
}
}
// MinSize Requires an array or string to be at least a given length.
...
...
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