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
5ed112e9
Commit
5ed112e9
authored
Feb 02, 2018
by
Andrea Spacca
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added CanSkipAlso
parent
453f1120
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
5 deletions
+59
-5
validation.go
validation/validation.go
+19
-3
validation_test.go
validation/validation_test.go
+40
-2
No files found.
validation/validation.go
View file @
5ed112e9
...
...
@@ -245,14 +245,13 @@ func (v *Validation) ZipCode(obj interface{}, key string) *Result {
}
func
(
v
*
Validation
)
apply
(
chk
Validator
,
obj
interface
{})
*
Result
{
validatorName
:=
reflect
.
TypeOf
(
chk
)
.
Name
()
if
nil
==
obj
{
if
chk
.
IsSatisfied
(
obj
)
{
return
&
Result
{
Ok
:
true
}
}
}
else
if
reflect
.
TypeOf
(
obj
)
.
Kind
()
==
reflect
.
Ptr
{
if
reflect
.
ValueOf
(
obj
)
.
IsNil
()
{
if
"Required"
!=
validatorName
{
if
chk
.
IsSatisfied
(
nil
)
{
return
&
Result
{
Ok
:
true
}
}
}
else
{
...
...
@@ -372,7 +371,18 @@ func (v *Validation) Valid(obj interface{}) (b bool, err error) {
hasReuired
=
true
}
if
!
hasReuired
&&
v
.
RequiredFirst
&&
len
(
objV
.
Field
(
i
)
.
String
())
==
0
{
currentField
:=
objV
.
Field
(
i
)
.
Interface
()
if
objV
.
Field
(
i
)
.
Kind
()
==
reflect
.
Ptr
{
if
objV
.
Field
(
i
)
.
IsNil
()
{
currentField
=
""
}
else
{
currentField
=
objV
.
Field
(
i
)
.
Elem
()
.
Interface
()
}
}
chk
:=
Required
{
""
}
.
IsSatisfied
(
currentField
)
if
!
hasReuired
&&
v
.
RequiredFirst
&&
!
chk
{
if
_
,
ok
:=
CanSkipFuncs
[
vf
.
Name
];
ok
{
continue
}
...
...
@@ -429,3 +439,9 @@ func (v *Validation) RecursiveValid(objc interface{}) (bool, error) {
}
return
pass
,
err
}
func
(
v
*
Validation
)
CanSkipAlso
(
skipFunc
string
)
{
if
_
,
ok
:=
CanSkipFuncs
[
skipFunc
];
!
ok
{
CanSkipFuncs
[
skipFunc
]
=
struct
{}{}
}
}
validation/validation_test.go
View file @
5ed112e9
...
...
@@ -490,8 +490,8 @@ func TestPointer(t *testing.T) {
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
!
b
{
t
.
Fatal
(
"validation should be passed"
)
if
b
{
t
.
Fatal
(
"validation should
not
be passed"
)
}
invalidEmail
:=
"a@a"
...
...
@@ -523,3 +523,41 @@ func TestPointer(t *testing.T) {
t
.
Fatal
(
"validation should not be passed"
)
}
}
func
TestCanSkipAlso
(
t
*
testing
.
T
)
{
type
User
struct
{
ID
int
Email
string
`valid:"Email"`
ReqEmail
string
`valid:"Required;Email"`
MatchRange
int
`valid:"Range(10, 20)"`
}
u
:=
User
{
ReqEmail
:
"a@a.com"
,
Email
:
""
,
MatchRange
:
0
,
}
valid
:=
Validation
{
RequiredFirst
:
true
}
b
,
err
:=
valid
.
Valid
(
u
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
b
{
t
.
Fatal
(
"validation should not be passed"
)
}
valid
=
Validation
{
RequiredFirst
:
true
}
valid
.
CanSkipAlso
(
"Range"
)
b
,
err
=
valid
.
Valid
(
u
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
!
b
{
t
.
Fatal
(
"validation should be passed"
)
}
}
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