allow for either assoc or merge to get body content into :safe-params

This commit is contained in:
Gered 2015-01-05 13:23:38 -05:00
parent 14a462940d
commit f4527253b7

View file

@ -102,10 +102,13 @@
params))) params)))
(defn safe-body (defn safe-body
"Marks the body as safe. The request :body value is copied to :safe-params under "Marks the body as safe. The request's :body value is either copied into
the :body key (mainly for ease of access via parameter destructuring in routefn)." :safe-params under the :body key, or is merged into the existing :safe-params
[request] map (controlled via the copy-into-params? argument)."
(assoc-in request [:safe-params :body] (:body request))) [request & [copy-into-params?]]
(if copy-into-params?
(assoc-in request [:safe-params :body] (:body request))
(update-in request [:safe-params] merge (:body request))))
(defn validate (defn validate
"Validates the specified parameter using function f which gets passed the value "Validates the specified parameter using function f which gets passed the value
@ -133,17 +136,17 @@
"Validates the request body using function f which gets passed the body of "Validates the request body using function f which gets passed the body of
the request. If f returns a 'truthy' value, the body is marked safe. You the request. If f returns a 'truthy' value, the body is marked safe. You
likely will want to transform the body first before validation." likely will want to transform the body first before validation."
[request f] [request f & [copy-into-params?]]
(if (f (:body request)) (if (f (:body request))
(safe-body request))) (safe-body request copy-into-params?)))
(defn validate-body-schema (defn validate-body-schema
"Validates the request body by checking it against the given schema. If it "Validates the request body by checking it against the given schema. If it
validates, the body is marked safe. You likely will want to transform the body validates, the body is marked safe. You likely will want to transform the body
first before validation." first before validation."
[request schema] [request schema & [copy-into-params?]]
(if (nil? (s/check schema (:body request))) (if (nil? (s/check schema (:body request)))
(safe-body request))) (safe-body request copy-into-params?)))
(defn transform (defn transform
"Transforms the specified parameter using function f which gets passed the value "Transforms the specified parameter using function f which gets passed the value