#1627·validator

[Bug]: unique confuses nil with an anonymous empty struct

Author: fzlzjerryCreated Sep 9, 2026Updated Sep 9, 2026

What happened?

unique treats a nil pointer and a non-nil pointer to an anonymous empty struct as duplicates. The equivalent slice using a named empty struct passes. Map values have the same false positive.

The current nil marker in isUnique is struct{}{}, which is also a valid underlying value for these inputs. Nil should remain distinct from a non-nil empty struct. Duplicate nils and duplicate non-nil values should still fail as they do now.

I'd like to fix the internal marker collision and add regressions for slices, map values and unique=Field, preserving the existing nil semantics.

Version

Current master: dfe35cf8317892133dfe31e36054dcbf36aab604. Go 1.26.8, linux/amd64.

Example Code

go
package main

import (
    "fmt"
    "github.com/go-playground/validator/v10"
)

type Empty struct{}

func main() {
    v := validator.New()
    fmt.Println(v.Var([]*struct{}{nil, new(struct{})}, "unique"))
    fmt.Println(v.Var([]*Empty{nil, new(Empty)}, "unique"))
    fmt.Println(v.Var(map[string]*struct{}{"nil": nil, "empty": new(struct{})}, "unique"))
}

The first and third calls return a unique validation error; the second returns nil. All three should pass. Controls with two nil pointers or two non-nil empty structs already fail correctly.