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

Fix Bad Field type panic #1201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions validator.go
Expand Up @@ -445,6 +445,27 @@ OUTER:
}

default:
switch current.Kind() {
case reflect.Ptr, reflect.Interface:
if !ct.runValidationWhenNil && current.IsNil() {
v.errs = append(v.errs,
&fieldError{
Copy link
Author

Choose a reason for hiding this comment

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

Not very familiar with the errors in this library, so I just went by what was used in the other instance where the runValidationWhenNil is checked and could be entirely wrong 😄

v: v.v,
tag: ct.aliasTag,
actualTag: ct.tag,
ns: v.str1,
structNs: v.str2,
fieldLen: uint8(len(cf.altName)),
structfieldLen: uint8(len(cf.name)),
value: current.Interface(),
param: ct.param,
kind: kind,
typ: current.Type(),
},
)
return
}
}

// set Field Level fields
v.slflParent = parent
Expand Down
13 changes: 13 additions & 0 deletions validator_test.go
Expand Up @@ -697,6 +697,19 @@ func TestAliasTags(t *testing.T) {
PanicMatches(t, func() { validate.RegisterAlias("exists!", "gt=5,lt=10") }, "Alias 'exists!' either contains restricted characters or is the same as a restricted tag needed for normal operation")
}

func TestNoPanicOnRequiredIfBeforeGTEWithPointer(t *testing.T) {
val := New()

type WithPointer struct {
Type string `validate:"required"`
Quantity *int64 `validate:"required_if=Type special,gte=2"`
}
structWithPointer := &WithPointer{Quantity: nil, Type: "notspecial"}

errs := val.Struct(structWithPointer)
NotEqual(t, errs, nil)
}

func TestNilValidator(t *testing.T) {
type TestStruct struct {
Test string `validate:"required"`
Expand Down