-
Notifications
You must be signed in to change notification settings - Fork 7
/
validator_test.go
174 lines (155 loc) · 3.84 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package validator
import (
"testing"
"github.com/stretchr/testify/require"
"go.uber.org/multierr"
)
func TestError_Error(t *testing.T) {
t.Parallel()
want := "mock_error"
e := &Error{Err: "mock_error"}
got := e.Error()
require.Equal(t, want, got, "Error() = %v, want %v", got, want)
}
func TestNew(t *testing.T) {
t.Parallel()
tests := []struct {
name string
opts []Option
wantErr bool
}{
{
name: "success with empty options",
opts: nil,
wantErr: false,
},
{
name: "success with custom tag name option",
opts: []Option{
WithFieldNameTag("test_tag"),
},
wantErr: false,
},
{
name: "success with custom tag name and error templates options",
opts: []Option{
WithFieldNameTag("test_tag"),
WithErrorTemplates(ErrorTemplates()),
},
wantErr: false,
},
{
name: "success with custom tag name, custom validation and error templates options",
opts: []Option{
WithFieldNameTag("test_tag"),
WithCustomValidationTags(CustomValidationTags()),
WithErrorTemplates(ErrorTemplates()),
},
wantErr: false,
},
{
name: "fail with invalid error template",
opts: []Option{
WithErrorTemplates(map[string]string{"error": "{{.ERROR} ---"}),
},
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := New(tt.opts...)
if tt.wantErr {
require.Nil(t, got, "New() returned Validator should be nil")
require.Error(t, err, "New() error = %v, wantErr %v", err, tt.wantErr)
return
}
require.NotNil(t, got, "New() returned Validator should not be nil")
require.NoError(t, err, "New() unexpected error = %v", err)
})
}
}
func TestValidator_ValidateStruct(t *testing.T) {
t.Parallel()
type subStruct struct {
URLField string `json:"sub_string" validate:"required,url"`
IntField int `json:"sub_int" validate:"required,min=2"`
}
type rootStruct struct {
BoolField bool `json:"bool_field"`
SubStruct subStruct `json:"sub_struct" validate:"required"`
SubStructPtr *subStruct `json:"sub_struct_ptr" validate:"required"`
StringField string `json:"string_field" validate:"required"`
NoNameField string `json:"-" validate:"required"`
}
validObj := rootStruct{
BoolField: true,
SubStruct: subStruct{
URLField: "http://first.test.invalid",
IntField: 3,
},
SubStructPtr: &subStruct{
URLField: "http://second.test.invalid",
IntField: 123,
},
StringField: "hello world",
NoNameField: "test",
}
tests := []struct {
name string
obj interface{}
opts []Option
wantErr bool
wantErrCount int
}{
{
name: "success with custom tag",
obj: validObj,
opts: []Option{
WithFieldNameTag("json"),
},
wantErr: false,
wantErrCount: 0,
},
{
name: "success with custom tag name and error templates options",
obj: validObj,
opts: []Option{
WithFieldNameTag("json"),
WithErrorTemplates(ErrorTemplates()),
},
wantErr: false,
wantErrCount: 0,
},
{
name: "fail with empty data and no options",
obj: rootStruct{},
opts: []Option{},
wantErr: true,
wantErrCount: 5,
},
{
name: "fail with empty data error templates",
obj: rootStruct{},
opts: []Option{
WithFieldNameTag("json"),
WithErrorTemplates(ErrorTemplates()),
},
wantErr: true,
wantErrCount: 5,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
v, err := New(tt.opts...)
require.NoError(t, err, "New() unexpected error = %v", err)
err = v.ValidateStruct(tt.obj)
require.Equal(t, tt.wantErr, err != nil, "ValidateStruct() error = %v, wantErr %v", err, tt.wantErr)
errs := multierr.Errors(err)
require.Equal(t, tt.wantErrCount, len(errs), "errors: %+v", errs)
})
}
}