-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathboolean.go
163 lines (137 loc) · 3.8 KB
/
boolean.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
package zog
import (
"github.com/Oudwins/zog/conf"
p "github.com/Oudwins/zog/internals"
"github.com/Oudwins/zog/zconst"
)
var _ PrimitiveZogSchema[bool] = &BoolSchema{}
type BoolSchema struct {
preTransforms []p.PreTransform
tests []p.Test
postTransforms []p.PostTransform
defaultVal *bool
required *p.Test
catch *bool
coercer conf.CoercerFunc
}
// ! INTERNALS
// Returns the type of the schema
func (v *BoolSchema) getType() zconst.ZogType {
return zconst.TypeBool
}
// Sets the coercer for the schema
func (v *BoolSchema) setCoercer(c conf.CoercerFunc) {
v.coercer = c
}
// ! USER FACING FUNCTIONS
// Returns a new Bool Schema
func Bool(opts ...SchemaOption) *BoolSchema {
b := &BoolSchema{
coercer: conf.Coercers.Bool, // default coercer
}
for _, opt := range opts {
opt(b)
}
return b
}
// Parse data into destination pointer
func (v *BoolSchema) Parse(data any, dest *bool, options ...ExecOption) p.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()
v.process(ctx.NewSchemaCtx(data, dest, path, v.getType()))
return errs.List
}
// Internal function to process the data
func (v *BoolSchema) process(ctx *p.SchemaCtx) {
primitiveProcessor(ctx, v.preTransforms, v.tests, v.postTransforms, v.defaultVal, v.required, v.catch, v.coercer, p.IsParseZeroValue)
}
// Validate data against schema
func (v *BoolSchema) Validate(val *bool, options ...ExecOption) p.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()
v.validate(ctx.NewSchemaCtx(val, val, path, v.getType()))
return errs.List
}
// Internal function to validate data
func (v *BoolSchema) validate(ctx *p.SchemaCtx) {
primitiveValidator(ctx, v.preTransforms, v.tests, v.postTransforms, v.defaultVal, v.required, v.catch)
}
// GLOBAL METHODS
func (v *BoolSchema) Test(t p.Test, options ...TestOption) *BoolSchema {
for _, opt := range options {
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 *BoolSchema) TestFunc(testFunc p.TestFunc, options ...TestOption) *BoolSchema {
test := TestFunc("", testFunc)
v.Test(test, options...)
return v
}
// Adds pretransform function to schema
func (v *BoolSchema) PreTransform(transform p.PreTransform) *BoolSchema {
if v.preTransforms == nil {
v.preTransforms = []p.PreTransform{}
}
v.preTransforms = append(v.preTransforms, transform)
return v
}
// Adds posttransform function to schema
func (v *BoolSchema) PostTransform(transform p.PostTransform) *BoolSchema {
if v.postTransforms == nil {
v.postTransforms = []p.PostTransform{}
}
v.postTransforms = append(v.postTransforms, transform)
return v
}
// ! MODIFIERS
// marks field as required
func (v *BoolSchema) Required(options ...TestOption) *BoolSchema {
r := p.Required()
for _, opt := range options {
opt(&r)
}
v.required = &r
return v
}
// marks field as optional
func (v *BoolSchema) Optional() *BoolSchema {
v.required = nil
return v
}
// sets the default value
func (v *BoolSchema) Default(val bool) *BoolSchema {
v.defaultVal = &val
return v
}
// sets the catch value (i.e the value to use if the validation fails)
func (v *BoolSchema) Catch(val bool) *BoolSchema {
v.catch = &val
return v
}
// UNIQUE METHODS
func (v *BoolSchema) True() *BoolSchema {
v.tests = append(v.tests, p.EQ[bool](true))
return v
}
func (v *BoolSchema) False() *BoolSchema {
v.tests = append(v.tests, p.EQ[bool](false))
return v
}