1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-01-10 04:17:59 +02:00
authboss/validation.go

67 lines
1.3 KiB
Go
Raw Normal View History

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_"
)
// 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 {
// 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
Errors(in string) []error
2015-01-25 02:07:41 +02:00
Rules() []string
}
// 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()
}
// 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
}