forked from thedevsaddam/govalidator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator_test.go
146 lines (127 loc) · 3.01 KB
/
validator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package govalidator
import (
"bytes"
"encoding/json"
"net/http"
"net/url"
"testing"
)
func TestValidator_SetDefaultRequired(t *testing.T) {
v := New(Options{})
v.SetDefaultRequired(true)
if !v.Opts.RequiredDefault {
t.Error("SetDefaultRequired failed")
}
}
func TestValidator_Validate(t *testing.T) {
var URL *url.URL
URL, _ = url.Parse("http://www.example.com")
params := url.Values{}
params.Add("name", "John Doe")
params.Add("username", "jhondoe")
params.Add("email", "[email protected]")
params.Add("zip", "8233")
URL.RawQuery = params.Encode()
r, _ := http.NewRequest("GET", URL.String(), nil)
rulesList := MapData{
"name": []string{"required"},
"age": []string{"between:5,16"},
"email": []string{"email"},
"zip": []string{"digits:4"},
}
opts := Options{
Request: r,
Rules: rulesList,
}
v := New(opts)
validationError := v.Validate()
if len(validationError) > 0 {
t.Error("Validate failed to validate correct inputs!")
}
defer func() {
if r := recover(); r == nil {
t.Errorf("Validate did not panic")
}
}()
v1 := New(Options{Rules: MapData{}})
v1.Validate()
}
func Benchmark_Validate(b *testing.B) {
var URL *url.URL
URL, _ = url.Parse("http://www.example.com")
params := url.Values{}
params.Add("name", "John Doe")
params.Add("age", "27")
params.Add("email", "[email protected]")
params.Add("zip", "8233")
URL.RawQuery = params.Encode()
r, _ := http.NewRequest("GET", URL.String(), nil)
rulesList := MapData{
"name": []string{"required"},
"age": []string{"numeric_between:18,60"},
"email": []string{"email"},
"zip": []string{"digits:4"},
}
opts := Options{
Request: r,
Rules: rulesList,
}
v := New(opts)
for n := 0; n < b.N; n++ {
v.Validate()
}
}
//============ validate json test ====================
func TestValidator_ValidateJSON(t *testing.T) {
type User struct {
Name string `json:"name"`
Email string `json:"email"`
Address string `json:"address"`
Age int `json:"age"`
Zip string `json:"zip"`
Color int `json:"color"`
}
postUser := User{
Name: "",
Email: "inalid email",
Address: "",
Age: 1,
Zip: "122",
Color: 5,
}
rules := MapData{
"name": []string{"required"},
"email": []string{"email"},
"address": []string{"required", "between:3,5"},
"age": []string{"bool"},
"zip": []string{"len:4"},
"color": []string{"min:10"},
}
var user User
body, _ := json.Marshal(postUser)
req, _ := http.NewRequest("POST", "http://www.example.com", bytes.NewReader(body))
opts := Options{
Request: req,
Data: &user,
Rules: rules,
}
vd := New(opts)
vd.SetTagIdentifier("json")
validationErr := vd.ValidateJSON()
if len(validationErr) != 5 {
t.Error("ValidateJSON failed")
}
}
func TestValidator_ValidateJSON_panic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("ValidateJSON did not panic")
}
}()
opts := Options{}
vd := New(opts)
validationErr := vd.ValidateJSON()
if len(validationErr) != 5 {
t.Error("ValidateJSON failed")
}
}