validator icon indicating copy to clipboard operation
validator copied to clipboard

How to get index current validated struct inside custom validation

Open init-database opened this issue 8 months ago • 1 comments

  • [ ] I have looked at the documentation here first?
  • [ ] I have looked at the examples provided that may showcase my question here?

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:


init-database avatar Oct 20 '23 01:10 init-database

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

marshallford avatar Dec 06 '23 22:12 marshallford