-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvalidation_error.go
89 lines (79 loc) · 2.2 KB
/
validation_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
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
package ess
import (
"bytes"
"fmt"
)
// ValidationError captures errors about the values of a command's
// parameter or the state of a whole aggregate.
//
// Return errors of this type by the methods handling commands in your
// aggregates.
type ValidationError struct {
Errors map[string][]string `json:"error"`
}
// NewValidationError returns a new, empty validation error.
func NewValidationError() *ValidationError {
return &ValidationError{
Errors: map[string][]string{},
}
}
// Ok returns true if no errors have been recorded with this instance.
func (self *ValidationError) Ok() bool { return len(self.Errors) == 0 }
// Add records an error for field using desc as the error description.
func (self *ValidationError) Add(field string, desc string) *ValidationError {
self.Errors[field] = append(self.Errors[field], desc)
return self
}
// Merge records errors from err into this instance.
//
// If err is a ValidationError, all recorded errors for all fields
// from err are merged into this instance.
//
// Otherwise err's string representation is recorded in the field
// $all.
func (self *ValidationError) Merge(err error) *ValidationError {
verr, ok := err.(*ValidationError)
if !ok {
return self.Add("$all", err.Error())
}
for field, errors := range verr.Errors {
self.Errors[field] = append(self.Errors[field], errors...)
}
return self
}
// Return returns nil if no errors have been recorded with this
// instance. Otherwise this instance is returned.
//
// This method exists to avoid returning a typed nil accidentally.
//
// Example:
//
// func (obj *MyDomainObject) DoSomething(param string) error {
// err := NewValidationError()
// if param == "" {
// err.Add("param", "empty")
// }
// return err.Return()
// }
func (self *ValidationError) Return() error {
if len(self.Errors) == 0 {
return nil
} else {
return self
}
}
// Error implements the error interface.
func (self *ValidationError) Error() string {
out := new(bytes.Buffer)
for field, errors := range self.Errors {
fmt.Fprintf(out, "%s: ", field)
for i, desc := range errors {
fmt.Fprintf(out, "%s", desc)
if i < len(errors)-1 {
fmt.Fprintf(out, ", ")
}
}
fmt.Fprintf(out, "; ")
}
return out.String()
}