1
0
mirror of https://github.com/volatiletech/authboss.git synced 2024-11-28 08:58:38 +02:00
authboss/defaults/validation.go
Aaron L 2db3a3f782 Fix the tests in defaults package
- Make the default responder aggregate data, this allows aggregation of
  data from various middlewares like csrf or extra data for the layout
  without having to have an extra callback function like LayoutDataMaker
  or CRSFMaker
2018-02-01 10:25:54 -08:00

71 lines
1.7 KiB
Go

package defaults
import (
"fmt"
"net/http"
"github.com/volatiletech/authboss"
)
// HTTPFormValidator validates HTTP post type inputs
type HTTPFormValidator struct {
Ruleset []authboss.FieldValidator
ConfirmFields []string
}
// Validate validates a request using the given ruleset.
func (h HTTPFormValidator) Validate(r *http.Request) authboss.ErrorList {
var errList authboss.ErrorList
for _, fieldValidator := range h.Ruleset {
field := fieldValidator.Field()
val := r.FormValue(field)
if errs := fieldValidator.Errors(val); len(errs) > 0 {
errList = append(errList, errs...)
}
}
for i := 0; i < len(h.ConfirmFields)-1; i += 2 {
main := r.FormValue(h.ConfirmFields[i])
if len(main) == 0 {
continue
}
confirm := r.FormValue(h.ConfirmFields[i+1])
if len(confirm) == 0 || main != confirm {
errList = append(errList, FieldError{h.ConfirmFields[i+1], fmt.Errorf("Does not match %s", h.ConfirmFields[i])})
}
}
return errList
}
// FieldError represents an error that occurs during validation and is always
// attached to field on a form.
type FieldError struct {
FieldName string
FieldErr error
}
// NewFieldError literally only exists because of poor name planning
// where name and err can't be exported on the struct due to the method names
func NewFieldError(name string, err error) FieldError {
return FieldError{FieldName: name, FieldErr: err}
}
// Name of the field the error is about
func (f FieldError) Name() string {
return f.FieldName
}
// Err for the field
func (f FieldError) Err() error {
return f.FieldErr
}
// Error in string form
func (f FieldError) Error() string {
return fmt.Sprintf("%s: %v", f.FieldName, f.FieldErr)
}