-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator_test.go
132 lines (111 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
package ginvalidator
import (
"net/http"
"testing"
vgo "github.com/bube054/validatorgo"
)
func TestCustomValidator(t *testing.T) {
tests := []struct {
name string
field string
errFmtFunc ErrFmtFuncHandler
cvf CustomValidatorFunc
reqOpts ginCtxReqOpts
want validationChainRule
}{
{
name: "Creates a CustomValidator chain rule. Returns true.",
field: "name",
errFmtFunc: nil,
cvf: func(r *http.Request, initialValue, sanitizedValue string) bool {
return true
},
reqOpts: ginCtxReqOpts{body: `{"name": "John"}`, contentType: "application/json"},
want: NewValidationChainRule(
withIsValid(true),
withNewValue("John"),
withValidationChainName(CustomValidatorName),
withValidationChainType(validatorType),
),
},
{
name: "Creates a CustomValidator chain rule. Returns false.",
field: "name",
errFmtFunc: nil,
cvf: func(r *http.Request, initialValue, sanitizedValue string) bool {
return false
},
reqOpts: ginCtxReqOpts{body: `{"name": "John"}`, contentType: "application/json"},
want: NewValidationChainRule(
withIsValid(false),
withNewValue("John"),
withValidationChainName(CustomValidatorName),
withValidationChainType(validatorType),
),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
body := NewBody(test.field, test.errFmtFunc)
chain := body.Chain()
vc := chain.CustomValidator(test.cvf)
vcrs := vc.validator.rulesCreatorFuncs
if len(vcrs) != 1 {
t.Errorf("rule creators length invalid.")
return
}
ctx := createTestGinCtx(test.reqOpts)
vcr := vcrs[0]
value, _ := extractFieldValFromBody(ctx, test.field)
r := vcr(ctx, value, value)
if r != test.want {
t.Errorf("got %+v, want %+v", r, test.want)
}
})
}
}
func TestContains(t *testing.T) {
tests := []struct {
name string
field string
errFmtFunc ErrFmtFuncHandler
seed string
opts *vgo.ContainsOpt
reqOpts ginCtxReqOpts
want validationChainRule
}{
{
name: "Creates a Contains validator chain rule. Returns true.",
field: "text",
errFmtFunc: nil,
seed: "world",
opts: &vgo.ContainsOpt{},
reqOpts: ginCtxReqOpts{body: `{"text": "Hello world"}`, contentType: "application/json"},
want: NewValidationChainRule(
withIsValid(true),
withNewValue("Hello world"),
withValidationChainName(ContainsValidatorName),
withValidationChainType(validatorType),
),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
body := NewBody(test.field, test.errFmtFunc)
chain := body.Chain()
vc := chain.Contains(test.seed, test.opts)
vcrs := vc.validator.rulesCreatorFuncs
if len(vcrs) != 1 {
t.Errorf("rule creators length invalid.")
return
}
ctx := createTestGinCtx(test.reqOpts)
vcr := vcrs[0]
value, _ := extractFieldValFromBody(ctx, test.field)
r := vcr(ctx, value, value)
if r != test.want {
t.Errorf("got %+v, want %+v", r, test.want)
}
})
}
}