go-playground / validator

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
MIT License
16.79k stars 1.32k forks source link

`unique` validator for map with Pointer value is not properly validated #1060

Closed vijaynallagatla closed 1 year ago

vijaynallagatla commented 1 year ago

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

Issue: Validator tag unique on kind Map with pointer value doesn't seem to be evaluating properly.

Code sample, to showcase or reproduce:

package main

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

type Class struct {
    Sub map[string]*string `validate:"unique"`
}

var validate *validator.Validate

func main() {
    validate = validator.New()
    validateStruct()
}

func validateStruct() {
    data := Class{
        Sub: map[string]*string{
            "one": strPtr("abc"),
            "two": strPtr("abc"),
        },
    }

    nonPointer := struct {
        Sub map[string]string `validate:"unique"`
    }{
        Sub: map[string]string{
            "one": "abc",
            "two": "abc",
        },
    }

    err := validate.Struct(data)
    if err != nil {
        fmt.Println("Error with pointer validation %w", err)
    }

    err = validate.Struct(nonPointer)
    if err != nil {
        fmt.Println("Error with struct validation %w", err)
    }

}

func strPtr(a string) *string {
    return &a
}

Play: https://go.dev/play/p/9WDr7hclSmA