-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathmessages_test.go
More file actions
259 lines (210 loc) · 6.52 KB
/
Copy pathmessages_test.go
File metadata and controls
259 lines (210 loc) · 6.52 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
package validate
import (
"fmt"
"testing"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/jsonutil"
"github.com/gookit/goutil/x/assert"
)
func TestBuiltinMessages(t *testing.T) {
bm := BuiltinMessages()
assert.NotContains(t, bm, "testMsg0")
AddBuiltinMessages(map[string]string{
"testMsg0": "message value",
})
bm = BuiltinMessages()
assert.Contains(t, bm, "testMsg0")
AddGlobalMessages(map[string]string{
"testMsg1": "message value",
})
bm = BuiltinMessages()
assert.Contains(t, bm, "testMsg1")
}
func TestErrorsBasic(t *testing.T) {
es := Errors{}
assert.True(t, es.Empty())
assert.Equal(t, "", es.One())
assert.Equal(t, "", es.String())
assert.Nil(t, es.ErrOrNil())
es.Add("field", "required", "error msg0")
assert.Len(t, es, 1)
assert.Equal(t, "error msg0", es.One())
assert.Equal(t, "error msg0", es.FieldOne("field"))
assert.Equal(t, "required: error msg0", es.String())
es.Add("field2", "min", "error msg2")
assert.Contains(t, fmt.Sprintf("%v", es.All()), "field:map[required:error msg0]")
assert.Contains(t, fmt.Sprintf("%v", es.All()), "field2:map[min:error msg2]")
es.Add("field", "minLen", "error msg1")
assert.Len(t, es.Field("field"), 2)
jsonsStr, err := jsonutil.Pretty(es)
assert.NoError(t, err)
fmt.Println(jsonsStr)
dump.V(es)
}
func TestTranslatorBasic(t *testing.T) {
tr := NewTranslator()
assert.True(t, tr.HasMessage("min"))
assert.False(t, tr.HasMessage("not-exists"))
assert.False(t, tr.HasLabel("FIELD1"))
assert.False(t, tr.HasField("FIELD1"))
tr.AddMessage("FIELD1.min", "{field} message1")
assert.True(t, tr.HasMessage("FIELD1.min"))
assert.Equal(t, "FIELD1 message1", tr.Message("min", "FIELD1"))
tr.AddFieldMap(map[string]string{"FIELD1": "output_name"})
assert.Equal(t, "output_name message1", tr.Message("min", "FIELD1"))
tr.AddLabelMap(map[string]string{"FIELD1": "Show Name"})
assert.Equal(t, "Show Name message1", tr.Message("min", "FIELD1"))
tr.Reset()
}
func TestUseAliasMessageKey(t *testing.T) {
is := assert.New(t)
v := New(M{
"name": "inhere",
})
v.StringRule("name", "required|string|minLen:7|maxLen:15")
v.WithMessages(map[string]string{
"name.minLen": "USERNAME min length is 7",
// "minLen": "USERNAME min length is 7",
// "name.minLength": "USERNAME min length is 7",
})
is.False(v.Validate())
is.Equal("USERNAME min length is 7", v.Errors.One())
}
func TestMessageOnStruct(t *testing.T) {
is := assert.New(t)
s := &struct {
Name string `validate:"string"`
BirthDay string `validate:"date" message:"出生日期有误"`
}{
"tom",
"invalid",
}
v := Struct(s)
is.False(v.Validate())
is.Equal("出生日期有误", v.Errors.One())
s1 := &struct {
Name string `validate:"string"`
BirthDay string `validate:"date" message:"date: 出生日期有误"`
}{
"tom",
"invalid",
}
v = Struct(s1)
is.False(v.Validate())
is.Equal("出生日期有误", v.Errors.One())
s2 := &struct {
Name string `validate:"string"`
BirthDay string `validate:"required|date" message:"date: 出生日期有误"`
}{
"tom",
"invalid",
}
v = Struct(s2)
is.False(v.Validate())
is.Equal("出生日期有误", v.Errors.One())
s3 := &struct {
Name string `validate:"string"`
BirthDay string `validate:"date|maxlen:20" message:"出生日期有误"`
}{
"tom",
"invalid",
}
v = Struct(s3)
is.False(v.Validate())
is.Equal("出生日期有误", v.Errors.One())
s4 := &struct {
Name string `validate:"string" json:"name"`
BirthDay string `validate:"date|maxlen:20" json:"birth_day" message:"出生日期有误"`
}{
"tom",
"invalid",
}
v = Struct(s4)
is.False(v.Validate())
is.Equal("出生日期有误", v.Errors.One())
// Ensure message override with no specified field applies to all validation errors if SkipOnError=false is set in
// global options
Config(func(opt *GlobalOption) {
opt.StopOnError = false
})
s5 := &struct {
Name string `validate:"string"`
BirthDay string `validate:"max_len:1|date" message:"出生日期有误"`
}{
"tom",
"ff",
}
v = Struct(s5)
is.False(v.Validate())
is.Contains(v.Errors.String(), "BirthDay")
is.Contains(v.Errors.String(), "max_len: 出生日期有误")
is.Contains(v.Errors.String(), "date: 出生日期有误")
// Restore original global options
Config(func(opt *GlobalOption) {
opt.StopOnError = true
})
}
// with field tag: json
func TestMessageOnStruct_withFieldTag(t *testing.T) {
is := assert.New(t)
s1 := &struct {
Name string `validate:"string" json:"name"`
BirthDay string `validate:"date|maxlen:20" json:"birth_day" message:"出生日期有误"`
}{
"tom",
"invalid",
}
v := Struct(s1)
is.False(v.Validate())
is.Equal("出生日期有误", v.Errors.One())
}
func TestMessageOnStruct_withNested(t *testing.T) {
is := assert.New(t)
type subSt struct {
Tags []string `json:"tags"`
Key1 string
}
s1 := &struct {
Name string `validate:"string" json:"name"`
BirthDay string `validate:"date|maxlen:20" json:"birth_day" label:"birth day" message:"{field} 出生日期有误"`
SubSt subSt `validate:""`
}{
"tom",
"invalid",
subSt{
Key1: "abc",
},
}
v := Struct(s1)
tr := v.Trans()
dump.V(tr.FieldMap(), tr.LabelMap())
is.Contains(tr.FieldMap(), "BirthDay")
is.Contains(tr.FieldMap(), "SubSt.Tags")
is.Equal("birth_day", tr.FieldName("BirthDay"))
is.Equal("tags", tr.FieldName("SubSt.Tags"))
is.Contains(tr.LabelMap(), "BirthDay")
is.Equal("birth day", tr.LabelName("BirthDay"))
is.Equal("tags", tr.LabelName("SubSt.Tags"))
is.False(v.Validate())
dump.V(v.Errors)
is.Equal("birth day 出生日期有误", v.Errors.One())
}
// TestAllocsTranslator locks in the P5a optimization: NewTranslator no longer
// copies the ~150 builtinMessages into every instance, so allocations stay low.
// Before P5a, Reset() copied builtin messages = many allocs per construction.
func TestAllocsTranslator(t *testing.T) {
avg := testing.AllocsPerRun(200, func() {
tr := NewTranslator()
_ = tr
})
// labelMap + fieldMap make() + Translator struct itself; well under the
// pre-P5a count (which included the ~150-entry builtin message copy).
assert.True(t, avg <= 3, "NewTranslator allocs/op should be <=3, got %v", avg)
// builtin fallback still works (no per-instance copy needed).
tr := NewTranslator()
assert.True(t, tr.HasMessage("required"))
assert.Equal(t, "name is required to not be empty", tr.Message("required", "name"))
// custom message overrides builtin via the lazy instance map.
tr.AddMessage("required", "{field} custom required")
assert.Equal(t, "name custom required", tr.Message("required", "name"))
}