Serge Bazanski | cc25bdf | 2018-10-25 14:02:58 +0200 | [diff] [blame] | 1 | package govalidator |
| 2 | |
| 3 | import "strings" |
| 4 | |
| 5 | // Errors is an array of multiple errors and conforms to the error interface. |
| 6 | type Errors []error |
| 7 | |
| 8 | // Errors returns itself. |
| 9 | func (es Errors) Errors() []error { |
| 10 | return es |
| 11 | } |
| 12 | |
| 13 | func (es Errors) Error() string { |
| 14 | var errs []string |
| 15 | for _, e := range es { |
| 16 | errs = append(errs, e.Error()) |
| 17 | } |
| 18 | return strings.Join(errs, ";") |
| 19 | } |
| 20 | |
| 21 | // Error encapsulates a name, an error and whether there's a custom error message or not. |
| 22 | type Error struct { |
| 23 | Name string |
| 24 | Err error |
| 25 | CustomErrorMessageExists bool |
| 26 | |
| 27 | // Validator indicates the name of the validator that failed |
| 28 | Validator string |
| 29 | } |
| 30 | |
| 31 | func (e Error) Error() string { |
| 32 | if e.CustomErrorMessageExists { |
| 33 | return e.Err.Error() |
| 34 | } |
| 35 | return e.Name + ": " + e.Err.Error() |
| 36 | } |