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.

47 lines
937 B

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