-
Notifications
You must be signed in to change notification settings - Fork 38
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
Messages customized based on input #43
Comments
Thanks for raising this. It's been on my radar for a while but I haven't gotten to a solution I'm happy with. What would the public API look like? |
I like the (defvalidator max-count
[value maximum metadata]
(if (<= (count (seq value)) maximum)
[true nil]
[false (format "%s should be less than %d characters" (:key metadata) maximum)]))
(b/validate blog-post :headline [:max-count 50]) Or the messages could be made completely dynamic: (defvalidator max-count
[value maximum type metadata]
(let [cnt (count (seq value))]
(if (<= cnt maximum)
[true nil]
(cond
(= :hard type) [false {:errors [(format "%s should be less than %d characters" (:key metadata) maximum)]}]
(= :soft type) [true {:warnings [(format "Consider shortening %s(%d characters) %s" (:key metadata) cnt (:reason metadata)]}]))))
(b/validate blog-post [:headline [:max-count 83 :hard]])
(b/validate blog-post [:headline [:max-count 83 :soft] {:reason "for better SEO." }]) |
I like @codepodu 's first approach here, returning a seq with |
I'd be happy to send a pull request if the community likes it. @dLobatog @LeonardoBorges |
late to the game, but I like the way this looks. |
@LeonardoBorges What do you think? |
I'd like to propose the possibility of looking at a new method of grabbing error messages where the metadata for the function is created dynamically, and therefore can take parameters. This would allow for more useful error messages, e.g. "%s must be at least 5 long" as opposed to "it should be longer"
To do this, I imagine the statically defined would need to be replaced with functions.
The text was updated successfully, but these errors were encountered: