-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray.go
More file actions
148 lines (129 loc) · 3.05 KB
/
array.go
File metadata and controls
148 lines (129 loc) · 3.05 KB
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
package check
import "fmt"
// ArraySchema validates slices/arrays where each element matches an inner schema.
type ArraySchema struct {
optional bool
element Schema
minLen *int
maxLen *int
customFns []func([]any) error
}
func NewArray(element Schema) *ArraySchema {
return &ArraySchema{element: element}
}
// clone returns a copy of this schema to preserve immutability in chains.
func (s *ArraySchema) clone() *ArraySchema {
copy := *s
if s.customFns != nil {
copyCustom := make([]func([]any) error, len(s.customFns))
for i, v := range s.customFns {
copyCustom[i] = v
}
copy.customFns = copyCustom
}
return ©
}
func (s *ArraySchema) Optional() *ArraySchema {
s = s.clone()
s.optional = true
return s
}
func (s *ArraySchema) MinLength(n int) *ArraySchema {
s = s.clone()
s.minLen = &n
return s
}
func (s *ArraySchema) MaxLength(n int) *ArraySchema {
s = s.clone()
s.maxLen = &n
return s
}
func (s *ArraySchema) Custom(fn func([]any) error) *ArraySchema {
s = s.clone()
s.customFns = append(s.customFns, fn)
return s
}
func (s *ArraySchema) IsOptional() bool { return s.optional }
func (s *ArraySchema) Parse(value any) (any, error) {
errs := s.Validate(value)
if len(errs) > 0 {
return nil, ValidationErrors(errs)
}
if value == nil {
return nil, nil
}
arr := toSlice(value)
out := make([]any, len(arr))
for i, item := range arr {
parsed, _ := s.element.Parse(item)
out[i] = parsed
}
return out, nil
}
func (s *ArraySchema) Validate(value any) []ValidationError {
if value == nil {
if s.optional {
return nil
}
return []ValidationError{{Message: "required value is missing"}}
}
arr := toSlice(value)
if arr == nil {
return []ValidationError{{Message: fmt.Sprintf("expected array, got %T", value), Value: value}}
}
var errs []ValidationError
if s.minLen != nil && len(arr) < *s.minLen {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("array must have at least %d item(s) (got %d)", *s.minLen, len(arr)),
Value: len(arr),
})
}
if s.maxLen != nil && len(arr) > *s.maxLen {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("array must have at most %d item(s) (got %d)", *s.maxLen, len(arr)),
Value: len(arr),
})
}
for i, item := range arr {
itemErrs := s.element.Validate(item)
errs = append(errs, prefixErrors(fmt.Sprintf("[%d]", i), itemErrs)...)
}
for _, fn := range s.customFns {
if err := fn(arr); err != nil {
errs = append(errs, ValidationError{Message: err.Error(), Value: arr})
}
}
return errs
}
// toSlice attempts to convert any value to []any.
func toSlice(v any) []any {
switch val := v.(type) {
case []any:
return val
case []string:
out := make([]any, len(val))
for i, s := range val {
out[i] = s
}
return out
case []int:
out := make([]any, len(val))
for i, n := range val {
out[i] = n
}
return out
case []float64:
out := make([]any, len(val))
for i, f := range val {
out[i] = f
}
return out
case []bool:
out := make([]any, len(val))
for i, b := range val {
out[i] = b
}
return out
}
return nil
}