-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathstruct.go
291 lines (253 loc) · 7.66 KB
/
struct.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
package zog
import (
"fmt"
"reflect"
"github.com/Oudwins/zog/conf"
p "github.com/Oudwins/zog/internals"
"github.com/Oudwins/zog/zconst"
)
var _ ComplexZogSchema = &StructSchema{}
type StructSchema struct {
preTransforms []p.PreTransform
schema Schema
postTransforms []p.PostTransform
tests []p.Test
// defaultVal any
required *p.Test
// catch any
}
// Returns the type of the schema
func (v *StructSchema) getType() zconst.ZogType {
return zconst.TypeStruct
}
// Sets the coercer for the schema
func (v *StructSchema) setCoercer(c conf.CoercerFunc) {
// noop
}
// ! USER FACING FUNCTIONS
// A map of field names to zog schemas
type Schema map[string]ZogSchema
// Returns a new StructSchema which can be used to parse input data into a struct
func Struct(schema Schema) *StructSchema {
return &StructSchema{
schema: schema,
}
}
// Parses val into destPtr and validates each field based on the schema. Only supports val = map[string]any & dest = &struct
func (v *StructSchema) Parse(data any, destPtr any, options ...ExecOption) p.ZogIssueMap {
errs := p.NewErrsMap()
defer errs.Free()
ctx := p.NewExecCtx(errs, conf.IssueFormatter)
defer ctx.Free()
for _, opt := range options {
opt(ctx)
}
path := p.NewPathBuilder()
defer path.Free()
v.process(ctx.NewSchemaCtx(data, destPtr, path, v.getType()))
return errs.M
}
func (v *StructSchema) process(ctx *p.SchemaCtx) {
defer ctx.Free()
// 1. preTransforms
if v.preTransforms != nil {
for _, fn := range v.preTransforms {
nVal, err := fn(ctx.Val, ctx)
// bail if error in preTransform
if err != nil {
ctx.AddIssue(ctx.Issue().SetError(err))
return
}
ctx.Val = nVal
}
}
// 4. postTransforms
defer func() {
// only run posttransforms on success
if !ctx.HasErrored() {
for _, fn := range v.postTransforms {
err := fn(ctx.DestPtr, ctx)
if err != nil {
ctx.AddIssue(ctx.Issue().SetError(err))
return
}
}
}
}()
var dataProv p.DataProvider
// 2. cast data as DataProvider
if factory, ok := ctx.Val.(p.DpFactory); ok {
newDp, err := factory()
// This is a little bit hacky. But we want to exit here because the error came from zhttp. Meaning we had an error trying to parse the request.
// I'm not sure if this is the best behaviour? Do we want to exit here or do we want to continue processing (ofc we add the error always)
if err != nil {
ctx.AddIssue(ctx.IssueFromUnknownError(err))
return
}
dataProv = newDp
} else {
newDp, err := p.TryNewAnyDataProvider(ctx.Val)
if err != nil {
ctx.AddIssue(ctx.IssueFromCoerce(err))
return
}
dataProv = newDp
}
// 3. Process / validate struct fields
structVal := reflect.ValueOf(ctx.DestPtr).Elem()
for key, processor := range v.schema {
originalKey := key
if key[0] >= 'a' && key[0] <= 'z' {
var b [32]byte // Use a size that fits your max key length
copy(b[:], key)
b[0] -= 32
key = string(b[:len(key)])
}
fieldMeta, ok := structVal.Type().FieldByName(key)
if !ok {
panic(fmt.Sprintf("Struct is missing expected schema key: %s\n see the zog FAQ for more info", key))
}
destPtr := structVal.FieldByName(key).Addr().Interface()
subValue, fieldKey := dataProv.GetByField(fieldMeta, originalKey)
switch schema := processor.(type) {
case *StructSchema:
schema.process(ctx.NewSchemaCtx(subValue, destPtr, ctx.Path.Push(&fieldKey), schema.getType()))
default:
schema.process(ctx.NewSchemaCtx(subValue, destPtr, ctx.Path.Push(&fieldKey), schema.getType()))
}
ctx.Path.Pop()
}
// 3. Tests for struct
for _, test := range v.tests {
if !test.ValidateFunc(ctx.DestPtr, ctx) {
ctx.AddIssue(ctx.IssueFromTest(&test, ctx.DestPtr))
}
}
}
// Validate a struct pointer given the struct schema. Usage:
// userSchema.Validate(&User, ...options)
func (v *StructSchema) Validate(dataPtr any, options ...ExecOption) p.ZogIssueMap {
errs := p.NewErrsMap()
defer errs.Free()
ctx := p.NewExecCtx(errs, conf.IssueFormatter)
defer ctx.Free()
for _, opt := range options {
opt(ctx)
}
path := p.NewPathBuilder()
defer path.Free()
v.validate(ctx.NewValidateSchemaCtx(dataPtr, path, v.getType()))
return errs.M
}
// Internal function to validate the data
func (v *StructSchema) validate(ctx *p.SchemaCtx) {
defer ctx.Free()
// 4. postTransforms
defer func() {
// only run posttransforms on success
if !ctx.HasErrored() {
for _, fn := range v.postTransforms {
err := fn(ctx.Val, ctx)
if err != nil {
ctx.AddIssue(ctx.IssueFromUnknownError(err))
return
}
}
}
}()
refVal := reflect.ValueOf(ctx.Val).Elem()
// 1. preTransforms
if v.preTransforms != nil {
for _, fn := range v.preTransforms {
nVal, err := fn(refVal.Interface(), ctx)
// bail if error in preTransform
if err != nil {
ctx.AddIssue(ctx.IssueFromUnknownError(err))
return
}
refVal.Set(reflect.ValueOf(nVal))
}
}
// 2. cast data to string & handle default/required
// 3.1 tests for struct fields
for key, schema := range v.schema {
fieldKey := key
if key[0] >= 'a' && key[0] <= 'z' {
var b [32]byte // Use a size that fits your max key length
copy(b[:], key)
b[0] -= 32
key = string(b[:len(key)])
}
fieldMeta, ok := refVal.Type().FieldByName(key)
if !ok {
panic(fmt.Sprintf("Struct is missing expected schema key: %s", key))
}
destPtr := refVal.FieldByName(key).Addr().Interface()
fieldTag, ok := fieldMeta.Tag.Lookup(zconst.ZogTag)
if ok {
fieldKey = fieldTag
}
schema.validate(ctx.NewValidateSchemaCtx(destPtr, ctx.Path.Push(&fieldKey), schema.getType()))
ctx.Path.Pop()
}
// 3. tests for slice
for _, test := range v.tests {
if !test.ValidateFunc(ctx.Val, ctx) {
ctx.AddIssue(ctx.IssueFromTest(&test, ctx.Val))
}
}
// 4. postTransforms -> defered see above
}
// Add a pretransform step to the schema
func (v *StructSchema) PreTransform(transform p.PreTransform) *StructSchema {
if v.preTransforms == nil {
v.preTransforms = []p.PreTransform{}
}
v.preTransforms = append(v.preTransforms, transform)
return v
}
// Adds posttransform function to schema
func (v *StructSchema) PostTransform(transform p.PostTransform) *StructSchema {
if v.postTransforms == nil {
v.postTransforms = []p.PostTransform{}
}
v.postTransforms = append(v.postTransforms, transform)
return v
}
// ! MODIFIERS
// Deprecated: structs are not required or optional. They pass through to the fields. If you want to say that an entire struct may not exist you should use z.Ptr(z.Struct(...))
// This now is a noop. But I believe most people expect it to work how it does now.
// marks field as required
func (v *StructSchema) Required(options ...TestOption) *StructSchema {
return v
}
// Deprecated: structs are not required or optional. They pass through to the fields. If you want to say that an entire struct may not exist you should use z.Ptr(z.Struct(...))
// marks field as optional
func (v *StructSchema) Optional() *StructSchema {
return v
}
// // sets the default value
// func (v *StructSchema) Default(val any) *StructSchema {
// v.defaultVal = val
// return v
// }
// // sets the catch value (i.e the value to use if the validation fails)
// func (v *StructSchema) Catch(val any) *StructSchema {
// v.catch = val
// return v
// }
// ! VALIDATORS
// custom test function call it -> schema.Test(t z.Test, opts ...TestOption)
func (v *StructSchema) Test(t p.Test, opts ...TestOption) *StructSchema {
for _, opt := range opts {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// Create a custom test function for the schema. This is similar to Zod's `.refine()` method.
func (v *StructSchema) TestFunc(testFunc p.TestFunc, options ...TestOption) *StructSchema {
test := TestFunc("", testFunc)
v.Test(test, options...)
return v
}