diff --git a/project.clj b/project.clj index 39ef48c..d6b9a1a 100644 --- a/project.clj +++ b/project.clj @@ -7,4 +7,5 @@ :dependencies [[org.clojure/clojure "1.6.0"] [compojure "1.3.1" :scope "provided"] [ring/ring-core "1.3.1" :scope "provided"] - [cheshire "5.3.1"]]) + [cheshire "5.3.1"] + [prismatic/schema "0.3.3"]]) diff --git a/src/clj_webtoolbox/routes/checked.clj b/src/clj_webtoolbox/routes/checked.clj index 76ea04f..9212a7f 100644 --- a/src/clj_webtoolbox/routes/checked.clj +++ b/src/clj_webtoolbox/routes/checked.clj @@ -2,6 +2,7 @@ (:require [compojure.core :refer [routing]] [ring.util.response :refer [response?]] + [schema.core :as s] [clj-webtoolbox.response :as response] [clj-webtoolbox.routes.core :refer [destructure-route-bindings]])) @@ -99,6 +100,16 @@ (if (f (get-in request k)) (safe request parent [param]))))) +(defn validate-schema + "Validates the specified parameter by checking it against the given schema. + If it validates, the parameter is marked safe. Follows the same rules for + param/parent handling as validate." + ([request param schema] (validate-schema request :params param schema)) + ([request parent param schema] + (let [k (if (sequential? param) (concat [parent] param) [parent param])] + (if (nil? (s/check schema (get-in request k))) + (safe request parent [param]))))) + (defn transform "Transforms the specified parameter using function f which gets passed the value of the parameter. A nested parameter can be transformed by specifying it as a