Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Commit

Permalink
Pass the errors accumulated during binding to the validate method
Browse files Browse the repository at this point in the history
  • Loading branch information
mmurray committed Dec 14, 2015
1 parent 89411c4 commit b03a99e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 4 additions & 6 deletions binding.go
Expand Up @@ -41,7 +41,7 @@ func Bind(req *http.Request, userStruct FieldMapper) Errors {
return Form(req, userStruct)
} else {
errs.Add([]string{}, ContentTypeError, "Empty Content-Type")
errs = append(errs, Validate(req, userStruct)...)
errs = Validate(errs, req, userStruct)
}
} else {
errs.Add([]string{}, ContentTypeError, "Unsupported Content-Type")
Expand Down Expand Up @@ -123,17 +123,15 @@ func defaultJsonBinder(req *http.Request, userStruct FieldMapper) Errors {
return errs
}

errs = append(errs, Validate(req, userStruct)...)
errs = Validate(errs, req, userStruct)

return errs
}

// Validate ensures that all conditions have been met on every field in the
// populated struct. Validation should occur after the request has been
// deserialized into the struct.
func Validate(req *http.Request, userStruct FieldMapper) Errors {
var errs Errors

func Validate(errs Errors, req *http.Request, userStruct FieldMapper) Errors {
fm := userStruct.FieldMap(req)

for fieldPointer, fieldNameOrSpec := range fm {
Expand Down Expand Up @@ -663,7 +661,7 @@ func bindForm(req *http.Request, userStruct FieldMapper, formData map[string][]s

}

errs = append(errs, Validate(req, userStruct)...)
errs = Validate(errs, req, userStruct)

return errs
}
Expand Down
6 changes: 4 additions & 2 deletions validate_test.go
Expand Up @@ -17,7 +17,8 @@ func TestValidate(t *testing.T) {
t.Fatal(err)
}
model := NewCompleteModel()
errs := Validate(req, &model)
var errs Errors
errs = Validate(errs, req, &model)

expectedErrs := make(map[string]bool)
for _, v := range model.FieldMap(nil) {
Expand Down Expand Up @@ -63,7 +64,8 @@ func TestValidate(t *testing.T) {
t.Fatal(err)
}
model := new(AllTypes)
errs := Validate(req, model)
var errs Errors
errs = Validate(errs, req, model)

expectedErrs := make(map[string]bool)
for _, v := range model.FieldMap(nil) {
Expand Down

0 comments on commit b03a99e

Please sign in to comment.