25 lines
619 B
Go
25 lines
619 B
Go
package validate
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Collect validates a value against a sequence of validation functions,
|
||
|
|
// like All, but runs every function instead of stopping at the first
|
||
|
|
// failure. All failures are returned together as one joined error (see
|
||
|
|
// errors.Join); errors.Is still matches each individual error contained in
|
||
|
|
// it. If nothing fails, Collect returns nil.
|
||
|
|
func Collect[T any](fs ...func(T) error) func(T) error {
|
||
|
|
return func(value T) error {
|
||
|
|
var errs []error
|
||
|
|
|
||
|
|
for _, f := range fs {
|
||
|
|
if err := f(value); err != nil {
|
||
|
|
errs = append(errs, err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return errors.Join(errs...)
|
||
|
|
}
|
||
|
|
}
|