martini-contrib / binding

Martini handler for mapping and validating a raw request into a structure.
MIT License
140 stars 47 forks source link

add form binding to embedded struct pointer #35

Closed tpng closed 9 years ago

tpng commented 9 years ago

limitation: 1) no validations performed on the embedded struct fields if the pointer is not binded (similar issue for json binding) 2) only support pointer receiver for the Validator interface for the embedded pointer, and the Validator will need to check for nil receiver

suggestion: it might be safer to leave the newed embedded struct as is (without resetting to nil value) even if nothing is binded for the field, to overcome the above limitation

Closes #30

tpng commented 9 years ago

On limitation 2:

func (person Person) Validate(errs Errors, req *http.Request) Errors {
    ...
}

This will panic on nil pointer, so need to check for nil pointer using the following version:

func (person *Person) Validate(errs Errors, req *http.Request) Errors {
    if person == nil {
      // add some errors or else
      return errs
    }
    ...
}
mholt commented 9 years ago

I'm glad you documented the limitations here. I think they are acceptable considering how elegant the solution is otherwise. Thanks!