go-playground / validator

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

In case of nested struct, if validation failed for inner field how would I get the json tag name for that field #409

Closed ghost closed 5 years ago

ghost commented 5 years ago

Package version eg. v8, v9:

v9

Issue, Question or Enhancement:

Question : I have struct Config which consist of Field SliceOfAnotherStruct which contained the slice of pointer to AnotherStruct. Now if validation failed for a field in AnotherStruct then validation error return me field name as Config.AnotherStruct[0].Bank Here how could I get the json tag for field Bank in AnotherStruct

Code sample, to showcase or reproduce:

type Config struct {
    SliceOfAnotherStruct         []*AnotherStruct        `bson:"esignType" json:"esignType" validate:"required,dive,required"`
}

type AnotherStruct struct {
    Name                   string        `bson:"name" json:"name" validate:"required"`
    Cost                   string        `bson:"cost" json:"cost" validate:"required"`
    Bank    string        `bson:"bank" json:"bank" validate:"required"`
    IFSC     string        `bson:"ifsc" json:"ifsc" validate:"required"`
}
deankarn commented 5 years ago

The documentation for this was recently updated in this PR

https://godoc.org/gopkg.in/go-playground/validator.v9#Validate.RegisterTagNameFunc

after registering such a function then the json name will be used...it can also be customized any way you wish for BSON name, really anything.

let me know if that helps?