-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
180 lines (143 loc) · 3.31 KB
/
errors.go
File metadata and controls
180 lines (143 loc) · 3.31 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
package validate
import (
"errors"
"fmt"
"maps"
"strings"
)
// IsValidationError returns true if the given error is of type Error or Errors or if it contains a wrapped Error or Errors.
func IsValidationError(err error) bool {
if isValidationError(err) {
return true
}
// The error could be wrapped, try to unwrap it.
var errs Errors
if errors.As(err, &errs) {
return true
}
var single Error
return errors.As(err, &single)
}
func isValidationError(err error) bool {
switch err.(type) {
case Error, Errors:
return true
}
return false
}
type Errors []Error
// merge merges the given error into the errors.
// If there is already an error with the same exact path, it will merge the violations.
// Otherwise it is added to the errors.
func (e Errors) merge(errs Error) Errors {
for i, err := range e {
if err.ExactPath == errs.ExactPath {
e[i].Violations = append(e[i].Violations, errs.Violations...)
return e
}
}
return append(e, errs)
}
// mergeAll merges the two Errors.
func (e Errors) mergeAll(errs Errors) Errors {
for _, err := range errs {
e = e.merge(err)
}
return e
}
func (e Errors) Error() string {
var errs []string
for _, err := range e {
errs = append(errs, err.Error())
}
return fmt.Sprintf("validation errors: %v", errs)
}
// map runs fn on every error.
func (e Errors) mapErrors(fn func(Error) Error) Errors {
for i := range e {
e[i] = fn(e[i])
}
return e
}
type Error struct {
Path string
ExactPath string
Args Args
Violations []Violation
}
func (e Error) Error() string {
return fmt.Sprintf("validation error for exact path: %s, path: %s, args: %v, violations: %v", e.ExactPath, e.Path, e.Args, e.Violations)
}
type Violation struct {
Code string
Args Args
}
func (v Violation) Error() string {
return fmt.Sprintf("violation code: %s, args: %v", v.Code, v.Args)
}
type Violations []Violation
func (v Violations) Error() string {
var errs []string
for _, err := range v {
errs = append(errs, err.Error())
}
return fmt.Sprintf("violations: %v", errs)
}
type Args map[string]any
func (e Args) Add(key string, value any) Args {
if e == nil {
e = make(Args)
}
e[key] = value
return e
}
// Merge merges a and b into a new Args.
// If a key exists in both a and b, the value from b is used.
func Merge(a Args, b Args) Args {
if a == nil && b == nil {
return nil
}
if a == nil {
return b
}
if b == nil {
return a
}
dst := make(Args)
maps.Copy(dst, a)
maps.Copy(dst, b)
return dst
}
// LastPathSegment will return the last segment of the given path.
// It assumes the path is separated by dots.
func LastPathSegment(s string) string {
if i := strings.LastIndex(s, "."); i != -1 {
return s[i+1:]
}
return s
}
// mapError will run the mapFunc on the given err if the error is of type Error or Errors.
// This is used in the Map and Slice functions to add the correct path and args to an error if the
// validators returned a normal error instead of a list of violations.
func mapError(err error, mapFunc func(Error) Error) error {
if err == nil {
return nil
}
switch e := err.(type) {
case Errors:
for i := range e {
e[i] = mapFunc(e[i])
}
return e
case Error:
return mapFunc(e)
default:
return err
}
}
func prefixPath(path string, prefix string) string {
if path == "" {
return prefix
}
return prefix + "." + path
}