forked from cohesivestack/valgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory_test.go
235 lines (173 loc) · 7.43 KB
/
factory_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
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
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 unexisting 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 unexisting 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 unexisting 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 unexisting 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"])
}