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
103ac3ee
Commit
103ac3ee
authored
Jan 07, 2016
by
miraclesu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add custom validation function
parent
bef6bca3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
3 deletions
+23
-3
util.go
validation/util.go
+23
-3
No files found.
validation/util.go
View file @
103ac3ee
...
...
@@ -56,6 +56,27 @@ func init() {
}
}
type
CustomFunc
func
(
v
*
Validation
,
obj
interface
{},
key
string
)
// Add a custom function to validation
// The name can not be:
// Clear
// HasErrors
// ErrorMap
// Error
// Check
// Valid
// NoMatch
// If the name is same with exists function, it will replace the origin valid function
func
AddCustomFunc
(
name
string
,
f
CustomFunc
)
error
{
if
unFuncs
[
name
]
{
return
fmt
.
Errorf
(
"invalid function name: %s"
,
name
)
}
funcs
[
name
]
=
reflect
.
ValueOf
(
f
)
return
nil
}
// ValidFunc Valid function type
type
ValidFunc
struct
{
Name
string
...
...
@@ -102,7 +123,6 @@ func getValidFuncs(f reflect.StructField) (vfs []ValidFunc, err error) {
return
}
if
vfs
,
tag
,
err
=
getRegFuncs
(
tag
,
f
.
Name
);
err
!=
nil
{
fmt
.
Printf
(
"%+v
\n
"
,
err
)
return
}
fs
:=
strings
.
Split
(
tag
,
";"
)
...
...
@@ -214,7 +234,7 @@ func trim(name, key string, s []string) (ts []interface{}, err error) {
for
i
:=
0
;
i
<
len
(
s
);
i
++
{
var
param
interface
{}
// skip *Validation and obj params
if
param
,
err
=
magic
(
fn
.
Type
()
.
In
(
i
+
2
),
strings
.
TrimSpace
(
s
[
i
]));
err
!=
nil
{
if
param
,
err
=
parseParam
(
fn
.
Type
()
.
In
(
i
+
2
),
strings
.
TrimSpace
(
s
[
i
]));
err
!=
nil
{
return
}
ts
[
i
]
=
param
...
...
@@ -224,7 +244,7 @@ func trim(name, key string, s []string) (ts []interface{}, err error) {
}
// modify the parameters's type to adapt the function input parameters' type
func
magic
(
t
reflect
.
Type
,
s
string
)
(
i
interface
{},
err
error
)
{
func
parseParam
(
t
reflect
.
Type
,
s
string
)
(
i
interface
{},
err
error
)
{
switch
t
.
Kind
()
{
case
reflect
.
Int
:
i
,
err
=
strconv
.
Atoi
(
s
)
...
...
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