Comments (2)
Hello @fromYukki ,
Thanks for the question, a few people have asked on the side, but good to have it in an issue. I apologize for being so long winded but it's a complicated explanation; even I forget why I didn't implement this on occasion.
Summary
I thought about the ability to add custom error messages allot during the development of this package and every time have decided against it, the main reason is because of internationalization. Because validator is designed to be defined and used as a global object it is not tied to any specific context; I think the best way to describe the issue is to give an example of how I use the package myself.
Example
I have validator defined in an internationalized web application as a global variable.
Each http request could be from any user with any locale and validator has no line of site to what locale, nor which i18n package is being used; this is one of the main reasons I return a map of *FieldError for creating your own messages. Even if I added another parameter as type interface{} to accept anything, like the i18 package or locale for translation, you the user would still have to specify a custom validation for each tag; but it's not as simple as that either as the tags can apply to multiple types i.e. len can apply to string, int, float, array, map and so forth so a single message per tag isn't enough as if validation failed for a string you may want to give an error about number of characters, but if int the error would be about the int value; which means that the custom messages would have to be by tag, by type or specify a function to run for each tag which checks the type and I would have to go the function route as I would need to pass the locale and i18n information to the function.
At this point validator would be clogged up with all of this code and translation info and it would actually be more complicated and ugly than just ranging over the map and doing a switch on the tag. This is the way I do my translations, I have created a common function which accepts the map of errors and returns a map of translated errors and has proved to be very maintainable.
Final Thoughts
Unless I can find a better way, or someone could show me a better way I will likely not implement the ability to specify custom errors inline ( I am totally open to any ideas you may have )
I realize a good number of people won't care about internationalization, however there are also a great number who do, and happens to be a requirement for me.
was able to explain why dealing with your own message creation may be the best option? and like I said I am open to ideas
from validator.
P.S. I've been meaning to add an example with a default switch and default messages that users could copy, modify and add to in order to save some time. Guess I'll have to get on that a little quicker :)
from validator.
Related Issues (20)
- Cron validator is not correct
- Can't validate both `keys` and the struct value of maps of structs
- StructPartial does not dive to slice of structs HOT 1
- Proposal: Support validator chaining
- Multiple validations at one time
- Bool to struct{} in iso codes
- Failing validation using the base64url tag
- SSN regex doesn't match the rest of the validation and is loose with formatting rules
- Add support for `errors.As`
- err.Translate(trans) only works if translator passed via param (go-playground/validator)
- Is is possible to check if an array is greater then 0 in required_if
- Is it possible to register custom validation function directly returning errors? HOT 1
- Support for mutually exclusive groups of validations chained with OR
- security: update mimetype dependency to new secure version
- Partial struct validation does not validate child structs HOT 1
- Validation failed url with required_without
- validator for IBAN HOT 4
- Missing FieldError.Field() and 'Key' in ValidationErrors Returned from Var() and ValidateMap()
- Github sponsorhip HOT 1
- Panic on max, min Validation Tag with struct Fields in Version v10.20.0 and above
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from validator.