-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathstruct_test.go
375 lines (317 loc) · 7.46 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package zog
import (
"encoding/json"
"testing"
"time"
"github.com/Oudwins/zog/tutils"
"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(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(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(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 = Ptr(Struct(Schema{
"str": String().Required(),
"in": Int().Required(),
"fl": Float().Required(),
"bol": Bool().Required(),
"tim": Time().Required(),
}))
var o TestStruct
errs := objSchema.Parse(nil, &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 Ctx) 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().TestFunc(customTest),
})
var obj CustomStruct
data := map[string]any{
"str": "hello",
"num": -1,
}
errs := schema.Parse(data, &obj)
assert.NotNil(t, errs)
tutils.VerifyDefaultIssueMessagesMap(t, errs)
data["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(),
}).TestFunc(func(val any, ctx Ctx) 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": "[email protected]",
"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(),
"apellido": String().Required(),
"email": String().Required(),
"aluID": Int().Required(),
"password": String().Required(),
})
// Test with missing fields
errs := schema.Parse(map[string]any{}, &output)
assert.NotNil(t, errs)
tutils.VerifyDefaultIssueMessagesMap(t, errs)
// Test with valid data
errs = schema.Parse(data, &output)
assert.Nil(t, errs)
assert.Equal(t, "Juan", output.Nombre)
assert.Equal(t, "Perez", output.Apellido)
assert.Equal(t, "[email protected]", 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 Ctx) (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 Ctx) 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 TestStructPassThroughRequired(t *testing.T) {
type TestStruct struct {
Somefield string
}
schema := Struct(Schema{
"somefield": String().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)
tutils.VerifyDefaultIssueMessagesMap(t, errs)
assert.NotEmpty(t, errs["somefield"])
}
type Custom int
const (
Custom1 Custom = 1
Custom2 Custom = 2
)
func TestStructCustomType(t *testing.T) {
s := struct {
Custom int
}{}
schema := Struct(Schema{
"custom": Int().OneOf([]int{int(Custom1), int(Custom2)}),
})
errs := schema.Parse(map[string]any{"custom": int(Custom1)}, &s)
assert.Nil(t, errs)
assert.Equal(t, int(Custom1), s.Custom)
}
type Customs = int
const (
Customs1 Customs = 1
Customs2 Customs = 2
)
func TestStructCustomType2(t *testing.T) {
s := struct {
Custom Customs
}{}
schema := Struct(Schema{
"custom": Int().OneOf([]int{Customs1, Customs2}),
})
errs := schema.Parse(map[string]any{"custom": Customs1}, &s)
assert.Nil(t, errs)
assert.Equal(t, Customs1, s.Custom)
}
func TestStructGetType(t *testing.T) {
s := Struct(Schema{
"field": String(),
})
assert.Equal(t, zconst.TypeStruct, s.getType())
}