-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtime.go
250 lines (218 loc) · 5.92 KB
/
time.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
package zog
import (
"time"
"github.com/Oudwins/zog/conf"
p "github.com/Oudwins/zog/internals"
"github.com/Oudwins/zog/zconst"
)
// ! INTERNALS
var _ PrimitiveZogSchema[time.Time] = &TimeSchema{}
type TimeSchema struct {
preTransforms []PreTransform
tests []Test
postTransforms []PostTransform
defaultVal *time.Time
required *Test
catch *time.Time
coercer conf.CoercerFunc
}
// Returns the type of the schema
func (v *TimeSchema) getType() zconst.ZogType {
return zconst.TypeTime
}
// Sets the coercer for the schema
func (v *TimeSchema) setCoercer(c conf.CoercerFunc) {
v.coercer = c
}
type TimeFunc func(opts ...SchemaOption) *TimeSchema
// ! USER FACING FUNCTIONS
// Returns a new Time Schema
var Time TimeFunc = func(opts ...SchemaOption) *TimeSchema {
t := &TimeSchema{
coercer: conf.Coercers.Time,
}
for _, opt := range opts {
opt(t)
}
return t
}
// WARNING ONLY SUPPOORTS Schema.Parse!
// Sets the format function for the time schema.
// Usage is:
//
// z.Time(z.Time.FormatFunc(func(data string) (time.Time, error) {
// return time.Parse(time.RFC3339, data)
// }))
func (t TimeFunc) FormatFunc(format func(data string) (time.Time, error)) SchemaOption {
return func(s ZogSchema) {
s.setCoercer(conf.TimeCoercerFactory(format))
}
}
// WARNING ONLY SUPPOORTS Schema.Parse!
// Sets the string format for the time schema
// Usage is:
// z.Time(z.Time.Format(time.RFC3339))
func (t TimeFunc) Format(format string) SchemaOption {
return t.FormatFunc(func(data string) (time.Time, error) {
return time.Parse(format, data)
})
}
// Parses the data into the destination time.Time. Returns a list of errors
func (v *TimeSchema) Parse(data any, dest *time.Time, 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 processes the data
func (v *TimeSchema) process(ctx *p.SchemaCtx) {
primitiveProcessor(ctx, v.preTransforms, v.tests, v.postTransforms, v.defaultVal, v.required, v.catch, v.coercer, p.IsParseZeroValue)
}
// Validates an existing time.Time
func (v *TimeSchema) Validate(data *time.Time, 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
}
// Internal function to validate the data
func (v *TimeSchema) validate(ctx *p.SchemaCtx) {
primitiveValidator(ctx, v.preTransforms, v.tests, v.postTransforms, v.defaultVal, v.required, v.catch)
}
// Adds pretransform function to schema
func (v *TimeSchema) PreTransform(transform PreTransform) *TimeSchema {
if v.preTransforms == nil {
v.preTransforms = []PreTransform{}
}
v.preTransforms = append(v.preTransforms, transform)
return v
}
// Adds posttransform function to schema
func (v *TimeSchema) PostTransform(transform PostTransform) *TimeSchema {
if v.postTransforms == nil {
v.postTransforms = []PostTransform{}
}
v.postTransforms = append(v.postTransforms, transform)
return v
}
// ! MODIFIERS
// marks field as required
func (v *TimeSchema) Required(options ...TestOption) *TimeSchema {
r := p.Required()
for _, opt := range options {
opt(&r)
}
v.required = &r
return v
}
// marks field as optional
func (v *TimeSchema) Optional() *TimeSchema {
v.required = nil
return v
}
// sets the default value
func (v *TimeSchema) Default(val time.Time) *TimeSchema {
v.defaultVal = &val
return v
}
// sets the catch value (i.e the value to use if the validation fails)
func (v *TimeSchema) Catch(val time.Time) *TimeSchema {
v.catch = &val
return v
}
// GLOBAL METHODS
// custom test function call it -> schema.Test("error_code", func(val any, ctx Ctx) bool {return true})
func (v *TimeSchema) Test(t Test, opts ...TestOption) *TimeSchema {
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 *TimeSchema) TestFunc(testFunc p.TestFunc, options ...TestOption) *TimeSchema {
test := TestFunc("", testFunc)
v.Test(test, options...)
return v
}
// UNIQUE METHODS
// Checks that the value is after the given time
func (v *TimeSchema) After(t time.Time, opts ...TestOption) *TimeSchema {
r := Test{
IssueCode: zconst.IssueCodeAfter,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx Ctx) bool {
val, ok := v.(*time.Time)
if !ok {
return false
}
return val.After(t)
},
}
r.Params[zconst.IssueCodeAfter] = t
for _, opt := range opts {
opt(&r)
}
v.tests = append(v.tests, r)
return v
}
// Checks that the value is before the given time
func (v *TimeSchema) Before(t time.Time, opts ...TestOption) *TimeSchema {
r :=
Test{
IssueCode: zconst.IssueCodeBefore,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx Ctx) bool {
val, ok := v.(*time.Time)
if !ok {
return false
}
return val.Before(t)
},
}
r.Params[zconst.IssueCodeBefore] = t
for _, opt := range opts {
opt(&r)
}
v.tests = append(v.tests, r)
return v
}
// Checks that the value is equal to the given time
func (v *TimeSchema) EQ(t time.Time, opts ...TestOption) *TimeSchema {
r := Test{
IssueCode: zconst.IssueCodeEQ,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx Ctx) bool {
val, ok := v.(*time.Time)
if !ok {
return false
}
return val.Equal(t)
},
}
r.Params[zconst.IssueCodeEQ] = t
for _, opt := range opts {
opt(&r)
}
v.tests = append(v.tests, r)
return v
}