-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidator.go
212 lines (187 loc) · 5.96 KB
/
validator.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package types
import (
"cmp"
"errors"
"fmt"
"reflect"
"slices"
"strings"
)
// Validator can be implemented by types that can validate their data.
type Validator interface {
// Valid returns if the data of the implementation is valid.
Valid() bool
}
// ValidatorFunc implements the Validator interface with a function.
type ValidatorFunc func() bool
// Valid returns if the data of the implementation is valid.
func (f ValidatorFunc) Valid() bool {
return f()
}
// StaticValidator implements the Validator interface a bool validity value.
type StaticValidator bool
// Valid returns if the data of the implementation is valid.
func (valid StaticValidator) Valid() bool {
return bool(valid)
}
type Validators []Validator
// Valid returns if the data of the implementation is valid.
func (v Validators) Valid() bool {
for _, validator := range v {
if !validator.Valid() {
return false
}
}
return true
}
// CombinedValidator creates a Validator whose Valid method
// returns false if any of the passed validators Valid methods
// returned false, else it returns true.
func CombinedValidator(validators ...Validator) Validator {
return Validators(validators)
}
// ValidatErr can be implemented by types that can validate their data.
type ValidatErr interface {
// Validate returns an error if the data of the implementation is not valid.
Validate() error
}
// ValidatErrFunc implements the ValidatErr interface with a function.
type ValidatErrFunc func() error
// Validate returns an error if the data of the implementation is not valid.
func (f ValidatErrFunc) Validate() error {
return f()
}
// StaticValidatErr implements the ValidatErr interface for a validation error value.
type StaticValidatErr struct {
Err error
}
// Validate returns an error if the data of the implementation is not valid.
func (v StaticValidatErr) Validate() error {
return v.Err
}
type ValidatErrs []ValidatErr
// Validate returns an error if the data of the implementation is not valid.
func (v ValidatErrs) Validate() error {
for _, validatErr := range v {
if err := validatErr.Validate(); err != nil {
return err
}
}
return nil
}
// CombinedValidatErr creates a ValidatErr whose Validate method
// returns the first error from the passed validatErrs Validate methods
// or nil if none returned an error.
func CombinedValidatErr(validatErrs ...ValidatErr) ValidatErr {
return ValidatErrs(validatErrs)
}
// ValidatorAsValidatErr wraps a Validator as a ValidatErr,
// returning ErrInvalidValue when Validator.Valid() returns false.
type ValidatorAsValidatErr struct {
Validator
}
func (v ValidatorAsValidatErr) Validate() error {
if v.Valid() {
return nil
}
return ErrInvalidValue
}
// ErrInvalidValue means that a value is not valid,
// returned by Validate() and ValidatorAsValidatErr.Validate().
var ErrInvalidValue = errors.New("invalid value")
// Validate returns an error if v implements ValidatErr or Validator
// and the methods ValidatErr.Validate() or Validator.Valid()
// indicate an invalid value.
// The error from ValidatErr.Validate() is returned directly,
// and ErrInvalidValue is returned if Validator.Valid() is false.
// If v does not implement ValidatErr or Validator then nil will be returned.
func Validate(v any) error {
switch x := v.(type) {
case ValidatErr:
return x.Validate()
case Validator:
if !x.Valid() {
return ErrInvalidValue
}
}
return nil
}
// TryValidate returns an error and true if v implements ValidatErr or Validator
// and the methods ValidatErr.Validate() or Validator.Valid()
// indicate an invalid value.
// The error from ValidatErr.Validate() is returned directly,
// and ErrInvalidValue is returned if Validator.Valid() is false.
// If v does not implement ValidatErr or Validator then nil and false
// will be returned.
func TryValidate(v any) (err error, isValidatable bool) {
switch x := v.(type) {
case ValidatErr:
return x.Validate(), true
case Validator:
if x.Valid() {
return nil, true
} else {
return ErrInvalidValue, true
}
default:
return nil, false
}
}
// DeepValidate validates all fields of a struct, all elements of a slice or array,
// and all values of a map by recursively calling Validate or Valid methods.
func DeepValidate(v any) error {
return deepValidate(reflect.ValueOf(v))
}
func deepValidate(v reflect.Value, path ...string) error {
err := Validate(v.Interface())
if err != nil && len(path) > 0 {
err = fmt.Errorf("%s: %w", strings.Join(path, " -> "), err)
}
if v.Kind() == reflect.Ptr {
if v.IsNil() {
return err
}
v = v.Elem()
}
switch v.Kind() {
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
name := fmt.Sprintf("struct field %s", v.Type().Field(i).Name)
err = errors.Join(err, deepValidate(v.Field(i), append(path, name)...))
}
case reflect.Map:
keys := v.MapKeys()
slices.SortFunc(keys, ReflectCompare)
for _, key := range keys {
name := fmt.Sprintf("map value [%#v]", key.Interface())
err = errors.Join(err, deepValidate(v.MapIndex(key), append(path, name)...))
}
case reflect.Slice, reflect.Array:
for i := 0; i < v.Len(); i++ {
name := fmt.Sprintf("elememt [%d]", i)
err = errors.Join(err, deepValidate(v.Index(i), append(path, name)...))
}
}
return err
}
// ReflectCompare compares two reflect.Values of the same type.
// The function panics if the types of a and b
// are not idential or not orderable.
// Orderable types are variantes of integers, floats, and strings.
func ReflectCompare(a, b reflect.Value) int {
if a.Type() != b.Type() {
panic("values are not of the same type")
}
switch a.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return cmp.Compare(a.Int(), b.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return cmp.Compare(a.Uint(), b.Uint())
case reflect.Float32, reflect.Float64:
return cmp.Compare(a.Float(), b.Float())
case reflect.String:
return cmp.Compare(a.String(), b.String())
default:
panic("values are not of an orderable type")
}
}