go-playground / validator

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

Is it possible to use the json tag to show the error message? #1175

Open jadetang opened 1 year ago

jadetang commented 1 year ago

Package version eg. v9, v10:

Issue, Question or Enhancement:

Hello, we are using the package to validate our HTTP request body. To generate better message, is it possible to use the JSON tag instead of the validate tag?

Example:

type Address struct {
    Street string `validate:"required", json:"strass"`
    City   string `validate:"required"`
    Planet string `validate:"required"`
    Phone  string `validate:"required"`
}

In case the String is empty, is it possible to generate an error message using the strass?

strass is required

Code sample, to showcase or reproduce:

bernd commented 11 months ago

@jadetang You can use RegisterTagNameFunc to do that. (see the example in the docs)