-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.go
More file actions
103 lines (88 loc) · 2.1 KB
/
validation.go
File metadata and controls
103 lines (88 loc) · 2.1 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
package httpc
import (
"fmt"
"strings"
)
type InvalidParamError struct {
Context []string
Reason string
}
func (e *InvalidParamError) Error() string {
var s strings.Builder
e.writeTo(&s)
return s.String()
}
func (e *InvalidParamError) writeTo(s *strings.Builder, context ...string) {
context = append(context, e.Context...)
s.WriteString(e.Reason)
if len(context) == 0 {
return
}
s.WriteString(", ")
s.WriteString(context[0])
for _, ctx := range context[1:] {
if !strings.HasPrefix(ctx, "[") {
s.WriteByte('.')
}
s.WriteString(ctx)
}
s.WriteByte('.')
}
type InvalidParamsError struct {
Context []string
Errs []InvalidParamError
}
func (e *InvalidParamsError) Err() error {
if len(e.Errs) == 0 {
return nil
}
return e
}
func (e *InvalidParamsError) Error() string {
var s strings.Builder
fmt.Fprintf(&s, "%d validation error(s) found.\n", len(e.Errs))
for _, err := range e.Errs {
s.WriteString("- ")
err.writeTo(&s, e.Context...)
s.WriteByte('\n')
}
return s.String()
}
func (e *InvalidParamsError) AddInvalid(err error, context ...string) {
if err == nil {
return
}
switch ierr := err.(type) {
case *InvalidParamError:
e.addInvalidParamError(ierr, context...)
case *InvalidParamsError:
e.addInvalidParamsError(ierr, context...)
default:
e.Errs = append(e.Errs, InvalidParamError{
Context: context,
Reason: err.Error(),
})
}
}
func (e *InvalidParamsError) AddInvalidWithIndex(err error, index int, context ...string) {
e.AddInvalid(err, append(context, fmt.Sprintf("[%d]", index))...)
}
func (e *InvalidParamsError) addInvalidParamError(err *InvalidParamError, context ...string) {
if err == nil {
return
}
err2 := *err
err2.Context = append(context, err2.Context...)
e.Errs = append(e.Errs, err2)
}
func (e *InvalidParamsError) addInvalidParamsError(err *InvalidParamsError, context ...string) {
if err == nil {
return
}
for i := range err.Errs {
e.addInvalidParamError(&err.Errs[i], context...)
}
}
func NewParamRequiredError(context ...string) *InvalidParamError {
return &InvalidParamError{Context: context, Reason: "missing required param"}
}