Skip to content

Instantly share code, notes, and snippets.

@wilig
Created February 7, 2010 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilig/2574d3bba9a9540c1bb8 to your computer and use it in GitHub Desktop.
Save wilig/2574d3bba9a9540c1bb8 to your computer and use it in GitHub Desktop.
(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