Fix accessing fields in composed structs (#733)

pull/753/merge
Michał Treter 4 years ago committed by GitHub
parent 514c9dbd77
commit ec1baac93d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      validator_instance.go
  2. 32
      validator_test.go

@ -414,7 +414,10 @@ func (v *Validate) StructPartialCtx(ctx context.Context, s interface{}, fields .
if len(flds) > 0 {
vd.misc = append(vd.misc[0:0], name...)
// Don't append empty name for unnamed structs
if len(vd.misc) != 0 {
vd.misc = append(vd.misc, '.')
}
for _, s := range flds {

@ -949,6 +949,38 @@ func TestStructPartial(t *testing.T) {
NotEqual(t, errs, nil)
AssertError(t, errs, "TestPartial.Anonymous.SubAnonStruct[0].Test", "TestPartial.Anonymous.SubAnonStruct[0].Test", "Test", "Test", "required")
// Test for unnamed struct
testStruct := &TestStruct{
String: "test",
}
unnamedStruct := struct {
String string `validate:"required" json:"StringVal"`
}{String: "test"}
composedUnnamedStruct := struct{ *TestStruct }{&TestStruct{String: "test"}}
errs = validate.StructPartial(testStruct, "String")
Equal(t, errs, nil)
errs = validate.StructPartial(unnamedStruct, "String")
Equal(t, errs, nil)
errs = validate.StructPartial(composedUnnamedStruct, "TestStruct.String")
Equal(t, errs, nil)
testStruct.String = ""
errs = validate.StructPartial(testStruct, "String")
NotEqual(t, errs, nil)
AssertError(t, errs, "TestStruct.String", "TestStruct.String", "String", "String", "required")
unnamedStruct.String = ""
errs = validate.StructPartial(unnamedStruct, "String")
NotEqual(t, errs, nil)
AssertError(t, errs, "String", "String", "String", "String", "required")
composedUnnamedStruct.String = ""
errs = validate.StructPartial(composedUnnamedStruct, "TestStruct.String")
NotEqual(t, errs, nil)
AssertError(t, errs, "TestStruct.String", "TestStruct.String", "String", "String", "required")
}
func TestCrossStructLteFieldValidation(t *testing.T) {

Loading…
Cancel
Save