Created
July 21, 2010 06:16
-
-
Save Kaali/484140 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
(defn validator [param forms] | |
(remove nil? | |
(map (fn [[f s]] (when-not (f param) s)) forms))) | |
(defn- validator-wrapper [forms] | |
`(fn [param#] (validator param# ~forms))) | |
(defn form* [forms] | |
(apply merge | |
(map (fn [[k v]] {k (validator-wrapper v)}) (apply array-map forms)))) | |
(defmacro defform | |
"Define a web form for validation" | |
[name & forms] | |
(let [validators (form* forms)] | |
`(defn ~name [params#] | |
(apply merge | |
(map (fn [[k# v#]] {k# ((k# ~validators) v#)}) params#))))) | |
(defn required [param] (not (nil? param))) | |
(defn minimum-length [minlen] | |
(fn [param] (>= (count param) minlen))) | |
(defform test-form | |
:login [[required "Login is required"]] | |
:password [[required "Password is required"] | |
[(minimum-length 5) "Password should be longer than 5 chars"]]) |
The newest version works, the problem was with the scope of Fns. I guess.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The test-form defined doesn't work:
which results in: