-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathzogSchema.go
194 lines (171 loc) · 4.7 KB
/
zogSchema.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
package zog
import (
p "github.com/Oudwins/zog/internals"
"github.com/Oudwins/zog/zconst"
)
// The ZogSchema is the interface all schemas must implement
// This is most useful for internal use. If you are looking to pass schemas around, use the ComplexZogSchema or PrimitiveZogSchema interfaces if possible.
type ZogSchema interface {
process(ctx *p.SchemaCtx)
validate(ctx *p.SchemaCtx)
setCoercer(c CoercerFunc)
getType() zconst.ZogType
}
// This is a common interface for all complex schemas (i.e structs, slices, pointers...)
// You can use this to pass any complex schema around
type ComplexZogSchema interface {
ZogSchema
Parse(val any, dest any, options ...ExecOption) ZogIssueMap
}
// This is a common interface for all primitive schemas (i.e strings, numbers, booleans, time.Time...)
// You can use this to pass any primitive schema around
type PrimitiveZogSchema[T p.ZogPrimitive] interface {
ZogSchema
Parse(val any, dest *T, options ...ExecOption) ZogIssueList
}
// Schema Parts Export
// Function signature for preTransforms. Takes the value and the context and returns the new value and an error.
type PreTransform = p.PreTransform
// Function signature for postTransforms. Takes the value pointer and the context and returns an error.
type PostTransform = p.PostTransform
type IssueFmtFunc = p.IssueFmtFunc
// ! PRIMITIVE PROCESSING
func primitiveProcessor[T p.ZogPrimitive](ctx *p.SchemaCtx, preTransforms []PreTransform, tests []Test, postTransforms []PostTransform, defaultVal *T, required *Test, catch *T, coercer CoercerFunc, isZeroFunc p.IsZeroValueFunc) {
ctx.CanCatch = catch != nil
destPtr := ctx.DestPtr.(*T)
// 4. postTransforms
defer func() {
// only run posttransforms on success
if !ctx.HasErrored() {
for _, fn := range postTransforms {
err := fn(destPtr, ctx)
if err != nil {
ctx.AddIssue(ctx.IssueFromUnknownError(err))
return
}
}
}
}()
// 1. preTransforms
for _, fn := range preTransforms {
nVal, err := fn(ctx.Val, ctx)
// bail if error in preTransform
if err != nil || ctx.Exit {
if ctx.CanCatch {
*destPtr = *catch
return
}
ctx.AddIssue(ctx.IssueFromUnknownError(err))
return
}
ctx.Val = nVal
}
// 2. cast data to string & handle default/required
isZeroVal := isZeroFunc(ctx.Val, ctx)
if isZeroVal {
if defaultVal != nil {
*destPtr = *defaultVal
} else if required == nil {
// This handles optional case
return
} else {
// is required & zero value
// required
if ctx.CanCatch {
*destPtr = *catch
return
} else {
ctx.AddIssue(ctx.IssueFromTest(required, ctx.Val))
return
}
}
} else {
newVal, err := coercer(ctx.Val)
if err == nil {
*destPtr = newVal.(T)
} else {
if ctx.CanCatch {
*destPtr = *catch
return
} else {
ctx.AddIssue(ctx.IssueFromCoerce(err))
return
}
}
}
// 3. tests
for _, test := range tests {
if !test.ValidateFunc(destPtr, ctx) {
// catching the first error if catch is set
if ctx.CanCatch {
*destPtr = *catch
return
}
ctx.AddIssue(ctx.IssueFromTest(&test, ctx.Val))
}
}
// 4. postTransforms -> Done above on defer
}
func primitiveValidator[T p.ZogPrimitive](ctx *p.SchemaCtx, preTransforms []PreTransform, tests []Test, postTransforms []PostTransform, defaultVal *T, required *Test, catch *T) {
ctx.CanCatch = catch != nil
valPtr := ctx.Val.(*T)
// 4. postTransforms
defer func() {
// only run posttransforms on success
if !ctx.HasErrored() {
for _, fn := range postTransforms {
err := fn(valPtr, ctx)
if err != nil {
ctx.AddIssue(ctx.IssueFromUnknownError(err))
return
}
}
}
}()
// 1. preTransforms
for _, fn := range preTransforms {
nVal, err := fn(*valPtr, ctx)
// bail if error in preTransform
if err != nil || ctx.Exit {
if ctx.CanCatch {
*valPtr = *catch
return
}
ctx.AddIssue(ctx.IssueFromUnknownError(err))
return
}
*valPtr = nVal.(T)
}
// 2. cast data to string & handle default/required
// Warning. This uses generic IsZeroValue because for Validate we treat zero values as invalid for required fields. This is different from Parse.
isZeroVal := p.IsZeroValue(*valPtr)
if isZeroVal {
if defaultVal != nil {
*valPtr = *defaultVal
} else if required == nil {
// This handles optional case
return
} else {
// is required & zero value
// required
if ctx.CanCatch {
*valPtr = *catch
return
} else {
ctx.AddIssue(ctx.IssueFromTest(required, ctx.Val))
return
}
}
}
// 3. tests
for _, test := range tests {
if !test.ValidateFunc(valPtr, ctx) {
// catching the first error if catch is set
if ctx.CanCatch {
*valPtr = *catch
return
}
ctx.AddIssue(ctx.IssueFromTest(&test, ctx.Val))
}
}
}