Skip to content
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

Validate tag omitempty, only base on field value #657

Merged
merged 1 commit into from Sep 27, 2020
Merged

Validate tag omitempty, only base on field value #657

merged 1 commit into from Sep 27, 2020

Conversation

nigelis
Copy link
Contributor

@nigelis nigelis commented Sep 1, 2020

Fixes validation of tag omitempty, base on the field value #654.

Make sure that you've checked the boxes below before you submit PR:

  • Tests exist or have been written that cover this particular change.

Change Details:

  • Validate the tag omitempty, only base on the field value.

@go-playground/admins

@nigelis
Copy link
Contributor Author

nigelis commented Sep 1, 2020

Hi @deankarn ,

Let me explain more about the change.
There are two code blocks in the function traverseField that validates the tag omitempty.

The first code block deals with the situation that the field value is empty and omitempty is the first tag.
When the field value is a nil pointer, the kind would be reflect.Ptr, and no more validation would be continued.

validator/validator.go

Lines 100 to 111 in ea924ce

current, kind, v.fldIsPointer = v.extractTypeInternal(current, false)
switch kind {
case reflect.Ptr, reflect.Interface, reflect.Invalid:
if ct == nil {
return
}
if ct.typeof == typeOmitEmpty || ct.typeof == typeIsDefault {
return
}

The second code block deals with other situations.
When the field value is a pointer, no matter its value, the validation is continued, which is not expected.
Because the hasValue can return the correct value, I simply remove the check about fldIsPointer.

validator/validator.go

Lines 237 to 257 in ea924ce

for {
if ct == nil {
return
}
switch ct.typeof {
case typeOmitEmpty:
// set Field Level fields
v.slflParent = parent
v.flField = current
v.cf = cf
v.ct = ct
if !v.fldIsPointer && !hasValue(v) {
return
}
ct = ct.next
continue

All previous test cases are passed.
The added test case leads to panic in the v10.3.0, but are passed in the PR.

@frederikhors
Copy link
Contributor

This is still a problem. Is this released in v10? https://play.golang.org/p/TOBSAdFl-FC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants