-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.go
121 lines (94 loc) · 2.38 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
package owl
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
)
type ArraySchema struct {
schema *AnySchema
of Schema
}
func Array(schema Schema) *ArraySchema {
self := &ArraySchema{Any(), schema}
self.Rule("type", self.Type(), func(value reflect.Value) (any, error) {
if !value.IsValid() {
return nil, nil
}
if value.Kind() != reflect.Array && value.Kind() != reflect.Slice {
return value.Interface(), errors.New("must be an array/slice")
}
return value.Interface(), nil
})
self.Rule("items", schema, nil)
return self
}
func (self ArraySchema) Type() string {
return fmt.Sprintf("array[%s]", self.of.Type())
}
func (self *ArraySchema) Rule(key string, value any, rule RuleFn) *ArraySchema {
self.schema.Rule(key, value, rule)
return self
}
func (self *ArraySchema) Message(message string) *ArraySchema {
self.schema.Message(message)
return self
}
func (self *ArraySchema) Required() *ArraySchema {
self.schema.Required()
return self
}
func (self *ArraySchema) Min(min int) *ArraySchema {
return self.Rule("min", min, func(value reflect.Value) (any, error) {
if !value.IsValid() {
return nil, nil
}
if value.Len() < min {
return value.Interface(), fmt.Errorf("must have length of at least %d", min)
}
return value.Interface(), nil
})
}
func (self *ArraySchema) Max(max int) *ArraySchema {
return self.Rule("max", max, func(value reflect.Value) (any, error) {
if !value.IsValid() {
return nil, nil
}
if value.Len() > max {
return value.Interface(), fmt.Errorf("must have length of at most %d", max)
}
return value.Interface(), nil
})
}
func (self ArraySchema) MarshalJSON() ([]byte, error) {
return json.Marshal(self.schema)
}
func (self ArraySchema) Validate(value any) error {
return self.validate("", reflect.Indirect(reflect.ValueOf(value)))
}
func (self ArraySchema) validate(key string, value reflect.Value) error {
if err := self.schema.validate(key, value); err != nil {
return err
}
if !value.IsValid() {
return nil
}
if value.Kind() == reflect.Interface {
value = value.Elem()
}
err := NewEmptyError("items", key)
for i := 0; i < value.Len(); i++ {
item := reflect.Indirect(value.Index(i))
if item.Kind() == reflect.Interface {
item = item.Elem()
}
if e := self.of.validate(strconv.Itoa(i), item); e != nil {
err = err.Add(e)
}
}
if len(err.Errors) > 0 {
return err
}
return nil
}