-
Notifications
You must be signed in to change notification settings - Fork 125
Fix ValidatePrivateFields: UpperFirst corrupts unexported embedded struct field paths #331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -270,23 +270,24 @@ func TestValidatePrivateFieldsWhenTrue(t *testing.T) { | |
| Field2 int `validate:"required|int" message:"Field2 outside of range"` | ||
| } | ||
|
|
||
| fooInt := 4 | ||
| barInt := 25 | ||
|
|
||
| myFoo := foo{Field1: fooInt} | ||
| barz := &bar{ | ||
| foo: myFoo, | ||
| Field2: barInt, | ||
| } | ||
|
|
||
| Config(func(opt *GlobalOption) { | ||
| opt.ValidatePrivateFields = true | ||
| }) | ||
|
|
||
| // Field1 = 4 violates min:5 — validation must fail for the right reason (rule violation) | ||
| barz := &bar{ | ||
| foo: foo{Field1: 4}, | ||
| Field2: 25, | ||
| } | ||
| v := Struct(barz) | ||
| v.Validate() | ||
|
|
||
| assert.Equal(t, v.hasError, true) | ||
|
|
||
| // Field1 = 5 satisfies all rules — validation must pass | ||
| barz.foo.Field1 = 5 | ||
| v = Struct(barz) | ||
| v.Validate() | ||
| assert.Equal(t, v.hasError, false) | ||
|
Comment on lines
283
to
+290
|
||
| } | ||
|
|
||
| func TestValidatePrivateFieldsWhenFalse(t *testing.T) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setcan now skipUpperFirstfor registered embedded-field paths (e.g.foo.Field1), but the rest ofSetdoes not handlefieldAtAnonymous(the switch case is empty). Ifd.fieldNames[field]isfieldAtAnonymous,fvremains an invalidreflect.ValueandremoveValuePtr(fv)will panic. Consider handlingfieldAtAnonymousthe same asfieldAtSubStruct(walk the path and select the nested field), or otherwise return a controlled error instead of leavingfvinvalid.