forked from cohesivestack/valgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory_test.go
More file actions
329 lines (249 loc) · 10.3 KB
/
Copy pathfactory_test.go
File metadata and controls
329 lines (249 loc) · 10.3 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package valgo
import (
"encoding/json"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFactoryUseLocaleCodeDefault(t *testing.T) {
factory := Factory(FactoryOptions{
LocaleCodeDefault: LocaleCodeEs,
})
v := factory.New().Is(String(" ").Not().Blank())
assert.Contains(t, v.Errors()["value_0"].Messages(), "Value 0 no puede estar en blanco")
// Default localization must be persistent in the same validation
v = v.Is(String(" ").Empty())
assert.Contains(t, v.Errors()["value_1"].Messages(), "Value 1 debe estar vacío")
// Default localization must be persistent in the other validations
v = factory.New().Is(String(" ").Not().Blank())
assert.Contains(t, v.Errors()["value_0"].Messages(), "Value 0 no puede estar en blanco")
// Default localization must be possible to change at Validation level
v = factory.New(Options{LocaleCode: LocaleCodeEn}).Is(String(" ").Not().Blank())
assert.Contains(t, v.Errors()["value_0"].Messages(), "Value 0 can't be blank")
}
func TestFactoryChangeLocaleEntries(t *testing.T) {
originalErrorMessage0 := (*getLocaleEn())[ErrorKeyNotBlank]
modifiedErrorMessage0 := "{{title}} should not be blank"
originalErrorMessage1 := (*getLocaleEn())[ErrorKeyBlank]
modifiedErrorMessage1 := "{{title}} should be blank"
assert.NotEqual(t, originalErrorMessage0, modifiedErrorMessage0)
assert.NotEqual(t, originalErrorMessage1, modifiedErrorMessage1)
locale := &Locale{
ErrorKeyNotBlank: modifiedErrorMessage0,
ErrorKeyBlank: modifiedErrorMessage1,
}
factory := Factory(FactoryOptions{
Locales: map[string]*Locale{
localeCodeDefault: locale,
},
})
v := factory.New().Is(String(" ").Not().Blank())
assert.Contains(t, v.Errors()["value_0"].Messages(), "Value 0 should not be blank")
v = v.Is(String("a").Blank())
assert.Contains(t, v.Errors()["value_1"].Messages(), "Value 1 should be blank")
// Other entries should not be modified
v = v.Is(String("").Not().Empty())
assert.Contains(t, v.Errors()["value_2"].Messages(), "Value 2 can't be empty")
v = v.Is(String(" ").Empty())
assert.Contains(t, v.Errors()["value_3"].Messages(), "Value 3 must be empty")
}
func TestFactoryUseOtherLocaleAndChangeLocaleEntries(t *testing.T) {
originalErrorMessage0 := (*getLocaleEs())[ErrorKeyNotBlank]
modifiedErrorMessage0 := "{{title}} no debería estar en blanco"
originalErrorMessage1 := (*getLocaleEs())[ErrorKeyBlank]
modifiedErrorMessage1 := "{{title}} debería estar en blanco"
assert.NotEqual(t, originalErrorMessage0, modifiedErrorMessage0)
assert.NotEqual(t, originalErrorMessage1, modifiedErrorMessage1)
locale := &Locale{
ErrorKeyNotBlank: modifiedErrorMessage0,
ErrorKeyBlank: modifiedErrorMessage1,
}
factory := Factory(FactoryOptions{
Locales: map[string]*Locale{
LocaleCodeEs: locale,
},
})
v := factory.New(Options{LocaleCode: LocaleCodeEs}).Is(String(" ").Not().Blank())
assert.Contains(t, v.Errors()["value_0"].Messages(), "Value 0 no debería estar en blanco")
v = v.Is(String("a").Blank())
assert.Contains(t, v.Errors()["value_1"].Messages(), "Value 1 debería estar en blanco")
// Other entries should not be modified
v = v.Is(String("").Not().Empty())
assert.Contains(t, v.Errors()["value_2"].Messages(), "Value 2 no puede estar vacío")
v = v.Is(String(" ").Empty())
assert.Contains(t, v.Errors()["value_3"].Messages(), "Value 3 debe estar vacío")
}
func TestFactoryAddNewLocaleEntries(t *testing.T) {
locale := &Locale{
ErrorKeyNotBlank: "{{title}} can't be blank (XX)",
ErrorKeyBlank: "{{title}} must be blank (XX)",
}
factory := Factory(FactoryOptions{
Locales: map[string]*Locale{
"xx": locale,
},
})
v := factory.New(Options{LocaleCode: "xx"}).Is(String(" ").Not().Blank())
assert.Contains(t, v.Errors()["value_0"].Messages(), "Value 0 can't be blank (XX)")
v = v.Is(String("a").Blank())
assert.Contains(t, v.Errors()["value_1"].Messages(), "Value 1 must be blank (XX)")
// For the nonexisting keys, then should use the default language
v = v.Is(String("").Not().Empty())
assert.Contains(t, v.Errors()["value_2"].Messages(), "Value 2 can't be empty")
v = v.Is(String(" ").Empty())
assert.Contains(t, v.Errors()["value_3"].Messages(), "Value 3 must be empty")
// Use new locale Entries but changing the default in the Factory
factory = Factory(FactoryOptions{
LocaleCodeDefault: LocaleCodeEs,
Locales: map[string]*Locale{
"xx": locale,
},
})
v = factory.New().Is(String(" ").Not().Blank())
assert.Contains(t, v.Errors()["value_0"].Messages(), "Value 0 can't be blank (XX)")
v = v.Is(String("a").Blank())
assert.Contains(t, v.Errors()["value_1"].Messages(), "Value 1 must be blank (XX)")
// For the nonexisting keys, then should use the default language
v = v.Is(String("").Not().Empty())
assert.Contains(t, v.Errors()["value_2"].Messages(), "Value 2 no puede estar vacío")
v = v.Is(String(" ").Empty())
assert.Contains(t, v.Errors()["value_3"].Messages(), "Value 3 debe estar vacío")
// Use new locale Entries but changing the default in the Factory to be the
// same new nonexisting locale. That will use the Valgo default locale ("en")
factory = Factory(FactoryOptions{
LocaleCodeDefault: "xx",
Locales: map[string]*Locale{
"xx": locale,
},
})
v = factory.New().Is(String(" ").Not().Blank())
assert.Contains(t, v.Errors()["value_0"].Messages(), "Value 0 can't be blank (XX)")
v = v.Is(String("a").Blank())
assert.Contains(t, v.Errors()["value_1"].Messages(), "Value 1 must be blank (XX)")
// For the nonexisting keys, then should use the default language
v = v.Is(String("").Not().Empty())
assert.Contains(t, v.Errors()["value_2"].Messages(), "Value 2 can't be empty")
v = v.Is(String(" ").Empty())
assert.Contains(t, v.Errors()["value_3"].Messages(), "Value 3 must be empty")
}
func TestFactoryCustomErrorMarshallJSON(t *testing.T) {
customFunc := func(e *Error) ([]byte, error) {
errors := map[string]interface{}{}
for k, v := range e.errors {
if len(v.Messages()) == 1 {
errors[k] = v.Messages()[0]
} else {
errors[k] = v.Messages()
}
}
// Add root level errors to customize errors interface
return json.Marshal(map[string]map[string]interface{}{"errors": errors})
}
factory := Factory(FactoryOptions{
MarshalJsonFunc: customFunc,
})
r, _ := regexp.Compile("a")
v := factory.New().
Check(String("", "email").Not().Blank().MatchingTo(r)).
Check(String("", "name").Not().Blank())
jsonByte, err := json.Marshal(v.Error())
assert.NoError(t, err)
jsonMap := map[string]map[string]interface{}{}
err = json.Unmarshal(jsonByte, &jsonMap)
assert.NoError(t, err)
assert.Equal(t, "Name can't be blank", jsonMap["errors"]["name"])
emailErrors := jsonMap["errors"]["email"].([]interface{})
assert.Contains(t, emailErrors, "Email can't be blank")
assert.Contains(t, emailErrors, "Email must match to \"a\"")
customFuncAtValidationLevel := func(e *Error) ([]byte, error) {
errors := map[string]string{"errors": "overridden"}
// Add root level errors to customize errors interface
return json.Marshal(errors)
}
// Marshal JSON should be overridden at Validation level
v = factory.New(Options{MarshalJsonFunc: customFuncAtValidationLevel}).
Check(String("", "email").Not().Blank().MatchingTo(r)).
Check(String("", "name").Not().Blank())
jsonByte, err = json.Marshal(v.Error())
assert.NoError(t, err)
jsonMapAtValidationLevel := map[string]string{}
err = json.Unmarshal(jsonByte, &jsonMapAtValidationLevel)
assert.NoError(t, err)
assert.Equal(t, "overridden", jsonMapAtValidationLevel["errors"])
}
func TestFactoryIs(t *testing.T) {
factory := Factory(FactoryOptions{})
// Test factory Is function
v := factory.Is(String("", "name").Not().Blank())
assert.False(t, v.Valid())
assert.Contains(t, v.Errors(), "name")
assert.Equal(t, "Name can't be blank", v.Errors()["name"].Messages()[0])
}
func TestFactoryCheck(t *testing.T) {
factory := Factory(FactoryOptions{})
// Test factory Check function
v := factory.Check(String("", "name").Not().Blank())
assert.False(t, v.Valid())
assert.Contains(t, v.Errors(), "name")
assert.Equal(t, "Name can't be blank", v.Errors()["name"].Messages()[0])
}
func TestFactoryIn(t *testing.T) {
factory := Factory(FactoryOptions{})
// Test factory In function
v := factory.In("address", Is(String("", "street").Not().Blank()))
assert.False(t, v.Valid())
assert.Contains(t, v.Errors(), "address.street")
assert.Equal(t, "Street can't be blank", v.Errors()["address.street"].Messages()[0])
}
func TestFactoryInRow(t *testing.T) {
factory := Factory(FactoryOptions{})
// Test factory InRow function
v := factory.InRow("addresses", 0, Is(String("", "street").Not().Blank()))
assert.False(t, v.Valid())
assert.Contains(t, v.Errors(), "addresses[0].street")
assert.Equal(t, "Street can't be blank", v.Errors()["addresses[0].street"].Messages()[0])
}
func TestFactoryInCell(t *testing.T) {
factory := Factory(FactoryOptions{})
// Test factory InCell function
v := factory.InCell("tags", 0, Is(String("", "tag").Not().Blank()))
assert.False(t, v.Valid())
assert.Contains(t, v.Errors(), "tags[0]")
assert.Equal(t, "Tag can't be blank", v.Errors()["tags[0]"].Messages()[0])
}
func TestFactoryAddErrorMessage(t *testing.T) {
factory := Factory(FactoryOptions{})
// Test factory AddErrorMessage function
v := factory.AddErrorMessage("field", "Custom error message")
assert.False(t, v.Valid())
assert.Contains(t, v.Errors(), "field")
assert.Equal(t, "Custom error message", v.Errors()["field"].Messages()[0])
}
func TestFactoryIf(t *testing.T) {
factory := Factory(FactoryOptions{})
// Test factory If function
v := factory.If(true, Is(String("", "name").Not().Blank()))
assert.False(t, v.Valid())
assert.Contains(t, v.Errors(), "name")
assert.Equal(t, "Name can't be blank", v.Errors()["name"].Messages()[0])
}
func TestFactoryDo(t *testing.T) {
factory := Factory(FactoryOptions{})
// Test factory Do function
v := factory.Do(func(val *Validation) {
val.Is(String("", "name").Not().Blank())
})
assert.False(t, v.Valid())
assert.Contains(t, v.Errors(), "name")
assert.Equal(t, "Name can't be blank", v.Errors()["name"].Messages()[0])
}
func TestFactoryWhen(t *testing.T) {
factory := Factory(FactoryOptions{})
// Test factory When function
v := factory.When(true, func(val *Validation) {
val.Is(String("", "name").Not().Blank())
})
assert.False(t, v.Valid())
assert.Contains(t, v.Errors(), "name")
assert.Equal(t, "Name can't be blank", v.Errors()["name"].Messages()[0])
}