go-playground / validator

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

Add methods for FieldError interface #802

Open ocelot93 opened 2 years ago

ocelot93 commented 2 years ago

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

I am working on validation for incoming spreadsheets. Validator has some custom tags, registered through either RegisterValidation or RegisterValidationCtx funcs. Each validation error is being translated, but I can't operate anything but FieldError interface while registering a translation.

It would be useful to add methods for FieldError interface such as StructLevel has, in case anyone wants to be able to work with reflection, e.g. to retrieve Top struct, or to get a struct tag. Because now for simply obtaining a slice index for occurred error, it's necessary to parse fe.Namespace() string

Example

Task: retrieve a spreadsheet name and spreadsheet row number for occurred error Current implementation: parsing fe.Namespace() string obtaining parent struct's name and slice index of occurred error, then retrieving calculated map from ctx Proposal: simply retrieve parent struct's tag using reflection; for row number - retrieve index of slice also using reflection

marshallford commented 6 months ago

@ocelot93 Did you find a workaround for this limitation -- in particular getting the index?

ocelot93 commented 1 week ago

@marshallford apologize for a delayed answer, I didn't find any solution for this, have to prematurely prepare a map through which I then seek for the needed value