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
Predicate validation #23
Conversation
|
Yeah, forget about: (presence-of :field :when predicate?) I agree that it could get messy and it's unnecessary. I find (defn new? [user]
(nil? (:id user)))
(defn unique-email? [user]
(if-let [existing (find-by-email (:email user)]
(= (:id user) (:id existing))
true))
(def validate
(validation-set
(presence-of :email)
(validate :email unique-email? :message "is already taken")
(validate-when new? (presence-of :password))
(validate-when #(contains? % :password) (presence-of :password)))) Does that make more sense? |
Yes, it does now. Please submit a pull request. Thank you! |
Added the two functions and tests. The generic |
|
||
Example: | ||
|
||
(use 'validateur.validation) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use clojure.core/use
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's just in the comment and I included it to be consistent with the rest of the examples. Perhaps changing all the examples is best saved for another issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.
I'd rename |
Why not give |
In |
Great- thanks! |
@michaelklishin Just tried using |
I guess the |
Try |
It's working. Thanks a lot! |
Curious how you feel about introducing a validator that accepts arbitrary predicate functions. I find that I already have useful functions that return
true
orfalse
but validateur expects something of the form[valid? {field #{msg}}]
.One potential solution:
Example usage:
Similarly it would be nice to be able to apply validators selectively:
As it is currently, I believe the
:when predicate
would have to be added separately to each validator. Perhaps some of the:message
/:message-fn
(and perhaps:when
) handling could be extracted into something more reusable.Alternatively, I'm currently using:
Example usage:
I'd be happy to submit a pull request if any of this is something you're interested in including.