2022-02-24 22:49:46 +01:00
|
|
|
package errors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-03-03 19:55:28 +01:00
|
|
|
// ValidationError is an interface which IF
|
|
|
|
// it custom error types completes, then
|
|
|
|
// it can by mapped to a validation error.
|
|
|
|
//
|
|
|
|
// A validation error(s) can be given by ErrorCodeName's Validation or Err methods.
|
|
|
|
type ValidationError interface {
|
|
|
|
error
|
|
|
|
|
|
|
|
GetField() string
|
|
|
|
GetValue() interface{}
|
|
|
|
GetReason() string
|
2022-02-24 22:49:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ValidationErrors []ValidationError
|
|
|
|
|
2022-03-03 19:55:28 +01:00
|
|
|
func (errs ValidationErrors) Error() string {
|
2022-02-24 22:49:46 +01:00
|
|
|
var buf strings.Builder
|
2022-03-03 19:55:28 +01:00
|
|
|
for i, err := range errs {
|
2022-02-24 22:49:46 +01:00
|
|
|
buf.WriteByte('[')
|
|
|
|
buf.WriteString(strconv.Itoa(i))
|
|
|
|
buf.WriteByte(']')
|
|
|
|
buf.WriteByte(' ')
|
|
|
|
|
|
|
|
buf.WriteString(err.Error())
|
|
|
|
|
2022-03-03 19:55:28 +01:00
|
|
|
if i < len(errs)-1 {
|
2022-02-24 22:49:46 +01:00
|
|
|
buf.WriteByte(',')
|
|
|
|
buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|