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

Translator for custom validation tag #551

Closed
bentcoder opened this issue Nov 29, 2019 · 2 comments
Closed

Translator for custom validation tag #551

bentcoder opened this issue Nov 29, 2019 · 2 comments

Comments

@bentcoder
Copy link

bentcoder commented Nov 29, 2019

Hi,

I wrote custom validator tag as shown below and it works fine. However, I couldn't find a way/example to add a custom translator to go with it so as a result the error message reads as Key: 'Item.founded_at' Error:Field validation for 'founded_at' failed on the 'iso_8601_date' tag when I use err.Translate(universal.Translator). How can I add custom translator for it? Any example or a link to it would be nice. All I want to use is {0} must be in valid ISO8601 date format. I looked into val.RegisterTranslation but cannot get my head around with the third and fourth arguments.

Thanks

Package version eg. v8, v9:

v9

Issue, Question or Enhancement:

Question

Code sample, to showcase or reproduce:

// register(validator)

func register(val *validator.Validate) {
	err := val.RegisterValidation("iso_8601_date", iso8601Date)
	if err != nil {
		panic(err)
	}
}

func iso8601Date(fl v9validator.FieldLevel) bool {
	_, err := time.Parse("2006-01-02", fl.Field().String())
	if err != nil {
		return false
	}

	return true
}
@bentcoder
Copy link
Author

Sorted

@bentcoder
Copy link
Author

Just in case if someone looks for same/similar thing.

validate: iso_8601_date
registerISO8601DateTag(val, trans)
func registerISO8601DateTag(val *v9validator.Validate, trans universal.Translator) {
	if err := val.RegisterValidation("iso_8601_date", handleISO8601DateTag); err != nil {
		panic(err.Error())
	}

	if err := val.RegisterTranslation(
		"iso_8601_date",
		trans,
		registerTranslator("iso_8601_date", "{0} must be in ISO 8601 date (yyyy-mm-dd) format"),
		translate,
	); err != nil {
		panic(err.Error())
	}
}

func handleISO8601DateTag(fl v9validator.FieldLevel) bool {
	if _, err := time.Parse("2006-01-02", fl.Field().String()); err != nil {
		return false
	}

	return true
}

func registerTranslator(tag string, msg string) v9validator.RegisterTranslationsFunc {
	return func(trans universal.Translator) error {
		if err := trans.Add(tag, msg, false); err != nil {
			return err
		}

		return nil
	}
}

func translate(trans universal.Translator, fe v9validator.FieldError) string {
	msg, err := trans.T(fe.Tag(), fe.Field())
	if err != nil {
		panic(fe.(error).Error())
	}

	return msg
}

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

No branches or pull requests

1 participant