GithubHelp home page GithubHelp logo

Comments (2)

deankarn avatar deankarn commented on August 15, 2024

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.

deankarn avatar deankarn commented on August 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.