-
Notifications
You must be signed in to change notification settings - Fork 12
/
object.go
187 lines (166 loc) · 4.34 KB
/
object.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
package jio
import (
"fmt"
"sort"
"strings"
)
type objectItem struct {
key string
schema Schema
}
// K object keys schema alias
type K map[string]Schema
func (k K) sort() []objectItem {
objects := make([]objectItem, 0, len(k))
for key, schema := range k {
objects = append(objects, objectItem{key, schema})
}
sort.Slice(objects, func(i, j int) bool {
return objects[i].schema.Priority() > objects[j].schema.Priority()
})
return objects
}
// Object Generates a schema object that matches object data type
func Object() *ObjectSchema {
return &ObjectSchema{
rules: make([]func(*Context), 0, 3),
}
}
var _ Schema = new(ObjectSchema)
// ObjectSchema match object data type
type ObjectSchema struct {
baseSchema
required *bool
rules []func(*Context)
}
// SetPriority same as AnySchema.SetPriority
func (o *ObjectSchema) SetPriority(priority int) *ObjectSchema {
o.priority = priority
return o
}
// PrependTransform same as AnySchema.PrependTransform
func (o *ObjectSchema) PrependTransform(f func(*Context)) *ObjectSchema {
o.rules = append([]func(*Context){f}, o.rules...)
return o
}
// Transform same as AnySchema.Transform
func (o *ObjectSchema) Transform(f func(*Context)) *ObjectSchema {
o.rules = append(o.rules, f)
return o
}
// Required same as AnySchema.Required
func (o *ObjectSchema) Required() *ObjectSchema {
o.required = boolPtr(true)
return o.PrependTransform(func(ctx *Context) {
if ctx.Value == nil {
ctx.Abort(fmt.Errorf("field `%s` is required", ctx.FieldPath()))
}
})
}
// Optional same as AnySchema.Optional
func (o *ObjectSchema) Optional() *ObjectSchema {
o.required = boolPtr(false)
return o.PrependTransform(func(ctx *Context) {
if ctx.Value == nil {
ctx.Skip()
}
})
}
// Default same as AnySchema.Default
func (o *ObjectSchema) Default(value map[string]interface{}) *ObjectSchema {
o.required = boolPtr(false)
return o.PrependTransform(func(ctx *Context) {
if ctx.Value == nil {
ctx.Value = value
}
})
}
// With require the presence of these keys.
func (o *ObjectSchema) With(keys ...string) *ObjectSchema {
return o.Transform(func(ctx *Context) {
ctxValue, ok := ctx.Value.(map[string]interface{})
if !ok {
ctx.Abort(fmt.Errorf("field `%s` value %v is not object", ctx.FieldPath(), ctx.Value))
return
}
for _, key := range keys {
_, ok := ctxValue[key]
if !ok {
ctx.Abort(fmt.Errorf("field `%s` not contains %v", ctx.FieldPath(), key))
return
}
}
})
}
// Without forbids the presence of these keys.
func (o *ObjectSchema) Without(keys ...string) *ObjectSchema {
return o.Transform(func(ctx *Context) {
ctxValue, ok := ctx.Value.(map[string]interface{})
if !ok {
ctx.Abort(fmt.Errorf("field `%s` value %v is not object", ctx.FieldPath(), ctx.Value))
return
}
contains := make([]string, 0, 3)
for _, key := range keys {
_, ok := ctxValue[key]
if ok {
contains = append(contains, key)
}
}
if len(contains) > 0 {
ctx.Abort(fmt.Errorf("field `%s` contains %v", ctx.FieldPath(), strings.Join(contains, ",")))
return
}
})
}
// When same as AnySchema.When
func (o *ObjectSchema) When(refPath string, condition interface{}, then Schema) *ObjectSchema {
return o.Transform(func(ctx *Context) { o.when(ctx, refPath, condition, then) })
}
// Keys set the object keys's schema
func (o *ObjectSchema) Keys(children K) *ObjectSchema {
return o.Transform(func(ctx *Context) {
ctxValue, ok := ctx.Value.(map[string]interface{})
if !ok {
ctx.Abort(fmt.Errorf("field `%s` value %v is not object", ctx.FieldPath(), ctx.Value))
return
}
fields := make([]string, len(ctx.fields))
copy(fields, ctx.fields)
defer func() {
ctx.fields = fields
ctx.Value = ctxValue
}()
for _, obj := range children.sort() {
value, _ := ctxValue[obj.key]
ctx.skip = false
ctx.fields = append(fields, obj.key)
ctx.Value = value
obj.schema.Validate(ctx)
if ctx.Err != nil {
return
}
if !ctx.skip {
ctxValue[obj.key] = ctx.Value
}
}
ctx.skip = false
})
}
// Validate same as AnySchema.Validate
func (o *ObjectSchema) Validate(ctx *Context) {
if o.required == nil {
o.Optional()
}
for _, rule := range o.rules {
rule(ctx)
if ctx.skip {
return
}
}
if ctx.Err == nil {
if _, ok := (ctx.Value).(map[string]interface{}); !ok {
ctx.Abort(fmt.Errorf("field `%s` value %v is not object", ctx.FieldPath(), ctx.Value))
}
}
}