Skip to content

Commit

Permalink
Initial validation code
Browse files Browse the repository at this point in the history
  • Loading branch information
zk committed Mar 1, 2015
1 parent 91a0bd8 commit b82291d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/clj/nsfw/validate.clj
@@ -0,0 +1,21 @@
(ns nsfw.validate
(:refer-clojure :exclude [key]))

(defn run [payload vs]
(->> vs
(map (fn [f]
(f payload)))
(apply merge-with concat)))

(defn key [k & opts]
(let [msg (last opts)
fns (butlast opts)]
(fn [pl]
(let [pass? (->> fns
(map (fn [f]
(f (get pl k))))
(reduce #(and %1 %2)))]
(if-not pass?
{k [msg]})))))

(def not-empty? #(not (empty? %)))
24 changes: 24 additions & 0 deletions test/clj/nsfw/validate_test.clj
@@ -0,0 +1,24 @@
(ns nsfw.validate-test
(:require [clojure.test :refer :all]
[nsfw.validate :as v]))


(deftest test-key
(is ((v/key :foo v/not-empty? "error") {})))

(deftest test-run
(is (v/run
{:asdf "bar"}
[(fn [pl]
(if (empty? (:foo pl))
{:foo [":foo can't be empty"]}))
(fn [pl]
(when (empty? (:bar pl))
{:bar [":bar can't be empty"]}))
(fn [pl]
(if (empty? (:foo pl))
{:foo [":foo some other thing"]}))
(fn [pl]
(if (empty? (:foo pl))
{:message "Here's a general message"}))
(v/key :foo v/not-empty? "Key message not empty")])))

0 comments on commit b82291d

Please sign in to comment.