Skip to content
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

Open
SevereOverfl0w opened this issue Nov 7, 2015 · 6 comments
Open

Messages customized based on input #43

SevereOverfl0w opened this issue Nov 7, 2015 · 6 comments

Comments

@SevereOverfl0w
Copy link

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.

@theleoborges
Copy link
Owner

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?

@ghost
Copy link

ghost commented Jan 20, 2016

I like the message-fn approach, but it doesn't help this use case. Considering the state-monad pattern the library uses in the validate fn, it makes sense to propagate it to validators too.

(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." }])

@dLobatog
Copy link

I like @codepodu 's first approach here, returning a seq with [boolean possible-error-message] seems like the best approach. @LeonardoBorges have you looked at this lately?

@ghost
Copy link

ghost commented Apr 17, 2016

I'd be happy to send a pull request if the community likes it. @dLobatog @LeonardoBorges

@armlesshobo
Copy link

late to the game, but I like the way this looks.

@ghost
Copy link

ghost commented May 31, 2016

@LeonardoBorges What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants