-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
feat: add custom error message to struct field and to struct level validation #1183
base: master
Are you sure you want to change the base?
Conversation
and to struct level validation
@navamedha TY for the PR. Now that validation tags run properly on structs themselves this is something I've been thinking about adding. This is a variant, one of many, I've been thinking about implementing and need to give it some thought. Some context is I'm still thinking about how to handle 2 things:
|
Have been waiting for something like this for a while now! I've currently pulled this PR in manually and am using it in my Of course, I'd love to move back to an official tag sooner rather than later, so am keen to see what @deankarn cooks up before this is eventually merged. |
Any plan to move this forward? Also, does this PR consider placeholders in the message? |
Sorry all, not sure when I am going to be able to get to this and many other PR’s as I am swamped with other projects at the moment. I could really use some help maintaining this package. high level thoughts are I think this is a good stopgap solution for this version of the package. The next version and better option I would change the validations function signature to return error instead and let people customize their error return messaging as they see fit along with stripping out translation dependencies. |
Thanks for the update. I would like to be able to do something like this. type UserParams struct {
UserId string `validate:"required(message = 'UserID is not provided'), uuid4(message = 'UserID is not a valid UUID4!')"`
} In a similar way to how we do it in for example the Rust world. That one should be great to have. |
Add custom error message to struct field and to struct level validation.
Enhances
struct field
We can now give a custom error message to a struct field by adding a struct tag and give that struct tag to
validate.RegisterErrMsgFunc
, like we give json struct tag tovalidate.RegisterTagNameFunc
.Custom error message can be accessed using
err.Msg()
as following.struct level
We can now add a custom error message while registering validation at struct level using
validate.RegisterStructValidation
.Custom error message can be accessed using
err.Msg()
as following.Make sure that you've checked the boxes below before you submit PR:
Created a test --> TestCustomErrorMessages
@go-playground/validator-maintainers