-
Notifications
You must be signed in to change notification settings - Fork 7
/
struct_test.go
356 lines (301 loc) · 7.49 KB
/
struct_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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package zog
import (
"encoding/json"
"testing"
"time"
p "github.com/Oudwins/zog/internals"
"github.com/Oudwins/zog/zconst"
"github.com/stretchr/testify/assert"
)
// structs with pointers
//maps with additional values
// errors are correct
// panics are correct
type obj struct {
Str string
In int
Fl float64
Bol bool
Tim time.Time
}
var objSchema = Struct(Schema{
"str": String().Required(),
"in": Int().Required(),
"fl": Float().Required(),
"bol": Bool().Required(),
"tim": Time().Required(),
})
type objTagged struct {
Str string `zog:"s"`
In int `zog:"i"`
Fl float64 `zog:"f"`
Bol bool `zog:"b"`
Tim time.Time
}
func TestStructExample(t *testing.T) {
var o obj
data := map[string]any{
"str": "hello",
"in": 10,
"fl": 10.5,
"bol": true,
"tim": "2024-08-06T00:00:00Z",
}
// parse the data
errs := objSchema.Parse(NewMapDataProvider(data), &o)
assert.Nil(t, errs)
assert.Equal(t, o.Str, "hello")
}
func TestStructTags(t *testing.T) {
var o objTagged
data := map[string]any{
"s": "hello",
"i": 10,
"f": 10.5,
"b": true,
"tim": "2024-08-06T00:00:00Z",
}
errs := objSchema.Parse(NewMapDataProvider(data), &o)
assert.Nil(t, errs)
assert.Equal(t, o.Str, "hello")
assert.Equal(t, o.In, 10)
assert.Equal(t, o.Fl, 10.5)
assert.Equal(t, o.Bol, true)
assert.Equal(t, o.Tim, time.Date(2024, 8, 6, 0, 0, 0, 0, time.UTC))
}
var nestedSchema = Struct(Schema{
"str": String().Required(),
"schema": Struct(Schema{"str": String().Required()}),
})
func TestStructNestedStructs(t *testing.T) {
v := struct {
Str string
Schema struct {
Str string
}
}{
Str: "hello",
Schema: struct {
Str string
}{},
}
m := map[string]any{
"str": "hello",
"schema": map[string]any{"str": "hello"},
}
errs := nestedSchema.Parse(NewMapDataProvider(m), &v)
assert.Nil(t, errs)
assert.Equal(t, v.Str, "hello")
assert.Equal(t, v.Schema.Str, "hello")
}
func TestStructOptional(t *testing.T) {
type TestStruct struct {
Str string `zog:"str"`
In int `zog:"in"`
Fl float64
Bol bool
Tim time.Time
}
var objSchema = Struct(Schema{
"str": String().Required(),
"in": Int().Required(),
"fl": Float().Required(),
"bol": Bool().Required(),
"tim": Time().Required(),
}).Required().Optional() // should override required
var o TestStruct
var m = map[string]any{}
dp := p.NewMapDataProvider(m)
dp = dp.GetNestedProvider("str")
errs := objSchema.Parse(dp, &o)
assert.Nil(t, errs)
}
func TestStructCustomTestInSchema(t *testing.T) {
type CustomStruct struct {
Str string `zog:"str"`
Num int `zog:"num"`
}
// Create a custom test function
customTest := func(val any, ctx ParseCtx) bool {
// Custom test logic here
num := val.(int)
return num > 0
}
// Create a schema with a custom test
schema := Struct(Schema{
"str": String().Required(),
"num": Int().Test(TestFunc("customTest", customTest)),
})
var obj CustomStruct
data := map[string]any{
"str": "hello",
"num": 10,
}
errs := schema.Parse(data, &obj)
assert.Nil(t, errs)
assert.Equal(t, obj.Str, "hello")
assert.Equal(t, obj.Num, 10)
}
func TestStructCustomTest(t *testing.T) {
type CustomStruct struct {
Str string `zog:"str"`
}
schema := Struct(Schema{
"str": String(),
}).Test(TestFunc("customTest", func(val any, ctx ParseCtx) bool {
s := val.(*CustomStruct)
return s.Str == "valid"
}), Message("customTest"))
var obj CustomStruct
data := map[string]any{
"str": "invalid",
}
errs := schema.Parse(data, &obj)
assert.NotNil(t, errs)
assert.Equal(t, "customTest", errs["$root"][0].Code())
assert.Equal(t, "customTest", errs["$root"][0].Message())
data["str"] = "valid"
errs = schema.Parse(data, &obj)
assert.Nil(t, errs)
}
func TestStructFromIssue(t *testing.T) {
s := `{
"nombre": "Juan",
"apellido": "Perez",
"email": "test@test.com",
"alu_id": 25,
"password": "hunter1"
}`
var data map[string]any
err := json.Unmarshal([]byte(s), &data)
assert.Nil(t, err)
var output struct {
Nombre string `zog:"nombre"`
Apellido string `zog:"apellido"`
Email string `zog:"email"`
AluID int `zog:"alu_id"`
Password string `zog:"password"`
}
schema := Struct(Schema{
"nombre": String().Required(Message("this doesn't display even if validation fails")),
"apellido": String().Required(),
"email": String().Required(),
"aluID": Int().Required(),
"password": String().Required(),
})
errs := schema.Parse(data, &output)
assert.Nil(t, errs)
assert.Equal(t, "Juan", output.Nombre)
assert.Equal(t, "Perez", output.Apellido)
assert.Equal(t, "test@test.com", output.Email)
assert.Equal(t, 25, output.AluID)
assert.Equal(t, "hunter1", output.Password)
}
func TestStructPanicsOnSchemaMismatch(t *testing.T) {
var objSchema = Struct(Schema{
"str": String().Required(),
"in": Int().Required(),
"fl": Float().Required(),
"bol": Bool().Required(),
"tim": Time().Required(),
"cause_panic": String(),
})
var o obj
data := map[string]any{
"str": "hello",
"in": 10,
"fl": 10.5,
"bol": true,
"tim": "2024-08-06T00:00:00Z",
}
assert.Panics(t, func() {
objSchema.Parse(data, &o)
})
}
func TestStructPreTransforms(t *testing.T) {
type TestStruct struct {
Value string
}
preTransform := func(val any, ctx ParseCtx) (any, error) {
if m, ok := val.(map[string]any); ok {
m["value"] = "transformed"
return m, nil
}
return val, nil
}
schema := Struct(Schema{
"value": String().Required(),
}).PreTransform(preTransform)
var output TestStruct
data := map[string]any{"value": "original"}
errs := schema.Parse(data, &output)
assert.Nil(t, errs)
assert.Equal(t, "transformed", output.Value)
}
func TestStructPostTransforms(t *testing.T) {
type TestStruct struct {
Value string
}
postTransform := func(val any, ctx ParseCtx) error {
if s, ok := val.(*TestStruct); ok {
s.Value = "post_" + s.Value
}
return nil
}
schema := Struct(Schema{
"value": String().Required(),
}).PostTransform(postTransform)
var output TestStruct
data := map[string]any{"value": "original"}
errs := schema.Parse(data, &output)
assert.Nil(t, errs)
assert.Equal(t, "post_original", output.Value)
}
func TestStructRequired(t *testing.T) {
type TestStruct struct {
Somefield string
}
schema := Struct(Schema{
"somefield": String().Required(),
}).Required(Message("custom_required"))
var output TestStruct
data := map[string]any{
"somefield": "someValue",
}
errs := schema.Parse(data, &output)
assert.Nil(t, errs)
assert.Equal(t, "someValue", output.Somefield)
var output2 TestStruct
errs = schema.Parse(nil, &output2)
assert.NotNil(t, errs)
assert.NotEmpty(t, errs["$root"])
assert.Equal(t, "custom_required", errs["$root"][0].Message())
}
// func TestStructOptional(t *testing.T) {
// type TestStruct struct {
// OptionalField string
// }
// schema := Struct(Schema{
// "optionalField": String().Optional(),
// })
// t.Run("Optional field present", func(t *testing.T) {
// var output TestStruct
// data := map[string]any{"optionalField": "present"}
// errs := schema.Parse(data, &output)
// assert.Nil(t, errs)
// assert.Equal(t, "present", output.OptionalField)
// })
// t.Run("Optional field missing", func(t *testing.T) {
// var output TestStruct
// data := map[string]any{}
// errs := schema.Parse(data, &output)
// assert.Nil(t, errs)
// assert.Equal(t, "", output.OptionalField)
// })
// }
func TestStructGetType(t *testing.T) {
s := Struct(Schema{
"field": String(),
})
assert.Equal(t, zconst.TypeStruct, s.getType())
}