2015-01-18 09:37:05 +02:00
|
|
|
package authboss
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
)
|
|
|
|
|
2015-02-23 12:03:29 +02:00
|
|
|
const (
|
|
|
|
// ConfirmPrefix is prepended to names of confirm fields.
|
|
|
|
ConfirmPrefix = "confirm_"
|
|
|
|
)
|
|
|
|
|
2018-01-30 02:24:42 +02:00
|
|
|
// Validator takes a form name and a set of inputs and returns any validation errors
|
|
|
|
// for the inputs.
|
2015-01-25 02:07:41 +02:00
|
|
|
type Validator interface {
|
2018-01-30 02:24:42 +02:00
|
|
|
// Validate inputs from the named form
|
|
|
|
Validate(name string, fieldValues map[string]string) []error
|
|
|
|
}
|
|
|
|
|
|
|
|
// FieldValidator is anything that can validate a string and provide a list of errors
|
|
|
|
// and describe its set of rules.
|
|
|
|
type FieldValidator interface {
|
2015-01-25 02:07:41 +02:00
|
|
|
Field() string
|
2018-01-30 02:24:42 +02:00
|
|
|
Errors(in string) []error
|
2015-01-25 02:07:41 +02:00
|
|
|
Rules() []string
|
|
|
|
}
|
|
|
|
|
2018-01-30 02:24:42 +02:00
|
|
|
// FieldError describes an error on a field
|
|
|
|
type FieldError interface {
|
|
|
|
Name() string
|
|
|
|
Err() error
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// ErrorList is simply a slice of errors with helpers.
|
2015-01-18 09:37:05 +02:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2015-01-21 08:41:31 +02:00
|
|
|
// 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 {
|
2018-01-30 02:24:42 +02:00
|
|
|
name, err := fieldErr.Name(), fieldErr.Err()
|
|
|
|
m[name] = append(m[name], err.Error())
|
2015-01-21 08:41:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|