Skip to content

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

Description

@fzlzjerry

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

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions