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
4cfb3678
Commit
4cfb3678
authored
Jul 04, 2017
by
astaxie
Committed by
GitHub
Jul 04, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2741 from miraclesu/validation
validation: support required option for some struct tag valids
parents
82586c70
e72b02b7
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
81 additions
and
0 deletions
+81
-0
README.md
validation/README.md
+3
-0
validation.go
validation/validation.go
+17
-0
validation_test.go
validation/validation_test.go
+51
-0
validators.go
validation/validators.go
+10
-0
No files found.
validation/README.md
View file @
4cfb3678
...
...
@@ -64,6 +64,9 @@ Struct Tag Use:
func main() {
valid := validation.Validation{}
// ignore empty field valid
// see CanSkipFuncs
// valid := validation.Validation{RequiredFirst:true}
u := user{Name: "test", Age: 40}
b, err := valid.Valid(u)
if err != nil {
...
...
validation/validation.go
View file @
4cfb3678
...
...
@@ -106,6 +106,11 @@ func (r *Result) Message(message string, args ...interface{}) *Result {
// A Validation context manages data validation and error messages.
type
Validation
struct
{
// if this field set true, in struct tag valid
// if the struct field vale is empty
// it will skip those valid functions, see CanSkipFuncs
RequiredFirst
bool
Errors
[]
*
Error
ErrorsMap
map
[
string
]
*
Error
}
...
...
@@ -324,7 +329,19 @@ func (v *Validation) Valid(obj interface{}) (b bool, err error) {
if
vfs
,
err
=
getValidFuncs
(
objT
.
Field
(
i
));
err
!=
nil
{
return
}
var
hasReuired
bool
for
_
,
vf
:=
range
vfs
{
if
vf
.
Name
==
"Required"
{
hasReuired
=
true
}
if
!
hasReuired
&&
v
.
RequiredFirst
&&
len
(
objV
.
Field
(
i
)
.
String
())
==
0
{
if
_
,
ok
:=
CanSkipFuncs
[
vf
.
Name
];
ok
{
continue
}
}
if
_
,
err
=
funcs
.
Call
(
vf
.
Name
,
mergeParam
(
v
,
objV
.
Field
(
i
)
.
Interface
(),
vf
.
Params
)
...
);
err
!=
nil
{
return
...
...
validation/validation_test.go
View file @
4cfb3678
...
...
@@ -391,3 +391,54 @@ func TestRecursiveValid(t *testing.T) {
t
.
Error
(
"validation should not be passed"
)
}
}
func
TestSkipValid
(
t
*
testing
.
T
)
{
type
User
struct
{
ID
int
Email
string
`valid:"Email"`
ReqEmail
string
`valid:"Required;Email"`
IP
string
`valid:"IP"`
ReqIP
string
`valid:"Required;IP"`
Mobile
string
`valid:"Mobile"`
ReqMobile
string
`valid:"Required;Mobile"`
Tel
string
`valid:"Tel"`
ReqTel
string
`valid:"Required;Tel"`
Phone
string
`valid:"Phone"`
ReqPhone
string
`valid:"Required;Phone"`
ZipCode
string
`valid:"ZipCode"`
ReqZipCode
string
`valid:"Required;ZipCode"`
}
u
:=
User
{
ReqEmail
:
"a@a.com"
,
ReqIP
:
"127.0.0.1"
,
ReqMobile
:
"18888888888"
,
ReqTel
:
"02088888888"
,
ReqPhone
:
"02088888888"
,
ReqZipCode
:
"510000"
,
}
valid
:=
Validation
{}
b
,
err
:=
valid
.
Valid
(
u
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
b
{
t
.
Fatal
(
"validation should not be passed"
)
}
valid
=
Validation
{
RequiredFirst
:
true
}
b
,
err
=
valid
.
Valid
(
u
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
!
b
{
t
.
Fatal
(
"validation should be passed"
)
}
}
validation/validators.go
View file @
4cfb3678
...
...
@@ -23,6 +23,16 @@ import (
"unicode/utf8"
)
// CanSkipFuncs will skip valid if RequiredFirst is true and the struct field's value is empty
var
CanSkipFuncs
=
map
[
string
]
struct
{}{
"Email"
:
struct
{}{},
"IP"
:
struct
{}{},
"Mobile"
:
struct
{}{},
"Tel"
:
struct
{}{},
"Phone"
:
struct
{}{},
"ZipCode"
:
struct
{}{},
}
// MessageTmpls store commond validate template
var
MessageTmpls
=
map
[
string
]
string
{
"Required"
:
"Can not be empty"
,
...
...
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