This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-10-23
Channels
- # 100-days-of-code (2)
- # aws (1)
- # beginners (105)
- # boot (36)
- # calva (4)
- # cider (56)
- # clara (37)
- # cljdoc (16)
- # cljs-dev (19)
- # clojure (44)
- # clojure-dev (20)
- # clojure-italy (24)
- # clojure-nl (3)
- # clojure-serbia (2)
- # clojure-spec (15)
- # clojure-uk (44)
- # clojurescript (41)
- # code-reviews (3)
- # core-async (12)
- # cursive (24)
- # datomic (4)
- # emacs (1)
- # figwheel-main (10)
- # fulcro (168)
- # funcool (2)
- # hyperfiddle (15)
- # jobs (2)
- # jobs-discuss (79)
- # juxt (19)
- # lein-figwheel (1)
- # leiningen (2)
- # luminus (14)
- # mount (8)
- # nrepl (9)
- # off-topic (9)
- # other-languages (1)
- # pathom (32)
- # reitit (6)
- # ring-swagger (3)
- # shadow-cljs (10)
- # slack-help (11)
- # spacemacs (20)
- # sql (29)
- # tools-deps (28)
- # vim (29)
- # yada (4)
Hello, we're trying to use coercion to do backend validation with spec but kept having spec errors even when we're sending the correct data: [:spec "(spec-tools.core/spec {:spec (clojure.spec.alpha/keys :req [:app/invites]), :type :map, :spec-tools.parse/key->spec {:app/invites :app/invites}, :spec-tools.parse/keys #{:app/invites}, :spec-tools.parse/keys-req #{:app/invites}})"][:problems [{:path [], :pred "map?", :val nil, :via [:app.web.invite/request], :in []}]][:type :reitit.coercion/request-coercion][:coercion :spec][:value nil][:in [:request :body-params]]
Spec seems to think that our body is nil and we initially thought one of our middlewares was causing this, but that doesn't seem like the case. FYI We are sending edn from the front end, with application/edn as our Content-Type, and our top-level handler is created using (rr/ring-handler (rr/router ...))
. Our routes look something like this:
(rr/ring-handler
(rr/router
["" {:coercion rspec/coercion
:middleware [[wrap-protocol]
[wrap-defaults {:cookies true :params {:urlencoded true :keywordize true}}]]}
["/api" {:name "api routes"
:middleware [[wrap-defaults defaults/api-defaults]
[wrap-cors]
[wrap-edn-params]]
:options ok}
["" {:name "client api routes"
:middleware [[wrap-session-auth]]}
["/invites" {:name "api/invites"
:parameters {:body :app.web.invite/request}
:responses {200 :app.web.invite/response
201 :app.web.invite/response}
:post invites}]]]]
{:data {:middleware [rrc/coerce-exceptions-middleware
rrc/coerce-request-middleware
rrc/coerce-response-middleware]}})
(-> (rr/create-default-handler)
(wrap-defaults defaults/site-defaults)))
@evangelinechengg the middleware stack is not easiest to debug. My 2cents are that your edn-middleware is pushing the parsed params into :edn-params
, leaving the :body-params
nil. You can add a debugging middleware between every mw to see how they transform the request, here's how: https://metosin.github.io/reitit/ring/transforming_middleware_chain.html
I would recommend using Muuntaja for request & response formatting, here's a example: https://github.com/metosin/reitit/blob/master/examples/ring-spec-swagger/src/example/server.clj
that's what we suspected, we'll give Muuntaja a go and let you know how we get on 🙂