gin: Custom Binding Error Message
After calling c.Bind(&form), is it possible to provide a custom validation error message per field rather than the generic “Field validation for ‘…’ failed on the ‘…’ tag”
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 3
- Comments: 32 (4 by maintainers)
Let me add my 2 cents here 😄
I have an error handling middleware that handles all the parsing for me. Gin allows you to set different types of errors, which makes error handling a breeze. But you need to parse the bind errors ‘manually’ to get nice responses out. All of it can be wrapped in 3 stages:
You can see the all three in action below, but what’s most important here is the
case gin.ErrorTypeBind:andValidationErrorToText(). The below could definitely be optimized, but so far it works great for my apps!If you use something like this together with the binding middleware, your handlers never need to think about errors. Handler is only executed if the form passed all validations and in case of any errors the above middleware takes care of everything!
Hope it helps a little 😄
I know this is old but I took liberty and try to little modify the code of @nazwa in accordance with “gopkg.in/go-playground/validator.v8” and also to get errors a little bit more readable
P.S @nazwa thanx for your solution really appreciate it!
just my 2 cents, but it can be solved one of two ways:
binding.Validatorto allow it to be overridden as beforeToo keep Gin configurable I would expose
binding.Validatorno matter the decision. I also cannot recommend updating to v9 enough, breaking or not(but I am a little bias)just for everyones information as of validator v9.1.0 custom validation errors are possible and are i18n and l10n aware using universal-translator and locales
click here for instructions to upgrade gin to validator v9
Hi
I suggest to try the third part package ShyGinErrors
first, you can define the validate rule with customize error message key in the data model.
then, we can initialize the ShyGinError and use it to parse the err return by gin.BindJson()
Okey, let me drop another stone into the well. I upgraded the code of @gobeam,@nazwa for the v10 validator.
@sudo-suhas thanks, that should work for me
Working link for future reference - https://github.com/go-playground/validator/tree/v9/_examples/gin-upgrading-overriding
Oh I changed the examples folder to _examples a while ago to avoid pulling in any external example dependencies, if any, when using go get, just modify the URL and the example is still there
@ismailbayram I assume it is because you are using
c.Bindorc.BindJSONinstead ofc.ShouldBindJSON.c.Bindsets content type totext/plainunder the hood with next line of code:c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind)@javierprovecho What do you suggest? Shall I make a PR to remove
RegisterValidationfrom the interface so that we are not locked intovalidator@v8? Or perhaps move forward with #1015?@joeybloggs This link is broken - https://github.com/go-playground/validator/tree/v9/examples/gin-upgrading-overriding
@joeybloggs That is what I’m currently doing… would like a way to set custom error messages… doing it that way just seems redundant and would rather just not use
c.Bindas my code would be much neater and cooler without it… at this point I really don’t see the point ofc.Bind