From f4527253b74613a7aab841379c44589f5ec5c9be Mon Sep 17 00:00:00 2001 From: gered Date: Mon, 5 Jan 2015 13:23:38 -0500 Subject: [PATCH] allow for either assoc or merge to get body content into :safe-params --- src/clj_webtoolbox/routes/checked.clj | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/clj_webtoolbox/routes/checked.clj b/src/clj_webtoolbox/routes/checked.clj index 60dfcd1..6936663 100644 --- a/src/clj_webtoolbox/routes/checked.clj +++ b/src/clj_webtoolbox/routes/checked.clj @@ -102,10 +102,13 @@ params))) (defn safe-body - "Marks the body as safe. The request :body value is copied to :safe-params under - the :body key (mainly for ease of access via parameter destructuring in routefn)." - [request] - (assoc-in request [:safe-params :body] (:body request))) + "Marks the body as safe. The request's :body value is either copied into + :safe-params under the :body key, or is merged into the existing :safe-params + map (controlled via the copy-into-params? argument)." + [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 "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 the request. If f returns a 'truthy' value, the body is marked safe. You likely will want to transform the body first before validation." - [request f] + [request f & [copy-into-params?]] (if (f (:body request)) - (safe-body request))) + (safe-body request copy-into-params?))) (defn validate-body-schema "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 first before validation." - [request schema] + [request schema & [copy-into-params?]] (if (nil? (s/check schema (:body request))) - (safe-body request))) + (safe-body request copy-into-params?))) (defn transform "Transforms the specified parameter using function f which gets passed the value