go-playground / validator

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

Pipe operator returns incorrect error Tags #926

Open ThePenguin1140 opened 2 years ago

ThePenguin1140 commented 2 years ago

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

piping lt|ltfield= does not enter a proper tag name into the FieldError if validation fails

Code sample, to showcase or reproduce:

Struct + validate example:

type DateRange struct {
    Start time.Time `validate:"lt|ltfield=End"`
    End time.Time `validate:"gt|gtfield=Start"`
}

Outputs an error with tag gt|gtfield=Start instead of gt or gtfield.

working example: https://go.dev/play/p/M5ztyLUs_TE

type DateRange struct {
    Start time.Time `validate:"lt|ltfield=End"`
    End   time.Time `validate:"gt|gtfield=Start"`
}

func main() {
    start, _ := time.Parse("2021-03-30T00:00:00-03:00", time.RFC3339)
    end, _ := time.Parse("2020-03-31T00:00:00-03:00", time.RFC3339)
    d := DateRange{
        Start: start,
        End:   end,
    }

    v := validator.New()

    if err := v.Struct(d); err != nil {
        if valErrs, ok := err.(validator.ValidationErrors); ok {
            for _, e := range valErrs {
                fmt.Println(e.Tag())
            }
        }
    } else {
        fmt.Println("OK")
    }
}
eddiedane commented 3 months ago

Is there any update on this? the behaviour is really counter intuitive, and its a nightmare when translating and mapping error messages