-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.go
63 lines (52 loc) · 1.52 KB
/
slice.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
package vaddy
import (
"fmt"
)
// ValidateSlice can be used to validate a slice of values.
type ValidateSlice[T any] func(values []T) error
// SliceMinLength validates that a slice has at least a minimum amount of values.
func SliceMinLength[T any](minLength int) ValidateSlice[T] {
return func(values []T) error {
l := len(values)
if l < minLength {
return &ValidationError{
Message: "not long enough",
Help: fmt.Sprintf("%d < %d", l, minLength),
}
}
return nil
}
}
// All can be used to validate all the items of a slice.
// If T implements the [Validator] interface, each value will also run that validation.
func All[T any](values []T, key string, validateSlice ...ValidateSlice[T]) error {
errs := make([]error, 0)
for i, value := range values {
if v, isValidator := (any(value)).(Validator); isValidator {
if err := v.Validate(); err != nil {
errs = append(errs, expandErrorKeyIndex(err, key, i))
}
}
}
for _, validation := range validateSlice {
err := validation(values)
if err != nil {
errs = append(errs, expandErrorKey(err, key))
}
}
return Join(errs...)
}
// Dive can be used to dive into a slice validating the values within.
func Dive[T any](validateValues ...ValidateValue[T]) ValidateSlice[T] {
return func(values []T) error {
errs := make([]error, 0)
for i, value := range values {
for _, validator := range validateValues {
if err := validator(value); err != nil {
errs = append(errs, expandErrorIndex(err, i))
}
}
}
return Join(errs...)
}
}