Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
bwallet
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Go
bwallet
Commits
b16c21ed
Commit
b16c21ed
authored
Jan 05, 2020
by
shajiaming
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
验证
parent
0d23e4b4
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
0 deletions
+59
-0
validate.go
validate_service/validate.go
+59
-0
No files found.
validate_service/validate.go
0 → 100644
View file @
b16c21ed
package
validate_service
import
(
"gopkg.in/go-playground/validator.v9"
"reflect"
"strings"
)
func
ValidateInputs
(
dataSet
interface
{})
(
bool
,
map
[
string
][]
string
)
{
validate
:=
validator
.
New
()
err
:=
validate
.
Struct
(
dataSet
)
if
err
!=
nil
{
//Validation syntax is invalid
if
err
,
ok
:=
err
.
(
*
validator
.
InvalidValidationError
);
ok
{
panic
(
err
)
}
//Validation errors occurred
errors
:=
make
(
map
[
string
][]
string
)
//Use reflector to reverse engineer struct
reflected
:=
reflect
.
ValueOf
(
dataSet
)
for
_
,
err
:=
range
err
.
(
validator
.
ValidationErrors
)
{
// Attempt to find field by name and get json tag name
field
,
_
:=
reflected
.
Type
()
.
FieldByName
(
err
.
StructField
())
var
name
string
//If json tag doesn't exist, use lower case of name
if
name
=
field
.
Tag
.
Get
(
"json"
);
name
==
""
{
name
=
strings
.
ToLower
(
err
.
StructField
())
}
switch
err
.
Tag
()
{
case
"required"
:
errors
[
name
]
=
append
(
errors
[
name
],
"The "
+
name
+
" is required"
)
break
case
"email"
:
errors
[
name
]
=
append
(
errors
[
name
],
"The "
+
name
+
" should be a valid email"
)
break
case
"url"
:
errors
[
name
]
=
append
(
errors
[
name
],
"The "
+
name
+
" should be a valid url"
)
break
case
"eqfield"
:
errors
[
name
]
=
append
(
errors
[
name
],
"The "
+
name
+
" should be equal to the "
+
err
.
Param
())
break
default
:
errors
[
name
]
=
append
(
errors
[
name
],
"The "
+
name
+
" is invalid"
)
break
}
}
return
false
,
errors
}
return
true
,
nil
}
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