-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.go
More file actions
220 lines (183 loc) · 4.93 KB
/
validate.go
File metadata and controls
220 lines (183 loc) · 4.93 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
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
213
214
215
216
217
218
219
220
package validate
import "errors"
// Validator represents a validator that can be used to validate a value.
// If a validator fails it should return an new Violation.
// If there is an unexpected exception a normal error should be returned. This error
// will bubble up and be returned to the caller.
type Validator[T any] func(value T) error
// Field will run the validators on the value and return the errors grouped by the field.
// If a violation returned a non Violation that is returned as exception error.
func Field[T any](fieldName string, value T, validators ...Validator[T]) error {
violations, err := validate(value, validators...)
if err != nil {
return err
}
if violations == nil {
return nil
}
return Error{
Path: fieldName,
ExactPath: fieldName,
Violations: violations,
}
}
// Join the errors into a single slice and merge all errors with the same exact path.
// It wil only Join errors that are of the type Error or Errors.
func Join(errs ...error) error {
verrs := Errors{}
for _, e := range errs {
if e == nil {
continue
}
switch e := e.(type) {
case Errors:
for _, err := range e {
verrs = verrs.merge(err)
}
case Error:
verrs = verrs.merge(e)
default:
// If we encountered an exception we just return that.
return e
}
}
if len(verrs) == 0 {
return nil
}
return verrs
}
// Collect will collect the Errors from the given error.
func Collect(err error) []Error {
switch e := err.(type) {
case Errors:
return e
case Error:
return []Error{e}
default:
// The error could be wrapped, try to unwrap it.
var errs Errors
if errors.As(err, &errs) {
return errs
}
var single Error
if errors.As(err, &single) {
return []Error{single}
}
return []Error{}
}
}
// And will run all validators and only return an error if all validators error.
func And[T any](validators ...Validator[T]) Validator[T] {
return func(value T) error {
var allViolations Violations
for _, validator := range validators {
violations, err := validate(value, validator)
if err != nil {
// If an a non violation error is returned we will bubble it up.
return err
}
// We can return early if there are no violations in one validator.
if len(violations) == 0 {
return nil
}
allViolations = append(allViolations, violations...)
}
if len(allViolations) == 0 {
return nil
}
return allViolations
}
}
// If will run the validators only if the shouldRun is true.
func If[T any](shouldRun bool, validators ...Validator[T]) Validator[T] {
if !shouldRun {
return func(value T) error {
return nil
}
}
return func(v T) error {
violations, err := validate(v, validators...)
if err != nil {
return err
}
if violations == nil {
return nil
}
return Violations(violations)
}
}
// FailFirst will run the validators in order and return the first error.
func FailFirst[T any](validators ...Validator[T]) Validator[T] {
return func(value T) error {
for _, validator := range validators {
err := validator(value)
if err != nil {
return err
}
}
return nil
}
}
// Resolve will resolve the value and run the validators on the resolved value while preserving the original validator target.
// This is useful for validating slices or maps where you want to validate a field inside the value.
func Resolve[Original any, Resolved any](resolveFunc func(Original) Resolved, validators ...Validator[Resolved]) []Validator[Original] {
wrapped := make([]Validator[Original], len(validators))
for i, validator := range validators {
validator := validator
wrapped[i] = func(input Original) error {
resolved := resolveFunc(input)
return validator(resolved)
}
}
return wrapped
}
// ReplaceIfErr will replace err with the given newErr if err is not nil.
// This is usefull for overriding a validation error with a custom error.
func ReplaceIfErr(err error, newErr error) error {
if err != nil {
return newErr
}
return nil
}
// Group will prefix the exact path in the given error.
// This function accepts Error and Errors.
func Group(prefix string, err error) error {
switch err := err.(type) {
case Error:
err.ExactPath = prefix + "." + err.ExactPath
return err
case Errors:
for j, e := range err {
e.ExactPath = prefix + "." + e.ExactPath
err[j] = e
}
return err
}
return err
}
// validate will run the validators on the value and return the violations.
// If a validator returns a non *Violation error it will return that error and discard the violations.
func validate[T any](
value T,
validators ...Validator[T],
) ([]Violation, error) {
var violations []Violation
for _, validator := range validators {
err := validator(value)
if err == nil {
continue
}
switch err := err.(type) {
case Violations:
violations = append(violations, err...)
case *Violation:
violations = append(violations, *err)
default:
return nil, err
}
}
if len(violations) == 0 {
return nil, nil
}
return violations, nil
}