Open navamedha opened 9 months ago
@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:
reflect.Value
or StructField
to the validation failures and let it be extracted dynamically there which may benefit a could other future things.map[int]string``validate:"gt=0,dive,keys,eq=1|eq=2,endkeys,required"
<- three different errors can occur at 3 different levelsHave been waiting for something like this for a while now!
I've currently pulled this PR in manually and am using it in my go.mod
- it's working really well, so great job. 🎉
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 guess, the PR is far from ready then. For example, It seems like does not support multiple messages per validation per struct field.
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