1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-07-15 01:24:33 +02:00

Second shot at proper validation.

This commit is contained in:
Aaron
2015-01-24 16:07:41 -08:00
parent fe7b990095
commit 65f7fad5fc
5 changed files with 135 additions and 31 deletions

View File

@ -5,6 +5,14 @@ import (
"fmt"
)
// Validator is anything that can validate a string and provide a list of errors
// and describe its set of rules.
type Validator interface {
Field() string
Errors(in string) ErrorList
Rules() []string
}
type ErrorList []error
// Error satisfies the error interface.
@ -49,9 +57,18 @@ func (f FieldError) Error() string {
return fmt.Sprintf("%s: %v", f.Name, f.Err)
}
// Validator is anything that can validate a string and provide a list of errors
// and describe its set of rules.
type Validator interface {
Errors(in string) ErrorList
Rules() []string
// Validate validates a request using the given ruleset.
func (ctx *Context) Validate(ruleset []Validator) ErrorList {
errList := make(ErrorList, 0)
for _, validator := range ruleset {
field := validator.Field()
val, _ := ctx.FirstFormValue(field)
if errs := validator.Errors(val); len(errs) > 0 {
errList = append(errList, errs...)
}
}
return errList
}