-
Notifications
You must be signed in to change notification settings - Fork 12
/
array.go
162 lines (144 loc) · 4.04 KB
/
array.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
package jio
import (
"errors"
"fmt"
"reflect"
)
var _ Schema = new(ArraySchema)
// Array Generates a schema object that matches array data type
func Array() *ArraySchema {
return &ArraySchema{
rules: make([]func(*Context), 0, 3),
}
}
// ArraySchema match array data type
type ArraySchema struct {
baseSchema
required *bool
rules []func(*Context)
}
// SetPriority same as AnySchema.SetPriority
func (a *ArraySchema) SetPriority(priority int) *ArraySchema {
a.priority = priority
return a
}
// PrependTransform same as AnySchema.PrependTransform
func (a *ArraySchema) PrependTransform(f func(*Context)) *ArraySchema {
a.rules = append([]func(*Context){f}, a.rules...)
return a
}
// Transform same as AnySchema.Transform
func (a *ArraySchema) Transform(f func(*Context)) *ArraySchema {
a.rules = append(a.rules, f)
return a
}
// Required same as AnySchema.Required
func (a *ArraySchema) Required() *ArraySchema {
a.required = boolPtr(true)
return a.PrependTransform(func(ctx *Context) {
if ctx.Value == nil {
ctx.Abort(fmt.Errorf("field `%s` is required", ctx.FieldPath()))
}
})
}
// Optional same as AnySchema.Optional
func (a *ArraySchema) Optional() *ArraySchema {
a.required = boolPtr(false)
return a.PrependTransform(func(ctx *Context) {
if ctx.Value == nil {
ctx.Skip()
}
})
}
// Default same as AnySchema.Default
func (a *ArraySchema) Default(value interface{}) *ArraySchema {
a.required = boolPtr(false)
return a.PrependTransform(func(ctx *Context) {
if ctx.Value == nil {
ctx.Value = value
}
})
}
// When same as AnySchema.When
func (a *ArraySchema) When(refPath string, condition interface{}, then Schema) *ArraySchema {
return a.Transform(func(ctx *Context) { a.when(ctx, refPath, condition, then) })
}
// Check use the provided function to validate the value of the key.
// Throws an error when the value is not a slice.
func (a *ArraySchema) Check(f func(interface{}) error) *ArraySchema {
return a.Transform(func(ctx *Context) {
if !ctx.AssertKind(reflect.Slice) {
ctx.Abort(fmt.Errorf("field `%s` value %v is not array", ctx.FieldPath(), ctx.Value))
return
}
if err := f(ctx.Value); err != nil {
ctx.Abort(fmt.Errorf("field `%s` value %v %s", ctx.FieldPath(), ctx.Value, err.Error()))
}
})
}
// Items check if this value can pass the validation of any schema.
func (a *ArraySchema) Items(schemas ...Schema) *ArraySchema {
return a.Check(func(ctxValue interface{}) error {
ctxRV := reflect.ValueOf(ctxValue)
for i := 0; i < ctxRV.Len(); i++ {
rv := ctxRV.Index(i).Interface()
var isValid bool
for _, schema := range schemas {
ctxNew := NewContext(rv)
schema.Validate(ctxNew)
if ctxNew.Err == nil {
isValid = true
break
}
}
if !isValid {
return errors.New("not valid type")
}
}
return nil
})
}
// Min check if the length of this slice is greater than or equal to the provided length.
func (a *ArraySchema) Min(min int) *ArraySchema {
return a.Check(func(ctxValue interface{}) error {
if reflect.ValueOf(ctxValue).Len() < min {
return fmt.Errorf("length less than %d", min)
}
return nil
})
}
// Max check if the length of this slice is less than or equal to the provided length.
func (a *ArraySchema) Max(max int) *ArraySchema {
return a.Check(func(ctxValue interface{}) error {
if reflect.ValueOf(ctxValue).Len() > max {
return fmt.Errorf("length exceeded %d", max)
}
return nil
})
}
// Length check if the length of this slice is equal to the provided length.
func (a *ArraySchema) Length(length int) *ArraySchema {
return a.Check(func(ctxValue interface{}) error {
if reflect.ValueOf(ctxValue).Len() != length {
return fmt.Errorf("length not equal to %d", length)
}
return nil
})
}
// Validate same as AnySchema.Validate
func (a *ArraySchema) Validate(ctx *Context) {
if a.required == nil {
a.Optional()
}
for _, rule := range a.rules {
rule(ctx)
if ctx.skip {
return
}
}
if ctx.Err == nil {
if !ctx.AssertKind(reflect.Slice) {
ctx.Abort(fmt.Errorf("field `%s` value %v is not array", ctx.FieldPath(), ctx.Value))
}
}
}