forked from volatiletech/authboss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
68 lines (58 loc) · 1.43 KB
/
validation.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
package authboss
import (
"bytes"
)
const (
// ConfirmPrefix is prepended to names of confirm fields.
ConfirmPrefix = "confirm_"
)
// Validator takes a form name and a set of inputs and returns any
// validation errors for the inputs.
type Validator interface {
// Validate makes the type validate itself and return
// a list of validation errors.
Validate() []error
}
// FieldError describes an error on a field
// Typically .Error() has both Name() and Err() together, hence the reason
// for separation.
type FieldError interface {
error
Name() string
Err() error
}
// ErrorMap is a shortcut to change []error into ErrorList and call Map on it
// since this is a common operation.
func ErrorMap(e []error) map[string][]string {
return ErrorList(e).Map()
}
// ErrorList is simply a slice of errors with helpers.
type ErrorList []error
// Error satisfies the error interface.
func (e ErrorList) Error() string {
b := &bytes.Buffer{}
first := true
for _, err := range e {
if first {
first = false
} else {
b.WriteString(", ")
}
b.WriteString(err.Error())
}
return b.String()
}
// Map groups errors by their field name
func (e ErrorList) Map() map[string][]string {
m := make(map[string][]string)
for _, err := range e {
fieldErr, ok := err.(FieldError)
if !ok {
m[""] = append(m[""], err.Error())
} else {
name, err := fieldErr.Name(), fieldErr.Err()
m[name] = append(m[name], err.Error())
}
}
return m
}