Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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? %))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")]))) |