Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion validator_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,10 @@ func (v *Validate) StructPartialCtx(ctx context.Context, s interface{}, fields .
if len(flds) > 0 {

vd.misc = append(vd.misc[0:0], name...)
vd.misc = append(vd.misc, '.')
// Don't append empty name for composed structs
if string(vd.misc) != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we check the length of vd.misc here instead of casting to a string? It would be one less allocation :)

vd.misc = append(vd.misc, '.')
}

for _, s := range flds {

Expand Down
22 changes: 22 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,28 @@ 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 composed struct
testStruct := &TestStruct{
String: "test",
}
composedStruct := struct{ *TestStruct }{&TestStruct{String: "test"}}

errs = validate.StructPartial(testStruct, "String")
Equal(t, errs, nil)

errs = validate.StructPartial(composedStruct, "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")

composedStruct.String = ""
errs = validate.StructPartial(composedStruct, "TestStruct.String")
NotEqual(t, errs, nil)
AssertError(t, errs, "TestStruct.String", "TestStruct.String", "String", "String", "required")
}

func TestCrossStructLteFieldValidation(t *testing.T) {
Expand Down