validator icon indicating copy to clipboard operation
validator copied to clipboard

unique tag panic on slice contains nil elem

Open Wanghb1 opened this issue 1 year ago • 1 comments

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

Wanghb1 avatar Sep 12 '24 03:09 Wanghb1

Your expectation is that a nil entry in the list would also fail validation, right?

chipaca avatar Jun 11 '25 12:06 chipaca