This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-10-30
Channels
- # announcements (15)
- # beginners (143)
- # boot (2)
- # calva (48)
- # cider (93)
- # cljsrn (2)
- # clojure (127)
- # clojure-europe (3)
- # clojure-italy (8)
- # clojure-losangeles (8)
- # clojure-nl (10)
- # clojure-spec (67)
- # clojure-uk (51)
- # clojurescript (20)
- # cursive (9)
- # data-science (2)
- # datomic (10)
- # duct (13)
- # figwheel-main (1)
- # fulcro (74)
- # instaparse (10)
- # jobs (3)
- # joker (8)
- # juxt (4)
- # lumo (1)
- # malli (11)
- # nrepl (3)
- # off-topic (4)
- # pathom (5)
- # pedestal (6)
- # planck (5)
- # re-frame (18)
- # reagent (5)
- # reitit (17)
- # shadow-cljs (165)
- # sql (30)
- # vim (12)
- # xtdb (6)
You know a path-param, if it's an id, say 10
, is there a built-in-way to say, extract this out as a number, rather than a string? Obviously, I could call (Integer/parseInt)
on it, but I was wondering if there is something in the configuration that would just do that coercion for me automatically
@dharrigan coersion does that for you automatically. Are you on frontend, backend or both?
{:data {:coercion reitit.coercion.spec/coercion
:middleware [rrc/coerce-exceptions-middleware
rrc/coerce-request-middleware
rrc/coerce-response-middleware]}})))
@dharrigan the coerced parameters are under :parameters
in request, did you look there?
(defn get-starship-by-id
[{{{:keys [id]} :path} :parameters}]
{:status 200 :body (-> (db/find-starship-by-id id)
(utils/map->string))})
(def router
(ring/router
["/api"
["/starships" get-starships]
["/starships/:id" {:get get-starship-by-id
:coercion reitit.coercion.schema/coercion
:parameters {:path {:id s/Int}}}]]
{:data {:middleware [rrc/coerce-exceptions-middleware
rrc/coerce-request-middleware
rrc/coerce-response-middleware]}}))
Good to hear you got it working. The route data gets accumulates from root to leaves, it's a good practise to out :coercion
to top-level, effecting all routes. Only reason not to do this is if you want to mix & match different coercion impls in a same routing tree.
(def router
(ring/router
["/api"
["/starships" get-starships]
["/starships/:id" {:get get-starship-by-id
:parameters {:path {:id s/Int}}}]]
{:data {:coercion reitit.coercion.schema/coercion
:middleware [rrc/coerce-exceptions-middleware
rrc/coerce-request-middleware
rrc/coerce-response-middleware]}}))