-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.go
More file actions
282 lines (252 loc) · 7.18 KB
/
validate.go
File metadata and controls
282 lines (252 loc) · 7.18 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package result
import (
"fmt"
"strings"
)
// MultiError holds multiple validation errors.
// Useful for collecting all validation failures instead of failing fast.
type MultiError struct {
Errors []error
}
// Error implements the error interface.
func (m MultiError) Error() string {
if len(m.Errors) == 0 {
return "no errors"
}
if len(m.Errors) == 1 {
return m.Errors[0].Error()
}
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%d validation errors: ", len(m.Errors)))
for i, err := range m.Errors {
if i > 0 {
sb.WriteString("; ")
}
sb.WriteString(err.Error())
}
return sb.String()
}
// Unwrap returns the first error for errors.Is and errors.As compatibility.
func (m MultiError) Unwrap() error {
if len(m.Errors) == 0 {
return nil
}
return m.Errors[0]
}
// Validator is an interface that any Result can implement for validation purposes.
// All Result[T] types automatically implement this interface.
type Validator interface {
IsErr() bool
UnwrapErr() error
}
// ValidateAll validates multiple Results and collects ALL errors (not fail-fast).
// Returns Ok with all values if all succeed, or Err with MultiError containing all failures.
//
// This is useful for form validation where you want to show all errors at once.
//
// Example:
//
// result := result.ValidateAll(
// domain.NewEmail(cmd.Email),
// domain.NewUsername(cmd.Username),
// domain.NewPassword(cmd.Password),
// )
// // If multiple validations fail, all errors are collected
func ValidateAll[T any](results ...Result[T]) Result[[]T] {
var values []T
var errors []error
for _, r := range results {
if r.IsOk() {
values = append(values, r.Unwrap())
} else {
errors = append(errors, r.UnwrapErr())
}
}
if len(errors) > 0 {
return Err[[]T](MultiError{Errors: errors})
}
return Ok(values)
}
// ValidateAllMap validates Results of different types and returns all values or all errors.
// Returns a struct with slices of Ok values and errors.
//
// Example:
//
// results := result.ValidateAllMap(
// domain.NewEmail(cmd.Email),
// domain.NewUsername(cmd.Username),
// domain.NewPassword(cmd.Password),
// )
// if len(results.Errors) > 0 {
// // Handle multiple validation errors
// }
func ValidateAllMap[T any](results ...Result[T]) (values []T, errors []error) {
for _, r := range results {
if r.IsOk() {
values = append(values, r.Unwrap())
} else {
errors = append(errors, r.UnwrapErr())
}
}
return values, errors
}
// ValidateRules applies multiple validation rules to a value.
// Returns Ok if all rules pass, or Err with the first failing rule.
//
// Example:
//
// result := result.ValidateRules(
// email,
// func(e Email) error {
// if isBlacklisted(e) { return ErrBlacklisted }
// return nil
// },
// func(e Email) error {
// if isDisposable(e) { return ErrDisposable }
// return nil
// },
// )
func ValidateRules[T any](value T, rules ...func(T) error) Result[T] {
for _, rule := range rules {
if err := rule(value); err != nil {
return Err[T](err)
}
}
return Ok(value)
}
// ValidateAllRules applies multiple validation rules and collects ALL errors.
// Returns Ok if all rules pass, or Err with MultiError containing all failures.
//
// Example:
//
// result := result.ValidateAllRules(
// email,
// func(e Email) error {
// if isBlacklisted(e) { return ErrBlacklisted }
// return nil
// },
// func(e Email) error {
// if isDisposable(e) { return ErrDisposable }
// return nil
// },
// )
// // Both errors will be collected if both rules fail
func ValidateAllRules[T any](value T, rules ...func(T) error) Result[T] {
var errors []error
for _, rule := range rules {
if err := rule(value); err != nil {
errors = append(errors, err)
}
}
if len(errors) > 0 {
return Err[T](MultiError{Errors: errors})
}
return Ok(value)
}
// ValidateField wraps a field validation with field name context.
// Useful for building structured validation error messages.
//
// Example:
//
// emailResult := result.ValidateField("email", domain.NewEmail(cmd.Email))
// usernameResult := result.ValidateField("username", domain.NewUsername(cmd.Username))
func ValidateField[T any](fieldName string, r Result[T]) Result[T] {
if r.IsErr() {
return Err[T](Wrap(r.UnwrapErr(), "field."+fieldName))
}
return r
}
// ValidateStruct validates multiple fields of a struct and collects all errors.
// Accepts any type that implements the Validator interface (which all Result[T] do).
// Returns Ok if all validations pass, or Err with MultiError.
//
// Example:
//
// result.ValidateStruct(
// result.ValidateField("email", domain.NewEmail(input.Email)),
// result.ValidateField("username", domain.NewUsername(input.Username)),
// result.ValidateField("password", domain.NewPassword(input.Password)),
// )
func ValidateStruct(results ...Validator) Result[struct{}] {
var errors []error
for _, r := range results {
if r.IsErr() {
errors = append(errors, r.UnwrapErr())
}
}
if len(errors) > 0 {
return Err[struct{}](MultiError{Errors: errors})
}
return Ok(struct{}{})
}
// ValidateMap validates all values in a map and collects errors by key.
// Returns Ok if all validations pass, or Err with map of key -> error.
//
// Example:
//
// fields := map[string]string{
// "email": "test@example.com",
// "username": "testuser",
// }
// result := result.ValidateMap(fields, func(key, value string) error {
// if value == "" {
// return fmt.Errorf("%s cannot be empty", key)
// }
// return nil
// })
func ValidateMap[K comparable, V any](
items map[K]V,
validator func(K, V) error,
) Result[map[K]V] {
errorMap := make(map[K]error)
for key, value := range items {
if err := validator(key, value); err != nil {
errorMap[key] = err
}
}
if len(errorMap) > 0 {
// Convert map errors to MultiError
var errors []error
for key, err := range errorMap {
errors = append(errors, fmt.Errorf("%v: %w", key, err))
}
return Err[map[K]V](MultiError{Errors: errors})
}
return Ok(items)
}
// ValidateSlice validates all elements in a slice and collects all errors.
// Returns Ok if all validations pass, or Err with MultiError.
//
// Example:
//
// emails := []string{"test1@example.com", "invalid", "test2@example.com"}
// result := result.ValidateSlice(emails, func(email string) error {
// _, err := result.Extract(domain.NewEmail(email), "validate")
// return err
// })
func ValidateSlice[T any](items []T, validator func(T) error) Result[[]T] {
var errors []error
for i, item := range items {
if err := validator(item); err != nil {
errors = append(errors, fmt.Errorf("index %d: %w", i, err))
}
}
if len(errors) > 0 {
return Err[[]T](MultiError{Errors: errors})
}
return Ok(items)
}
// ValidateSliceResults validates a slice of Results and collects all errors.
// This is like ValidateAll but specifically for slices.
//
// Example:
//
// emailResults := []result.Result[Email]{
// domain.NewEmail("test1@example.com"),
// domain.NewEmail("invalid"),
// domain.NewEmail("test2@example.com"),
// }
// result := result.ValidateSliceResults(emailResults)
func ValidateSliceResults[T any](results []Result[T]) Result[[]T] {
return ValidateAll(results...)
}