This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-11-11
Channels
- # asami (19)
- # babashka (41)
- # beginners (115)
- # biff (7)
- # calva (78)
- # clj-kondo (29)
- # cljs-dev (9)
- # clojure (39)
- # clojure-europe (17)
- # clojure-gamedev (29)
- # clojure-nl (1)
- # clojure-norway (9)
- # clojure-spec (2)
- # clojure-uk (3)
- # clojurescript (7)
- # core-async (26)
- # cursive (16)
- # datomic (13)
- # emacs (1)
- # events (5)
- # fulcro (2)
- # funcool (4)
- # gratitude (1)
- # helix (1)
- # holy-lambda (1)
- # humbleui (1)
- # introduce-yourself (4)
- # java (1)
- # jobs (2)
- # jobs-discuss (9)
- # lsp (28)
- # matcher-combinators (2)
- # mathematics (1)
- # membrane (1)
- # nbb (12)
- # off-topic (10)
- # pathom (52)
- # polylith (38)
- # portal (32)
- # re-frame (4)
- # reagent (16)
- # reitit (2)
- # remote-jobs (1)
- # reveal (1)
- # rewrite-clj (10)
- # sci (67)
- # shadow-cljs (45)
- # squint (1)
- # tools-build (13)
- # tools-deps (16)
Hello Clojurians, I've come across a rather confusing coercion behaviour - under :body-params
coercion just throws validation error and under :form-params
it does the coercion:
Error:
(def app
(ring/ring-handler
(ring/router
[
["/" {:name :root
:get (fn [_] {:status 200 :body "pong"})
:post {:coercion rcm/coercion
:parameters {:body [:map [:id uuid?]]}
:handler (fn [{:keys [parameters]}]
{:status 200
:body (-> parameters :form)})}}]]
{:data {:middleware [rrc/coerce-exceptions-middleware
rrc/coerce-request-middleware
rrc/coerce-response-middleware]}})))
(app
{:request-method :post
:uri "/"
:body-params {:id "644c1b76-4c3c-4067-8aeb-86236bcb5538"}})
{:status 400,
:body {:schema "[:map {:closed true} [:id uuid?]]",
:errors ({:path [:id],
:in [:id],
:schema "uuid?",
:value "644c1b76-4c3c-4067-8aeb-86236bcb5538",
:message "should be a uuid"}),
:value {:id "644c1b76-4c3c-4067-8aeb-86236bcb5538"},
:type :reitit.coercion/request-coercion,
:coercion :malli,
:in [:request :body-params],
:humanized {:id ["should be a uuid"]}}}
Correct:
(def app
(ring/ring-handler
(ring/router
[
["/" {:name :root
:get (fn [_] {:status 200 :body "pong"})
:post {:coercion rcm/coercion
:parameters {:form [:map [:id uuid?]]}
:handler (fn [{:keys [parameters]}]
{:status 200
:body (-> parameters :form)})}}]]
{:data {:middleware [rrc/coerce-exceptions-middleware
rrc/coerce-request-middleware
rrc/coerce-response-middleware]}})))
(app
{:request-method :post
:uri "/"
:form-params {:id "644c1b76-4c3c-4067-8aeb-86236bcb5538"}})
Result:
{:status 200, :body {:id #uuid"644c1b76-4c3c-4067-8aeb-86236bcb5538"}}