fix unique= when struct field is a nil pointer (#1041)

Fix https://github.com/go-playground/validator/issues/749

Co-authored-by: Yann Salaün <ysalaun@yubik.io>
pull/1081/head
Yann Salaün 2 years ago committed by GitHub
parent 3ee65f8c59
commit cc768b176d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      baked_in.go
  2. 1
      validator_test.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 {

@ -10043,6 +10043,7 @@ func TestUniqueValidationStructPtrSlice(t *testing.T) {
}{
{A: stringPtr("one"), B: stringPtr("two")},
{A: stringPtr("one"), B: stringPtr("three")},
{},
}
tests := []struct {

Loading…
Cancel
Save