Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FieldError Field function returns struct name instead of JSON tag #337

Closed
kerak19 opened this issue Jan 15, 2018 · 10 comments · May be fixed by #1358
Closed

FieldError Field function returns struct name instead of JSON tag #337

kerak19 opened this issue Jan 15, 2018 · 10 comments · May be fixed by #1358
Assignees
Labels

Comments

@kerak19
Copy link

kerak19 commented Jan 15, 2018

I do have this struct:

data := struct {
    Email    string `validate:"required,email" "json:"email"`
    Password string `validate:"required,min=8" "json:"password"`
}{}

Then i'm using validate.Struct(data) and i'm ranging over ValidationErrors.
Every Field() on error in ValidationErrors returns struct field instead of JSON tag.
For example, data.Email has json tag "email", but it retuns "Email" instead.
Why is that? Documentation clearly says it should return JSON tag.

@deankarn deankarn self-assigned this Jan 15, 2018
@deankarn
Copy link
Contributor

Hey @kerak19 can you point out exactly where in the documentation that it states it returns the json name by default? and I'll correct right away.

as for grabbing the json tag name, you have to register a tag name parser first eg.

        validate := New()
	validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
		name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
		if name == "-" {
			return ""
		}
		return name
	})

it's done this way in order to allow for parsing of any tag name like json, bson, sql tags etc...

Please let me know if this was helpful :)

@kerak19
Copy link
Author

kerak19 commented Jan 16, 2018

So i probably misunderstood this and thought it's default to take first existing tag name:

// returns the fields name with the tag name taking precedence over the
// fields actual name.
//
// eq. JSON name "fname"
// see ActualField for comparison
Field() string

Thanks for your answer! :)

@taylorkline
Copy link

I also understood that "the tag name taking precedence over the fields actual name" meant that err.Field() would give me the JSON tag name by default.

@pnicolcev-tulipretail
Copy link

The documentation definitely makes it look like it will pick something like the JSON tag name. I think it would be very useful to add a sentence to the documentation saying something like "You must implement RegisterTagNameFunc if you want to use a tag rather than the struct field name."

@deankarn
Copy link
Contributor

Thanks @pnicolcev-tulipretail I agree, any chance you could make a PR?

@shenxianghong
Copy link

shenxianghong commented Oct 31, 2019

Hey @kerak19 can you point out exactly where in the documentation that it states it returns the json name by default? and I'll correct right away.

as for grabbing the json tag name, you have to register a tag name parser first eg.

        validate := New()
	validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
		name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
		if name == "-" {
			return ""
		}
		return name
	})

it's done this way in order to allow for parsing of any tag name like json, bson, sql tags etc...

Please let me know if this was helpful :)

type RegisterAccountForm struct {
	Account         int `json:"account" validate:"required"`
	Password        string `json:"password" validate:"required"`
	ConfirmPassword string `json:"confirm_password" validate:"required,eqfield=Password"`
}

but tag "eqfield" seems doesn't work,It returns "password必须等于ConfirmPassword"

@rjcrystal
Copy link

For folks looking to implement this with gin gonic router here's how you can register the RegisterTagNameFunc

router := gin.New()

if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
	v.RegisterTagNameFunc(func(fld reflect.StructField) string {
		name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
		if name == "-" {
			return ""
		}
		return name
	})
}
//other router operations 

@BarcoMasile
Copy link

Hi, apparently the doc is still not updated. Any news on this?

@ameghdadian
Copy link

I've registered tag name parser:

	validate = validator.New()

	validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
		name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
		if name == "-" {
			return ""
		}
		return name
	})

I attempt to validate following struct:

type Credentials struct {
	Password        string   `json:"password" validate:"required"`
	PasswordConfirm string   `json:"password_confirm" validate:"eqfield=Password"`
}

However, I'm getting this error:
password_confirm must be equal to Password

What I expected to see:
password_confirm must be equal to password

Am I missing something?

@nodivbyzero
Copy link
Contributor

@ameghdadian Could you ask this question in the Discussions section here: https://github.com/go-playground/validator/discussions?
I believe Discussions is a more appropriate place to ask questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants