-
-
Save wilig/2574d3bba9a9540c1bb8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def validations | |
[[:username #(< (count %) 3) "Username must be more then two characters."] | |
[:username #(> (count %) 50) "Username must be less then fifty characters."] | |
[:password #(< (count %) 3) "Password must be more then two characters."] | |
[:password #(> (count %) 50) "Password must be lass then fifty characters."]]) | |
(defn- valid | |
[validation] | |
(let [[value pred message] validation] | |
(if (pred value) | |
message | |
nil))) | |
(defn validate | |
[fields] | |
(filter #(not (nil? %)) | |
(map #(valid %) | |
(map #(replace fields %) | |
(filter #(not (nil? (some (set (keys fields)) %))) validations))))) | |
;; Example usage: | |
(validate {:username "me"}) ; -> (Username must be more then two characters.) | |
(validate {:username "user" :password "p"}) ; -> (Password must be more then two characters) | |
(validate {:username "me" :password "pa"}) ; -> (Username must be more then two characters, Password must be more then two characters) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment