Skip to content
Myr

fix(binding): dereference pointer to struct (#3199)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Alonso Villegas · committed by GitHub · parent 86ff4a6 · Browse files · 2 files+28 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+28 added−1 removed

  • other:+28 added−1 removed

2 files+28 added−1 removed

modified:binding/default_validator.go+4 added-1 removed
52 unmodified lines
5354 value := reflect.ValueOf(obj)55 switch value.Kind() {56 case reflect.Ptr:57 if value.Elem().Kind() != reflect.Struct {57 return v.ValidateStruct(value.Elem().Interface())58 return v.ValidateStruct(value.Elem().Interface())59 }60 return v.validateStruct(obj)61 case reflect.Struct:62 return v.validateStruct(obj)63 case reflect.Slice, reflect.Array:64 count := value.Len()
36 unmodified lines
modified:binding/validate_test.go+24 added
190 unmodified lines
191 assert.NoError(t, validate(&str))192 assert.Equal(t, "value", str)193}194195type structModifyValidation struct {196 Integer int197}198199func toZero(sl validator.StructLevel) {200 var s *structModifyValidation = sl.Top().Interface().(*structModifyValidation)201 s.Integer = 0202}203204func TestValidateAndModifyStruct(t *testing.T) {205 // This validates that pointers to structs are passed to the validator206 // giving us the ability to modify the struct being validated.207 engine, ok := Validator.Engine().(*validator.Validate)208 assert.True(t, ok)209210 engine.RegisterStructValidation(toZero, structModifyValidation{})211212 s := structModifyValidation{Integer: 1}213 errs := validate(&s)214215 assert.Nil(t, errs)216 assert.Equal(t, s, structModifyValidation{Integer: 0})217}218219// structCustomValidation is a helper struct we use to check that220// custom validation can be registered on it.221// The `notone` binding directive is for custom validation and registered later.222type structCustomValidation struct {
30 unmodified lines