forked from Oudwins/zog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_test.go
231 lines (199 loc) · 4.69 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
package zog
import (
"encoding/json"
"testing"
"time"
p "github.com/Oudwins/zog/internals"
"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 o TestStruct
errs := objSchema.Parse(&p.EmptyDataProvider{}, &o)
assert.Nil(t, errs)
}
func TestStructMergeSchema(t *testing.T) {
var nameSchema = Struct(Schema{
"name": String().Min(3, Message("Override default message")).Max(10),
})
var ageSchema = Struct(Schema{
"age": Int().GT(18).Required(Message("is required")),
})
var schema = nameSchema.Merge(ageSchema)
type User struct {
Name string
Age int
}
var o User
errs := schema.Parse(NewMapDataProvider(map[string]any{"name": "hello", "age": 20}), &o)
assert.Nil(t, errs)
assert.Equal(t, o.Name, "hello")
assert.Equal(t, o.Age, 20)
}
func TestStructCustomTest(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 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(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, "[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)
})
}