diff --git a/baked_in.go b/baked_in.go index e8d17a7..dd4b331 100644 --- a/baked_in.go +++ b/baked_in.go @@ -312,10 +312,15 @@ func isUnique(fl FieldLevel) bool { } m := reflect.MakeMap(reflect.MapOf(sfTyp, v.Type())) + var fieldlen int for i := 0; i < field.Len(); i++ { - m.SetMapIndex(reflect.Indirect(reflect.Indirect(field.Index(i)).FieldByName(param)), v) + key := reflect.Indirect(reflect.Indirect(field.Index(i)).FieldByName(param)) + if key.IsValid() { + fieldlen++ + m.SetMapIndex(key, v) + } } - return field.Len() == m.Len() + return fieldlen == m.Len() case reflect.Map: var m reflect.Value if field.Type().Elem().Kind() == reflect.Ptr { diff --git a/validator_test.go b/validator_test.go index b8ebe64..07bc889 100644 --- a/validator_test.go +++ b/validator_test.go @@ -10043,6 +10043,7 @@ func TestUniqueValidationStructPtrSlice(t *testing.T) { }{ {A: stringPtr("one"), B: stringPtr("two")}, {A: stringPtr("one"), B: stringPtr("three")}, + {}, } tests := []struct {