forked from tonyhb/validate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
60 lines (53 loc) · 1.55 KB
/
error.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
package validate
type ValidationError struct {
Failures []string
// Stores a list of fields that failed validation. This is useful
// during testing: you can assert that all validation rules are
// working as expected.
//
// This is a map because maps can be compared via reflect.DeepEqual even if
// its contents are ordered differently. Slices can not. This makes testing
// more reliable: you can do this:
//
// if ! reflect.DeepEqual(ExpectedFields, err.Fields) {
// t.Fatalf()
// }
//
// instead of ordering your ExpectedField slice in the same manner as
// returned by ValidationError.
//
// @see http://play.golang.org/p/MhB4tDJVCz
Fields map[string]struct{}
}
func (ve *ValidationError) addFailure(field, msg string) {
ve.Failures = append(ve.Failures, msg)
// Ensure we're not assigning to a nil map
if ve.Fields == nil {
ve.Fields = map[string]struct{}{}
}
ve.Fields[field] = struct{}{}
}
// Turn the slice of strings into one string.
func (ve ValidationError) Error() string {
var str = "The following errors occured during validation: "
for _, e := range ve.Failures {
str += e + ". "
}
return str
}
func (ve ValidationError) Stringify() string {
return ve.Error()
}
// Merge validation errors together. This is used with recursion when validating
// anonymous structs.
func (ve *ValidationError) Merge(other ValidationError) {
for _, v := range other.Failures {
ve.Failures = append(ve.Failures, v)
}
for f, v := range other.Fields {
if ve.Fields == nil {
ve.Fields = map[string]struct{}{}
}
ve.Fields[f] = v
}
}