-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathnumbers.go
294 lines (256 loc) · 6.37 KB
/
numbers.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
package zog
import (
"github.com/Oudwins/zog/conf"
p "github.com/Oudwins/zog/internals"
"github.com/Oudwins/zog/zconst"
"golang.org/x/exp/constraints"
)
type Numeric = constraints.Ordered
var _ PrimitiveZogSchema[int] = &NumberSchema[int]{}
type NumberSchema[T Numeric] struct {
preTransforms []PreTransform
tests []Test
postTransforms []PostTransform
defaultVal *T
required *Test
catch *T
coercer conf.CoercerFunc
}
// ! INTERNALS
// Returns the type of the schema
func (v *NumberSchema[T]) getType() zconst.ZogType {
return zconst.TypeNumber
}
// Sets the coercer for the schema
func (v *NumberSchema[T]) setCoercer(c CoercerFunc) {
v.coercer = c
}
// ! USER FACING FUNCTIONS
// Deprecated: Use Float64 instead
// creates a new float64 schema
func Float(opts ...SchemaOption) *NumberSchema[float64] {
return Float64(opts...)
}
func Float64(opts ...SchemaOption) *NumberSchema[float64] {
s := &NumberSchema[float64]{
coercer: conf.Coercers.Float64,
}
for _, opt := range opts {
opt(s)
}
return s
}
func Float32(opts ...SchemaOption) *NumberSchema[float32] {
s := &NumberSchema[float32]{
coercer: func(data any) (any, error) {
x, err := conf.Coercers.Float64(data)
if err != nil {
return nil, err
}
if n, ok := x.(float64); ok {
return float32(n), nil
}
return x, nil
},
}
for _, opt := range opts {
opt(s)
}
return s
}
// creates a new int schema
func Int(opts ...SchemaOption) *NumberSchema[int] {
s := &NumberSchema[int]{
coercer: conf.Coercers.Int,
}
for _, opt := range opts {
opt(s)
}
return s
}
func Int64(opts ...SchemaOption) *NumberSchema[int64] {
s := &NumberSchema[int64]{
coercer: func(data any) (any, error) {
x, err := conf.Coercers.Int(data)
if err != nil {
return nil, err
}
if n, ok := x.(int); ok {
return int64(n), nil
}
return x, nil
},
}
for _, opt := range opts {
opt(s)
}
return s
}
func Int32(opts ...SchemaOption) *NumberSchema[int32] {
s := &NumberSchema[int32]{
coercer: func(data any) (any, error) {
x, err := conf.Coercers.Int(data)
if err != nil {
return nil, err
}
if n, ok := x.(int); ok {
return int32(n), nil
}
return x, nil
},
}
for _, opt := range opts {
opt(s)
}
return s
}
// parses the value and stores it in the destination
func (v *NumberSchema[T]) Parse(data any, dest *T, options ...ExecOption) ZogIssueList {
errs := p.NewErrsList()
defer errs.Free()
ctx := p.NewExecCtx(errs, conf.IssueFormatter)
defer ctx.Free()
for _, opt := range options {
opt(ctx)
}
path := p.NewPathBuilder()
defer path.Free()
sctx := ctx.NewSchemaCtx(data, dest, path, v.getType())
defer sctx.Free()
v.process(sctx)
return errs.List
}
// Internal function to process the data
func (v *NumberSchema[T]) process(ctx *p.SchemaCtx) {
primitiveProcessor(ctx, v.preTransforms, v.tests, v.postTransforms, v.defaultVal, v.required, v.catch, v.coercer, p.IsParseZeroValue)
}
// Validates a number pointer
func (v *NumberSchema[T]) Validate(data *T, options ...ExecOption) ZogIssueList {
errs := p.NewErrsList()
defer errs.Free()
ctx := p.NewExecCtx(errs, conf.IssueFormatter)
defer ctx.Free()
for _, opt := range options {
opt(ctx)
}
path := p.NewPathBuilder()
defer path.Free()
sctx := ctx.NewSchemaCtx(data, data, path, v.getType())
defer sctx.Free()
v.validate(sctx)
return errs.List
}
func (v *NumberSchema[T]) validate(ctx *p.SchemaCtx) {
primitiveValidator(ctx, v.preTransforms, v.tests, v.postTransforms, v.defaultVal, v.required, v.catch)
}
// GLOBAL METHODS
func (v *NumberSchema[T]) PreTransform(transform PreTransform) *NumberSchema[T] {
if v.preTransforms == nil {
v.preTransforms = []PreTransform{}
}
v.preTransforms = append(v.preTransforms, transform)
return v
}
// Adds posttransform function to schema
func (v *NumberSchema[T]) PostTransform(transform PostTransform) *NumberSchema[T] {
if v.postTransforms == nil {
v.postTransforms = []PostTransform{}
}
v.postTransforms = append(v.postTransforms, transform)
return v
}
// ! MODIFIERS
// marks field as required
func (v *NumberSchema[T]) Required(options ...TestOption) *NumberSchema[T] {
r := p.Required()
for _, opt := range options {
opt(&r)
}
v.required = &r
return v
}
// marks field as optional
func (v *NumberSchema[T]) Optional() *NumberSchema[T] {
v.required = nil
return v
}
// sets the default value
func (v *NumberSchema[T]) Default(val T) *NumberSchema[T] {
v.defaultVal = &val
return v
}
// sets the catch value (i.e the value to use if the validation fails)
func (v *NumberSchema[T]) Catch(val T) *NumberSchema[T] {
v.catch = &val
return v
}
// custom test function call it -> schema.Test(test, options)
func (v *NumberSchema[T]) Test(t Test, opts ...TestOption) *NumberSchema[T] {
for _, opt := range opts {
opt(&t)
}
t.ValidateFunc = customTestBackwardsCompatWrapper(t.ValidateFunc)
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 *NumberSchema[T]) TestFunc(testFunc p.TestFunc, options ...TestOption) *NumberSchema[T] {
test := TestFunc("", testFunc)
v.Test(test, options...)
return v
}
// UNIQUE METHODS
// Check that the value is one of the enum values
func (v *NumberSchema[T]) OneOf(enum []T, options ...TestOption) *NumberSchema[T] {
t := p.In(enum)
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// checks for equality
func (v *NumberSchema[T]) EQ(n T, options ...TestOption) *NumberSchema[T] {
t := p.EQ(n)
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// checks for lesser or equal
func (v *NumberSchema[T]) LTE(n T, options ...TestOption) *NumberSchema[T] {
t := p.LTE(n)
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// checks for greater or equal
func (v *NumberSchema[T]) GTE(n T, options ...TestOption) *NumberSchema[T] {
t := p.GTE(n)
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// checks for lesser
func (v *NumberSchema[T]) LT(n T, options ...TestOption) *NumberSchema[T] {
t := p.LT(n)
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// checks for greater
func (v *NumberSchema[T]) GT(n T, options ...TestOption) *NumberSchema[T] {
t := p.GT(n)
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}