gorilla/schema

[bug] Required check bypassed if there is another field whose prefix is the field

chunhui-pang opened this issue · 3 comments

Describe the bug

A clear and concise description of what the bug is.
The required field check would be bypassed if another field is provided whose prefix is the field.

For example, if we have a field named TestField, and mark it as required.
We could bypass the required check if we provide another field with the name TestFieldBypass

Versions

Go version: go version
package version: run git rev-parse HEAD inside the repo
latest

Steps to Reproduce

How can the bug be triggered?
struct Test {
TestField string schema:"TestField,required"
TestFieldBypass string schema:"TestFieldBypass"
}

If we deserialize the struct with only TestFieldBypass field set, it would success.

Expected behavior

What output or behaviour were you expecting instead?
Deserialize failed

Code Snippets

A minimum viable code snippet can be useful! (use backticks to format it).
See above

Meet the same bug today.

After some digging, I found in file decoder.go func isEmptyFields line 159:

for key := range src {
    if !isEmpty(f.typ, src[key]) && strings.HasPrefix(key, path) {
	    return false
    }
}

will pass the check when other field has same prefix with the required field.

any one can merge my pr #178 ? @kisielk

stale commented

This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days.