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

unique tag panic on slice contains nil elem #1319

Open
Wanghb1 opened this issue Sep 12, 2024 · 0 comments
Open

unique tag panic on slice contains nil elem #1319

Wanghb1 opened this issue Sep 12, 2024 · 0 comments

Comments

@Wanghb1
Copy link

Wanghb1 commented Sep 12, 2024

i want to make sure elem of pointer slice is not nil,meanwhile, i also need the Name field is unique.

the former can be achieved like:

struct {
F1 []*A validate:"dive,required"
}

however, the unique tag on nil elem cause panic

func TestName(t *testing.T) {
type A struct {
Name string
}
e := struct {
F1 []*A validate:"unique=Name"
}{F1: []*A{nil}}
err := validate.Struct(e)
assert.Error(t, err)
}

it turns out:
panic: reflect: call of reflect.Value.FieldByName on zero Value [recovered]
panic: reflect: call of reflect.Value.FieldByName on zero Value

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

No branches or pull requests

1 participant