go-playground / validator

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

How to get index current validated struct inside custom validation #1182

Open init-database opened 9 months ago

init-database commented 9 months ago

Package version eg. v10:

Issue, Question or Enhancement:

This ilustrate my problem:

type Parent struct { Childs []Child validate:"dive" }

type Child struct { JustAField string validate:"customValidation" }

func customValidation(fl validator.FieldLevel) bool { // here I want to get the index of child // which currently being validated }

errs := validate.Struct(Parent) I cannot find a method to get current index of child inside customValidation,

But if there is an error, somehow the library itself knows the index:

for _, e := range errs { e.Namespace() // Parent.childs[0].just_a_field } does anyone know how to get the index?

thanks

Code sample, to showcase or reproduce:

marshallford commented 7 months ago

@init-database Did you ever solve this? Seems related to this other issue: https://github.com/go-playground/validator/issues/802