forked from infobloxopen/atlas-app-toolkit
/
validationerrors.go
69 lines (62 loc) · 1.92 KB
/
validationerrors.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
package validationerrors
import (
"context"
"github.com/infobloxopen/atlas-app-toolkit/errors"
"google.golang.org/grpc/codes"
)
// ToMapFunc function converts mapping function for *validationerrors.Error to a conventional
// MapFunc from atlas-app-toolkit/errors package.
func ToMapFunc(f func(context.Context, ValidationError) (error, bool)) errors.MapFunc {
return func(ctx context.Context, err error) (error, bool) {
if vErr, ok := err.(ValidationError); ok {
return f(ctx, vErr)
}
return err, false
}
}
// CondValidation function returns a condition function that matches standard
// Validation error and ensures that the error contains a field and a reason.
func CondValidation() errors.MapCond {
return func(err error) bool {
if vErr, ok := err.(ValidationError); ok {
if vErr.Field() != "" && vErr.Reason() != "" {
return true
}
}
return false
}
}
// CondFieldEq function returns a condition function that checks if the
// field matches the validation error field.
func CondFieldEq(theField string) errors.MapCond {
return func(err error) bool {
if vErr, ok := err.(ValidationError); ok {
if vErr.Field() == theField {
return true
}
}
return false
}
}
// CondReasonEq function returns a condition function that checks if the
// reason matches the validation error reason.
func CondReasonEq(theReason string) errors.MapCond {
return func(err error) bool {
if vErr, ok := err.(ValidationError); ok {
if vErr.Reason() == theReason {
return true
}
}
return false
}
}
// DefaultMapping the default behavior for validation error mapping.
func DefaultMapping() errors.MapFunc {
return errors.NewMapping(
CondValidation(),
errors.MapFunc(func(ctx context.Context, err error) (error, bool) {
vErr, _ := err.(ValidationError)
return errors.NewContainer(codes.InvalidArgument, "Invalid %s: %s", vErr.Field(), vErr.Reason()).WithField(vErr.Field(), vErr.Reason()), true
}),
)
}