You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
904 B

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