Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
MKCLOS
Core Development Platform
corepkgv2
Commits
d03b6d5a
Commit
d03b6d5a
authored
6 years ago
by
Ajit Jagtap
Browse files
Options
Downloads
Plain Diff
Merge branch 'Rahuls_ValidationMDL_StructValidation' into 'devbranch'
Added struct validation See merge request
!11
parents
296cf52f
6db1bc2f
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!23
Devbranch to Master
,
!11
Added struct validation
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
validationmdl/validationmdl.go
+39
-1
validationmdl/validationmdl.go
validationmdl/validationmdl_test.go
+32
-16
validationmdl/validationmdl_test.go
with
71 additions
and
17 deletions
validationmdl/validationmdl.go
+
39
−
1
View file @
d03b6d5a
package
validationmdl
import
(
"bytes"
"net/http"
"net/url"
"github.com/pquerna/ffjson/ffjson"
"github.com/thedevsaddam/govalidator"
)
...
...
@@ -27,7 +29,6 @@ func ValidateRequest(httpRequest *http.Request, validationRules, validationMessa
opts
.
Messages
=
validationMessages
}
if
contentType
==
"application/json"
||
contentType
==
"text/plain"
{
//Validate request type json and text (RAW data from request)
data
:=
make
(
map
[
string
]
interface
{},
0
)
...
...
@@ -47,3 +48,40 @@ func ValidateRequest(httpRequest *http.Request, validationRules, validationMessa
}
return
nil
}
// ValidateStruct validates the structures
func
ValidateStruct
(
validationData
interface
{},
validationRules
,
validationMessages
govalidator
.
MapData
)
map
[
string
]
interface
{}
{
//Initilize validation errors
var
validationErrors
url
.
Values
//Initialize dummy request
validationDataBytes
,
_
:=
ffjson
.
Marshal
(
&
validationData
)
dymmyHTTPRequest
,
_
:=
http
.
NewRequest
(
"POST"
,
"testUrl.com"
,
bytes
.
NewBufferString
(
string
(
validationDataBytes
)))
dymmyHTTPRequest
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
data
:=
make
(
map
[
string
]
interface
{},
0
)
//Set validator options
opts
:=
govalidator
.
Options
{
Request
:
dymmyHTTPRequest
,
Rules
:
validationRules
,
Data
:
&
data
,
}
//Set custom validation messages if sent from user
if
validationMessages
!=
nil
{
opts
.
Messages
=
validationMessages
}
//Initialize the valiator and validate
validator
:=
govalidator
.
New
(
opts
)
validationErrors
=
validator
.
ValidateJSON
()
if
len
(
validationErrors
)
>
0
{
errs
:=
map
[
string
]
interface
{}{
"validationErrors"
:
validationErrors
}
return
errs
}
return
nil
}
This diff is collapsed.
Click to expand it.
validationmdl/validationmdl_test.go
+
32
−
16
View file @
d03b6d5a
...
...
@@ -207,23 +207,23 @@ func TestValidateRequest(t *testing.T) {
func
BenchmarkValidationTest
(
b
*
testing
.
B
)
{
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
//Validation rules
validationRules
:=
govalidator
.
MapData
{
"email"
:
[]
string
{
"required"
,
"min:5"
,
"max:20"
,
"email"
},
"password"
:
[]
string
{
"required"
},
}
//Validation messages
validationMessages
:=
govalidator
.
MapData
{
"email"
:
[]
string
{
"required:Email Id is required"
,
"min:Min length 5 required"
,
"max:Max length 20 allowed"
,
"email:Enter a valid email"
},
"password"
:
[]
string
{
"required:Password is required"
},
}
sunnyDayData
:=
RequestBodyData
{
Email
:
"test@mkcl.org"
,
Password
:
"test"
}
sunnyDayByteArray
,
_
:=
ffjson
.
Marshal
(
&
sunnyDayData
)
sunnyDayHTTPRequest
,
_
:=
http
.
NewRequest
(
"POST"
,
"test.com"
,
bytes
.
NewBufferString
(
string
(
sunnyDayByteArray
)))
sunnyDayHTTPRequest
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
//Validation rules
validationRules
:=
govalidator
.
MapData
{
"email"
:
[]
string
{
"required"
,
"min:5"
,
"max:20"
,
"email"
},
"password"
:
[]
string
{
"required"
},
}
//Validation messages
validationMessages
:=
govalidator
.
MapData
{
"email"
:
[]
string
{
"required:Email Id is required"
,
"min:Min length 5 required"
,
"max:Max length 20 allowed"
,
"email:Enter a valid email"
},
"password"
:
[]
string
{
"required:Password is required"
},
}
sunnyDayData
:=
RequestBodyData
{
Email
:
"test@mkcl.org"
,
Password
:
"test"
}
sunnyDayByteArray
,
_
:=
ffjson
.
Marshal
(
&
sunnyDayData
)
sunnyDayHTTPRequest
,
_
:=
http
.
NewRequest
(
"POST"
,
"test.com"
,
bytes
.
NewBufferString
(
string
(
sunnyDayByteArray
)))
sunnyDayHTTPRequest
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
ValidateRequest
(
sunnyDayHTTPRequest
,
validationRules
,
validationMessages
)
...
...
@@ -260,3 +260,19 @@ func BenchmarkValidationTestAgainstYQL(b *testing.B) {
ValidateRequest
(
sunnyDayHTTPRequest
,
validationRules
,
nil
)
}
}
func
BenchmarkStructValidation
(
b
*
testing
.
B
)
{
type
EmailValidation
struct
{
Email
string
`json:"email"`
Password
string
`json:"password"`
}
emailValidation
:=
EmailValidation
{}
emailValidation
.
Email
=
"test@mkcl.org"
emailValidation
.
Password
=
"testPassword"
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
ValidateStruct
(
emailValidation
,
validationRules
,
validationMessages
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets