mirror of
https://github.com/volatiletech/authboss.git
synced 2025-01-10 04:17:59 +02:00
386133a84b
In order to support multiple different types of requests, there needed to be an interface to be able to read values from a request, and subsequently validate them to return any errors. So we've adjusted the Validator interface to no longer validate a request but instead validate the object it lives on. And we've created a new BodyReader interface.
61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
package authboss
|
|
|
|
import (
|
|
"bytes"
|
|
)
|
|
|
|
const (
|
|
// ConfirmPrefix is prepended to names of confirm fields.
|
|
ConfirmPrefix = "confirm_"
|
|
)
|
|
|
|
// Validator takes a form name and a set of inputs and returns any validation errors
|
|
// for the inputs.
|
|
type Validator interface {
|
|
// Validate makes the type validate itself and return
|
|
// a list of validation errors.
|
|
Validate() []error
|
|
}
|
|
|
|
// FieldError describes an error on a field
|
|
type FieldError interface {
|
|
error
|
|
Name() string
|
|
Err() error
|
|
}
|
|
|
|
// ErrorList is simply a slice of errors with helpers.
|
|
type ErrorList []error
|
|
|
|
// Error satisfies the error interface.
|
|
func (e ErrorList) Error() string {
|
|
b := &bytes.Buffer{}
|
|
first := true
|
|
for _, err := range e {
|
|
if first {
|
|
first = false
|
|
} else {
|
|
b.WriteString(", ")
|
|
}
|
|
b.WriteString(err.Error())
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// Map groups errors by their field name
|
|
func (e ErrorList) Map() map[string][]string {
|
|
m := make(map[string][]string)
|
|
|
|
for _, err := range e {
|
|
fieldErr, ok := err.(FieldError)
|
|
if !ok {
|
|
m[""] = append(m[""], err.Error())
|
|
} else {
|
|
name, err := fieldErr.Name(), fieldErr.Err()
|
|
m[name] = append(m[name], err.Error())
|
|
}
|
|
}
|
|
|
|
return m
|
|
}
|