This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-07-05
Channels
- # announcements (5)
- # babashka (2)
- # beginners (47)
- # calva (3)
- # cider (42)
- # clerk (5)
- # clj-commons (21)
- # clojure (145)
- # clojure-austin (25)
- # clojure-europe (14)
- # clojure-norway (9)
- # clojure-spec (7)
- # clojurescript (21)
- # datomic (4)
- # dev-tooling (4)
- # docker (5)
- # gratitude (5)
- # honeysql (6)
- # hyperfiddle (45)
- # jobs (1)
- # juxt (2)
- # malli (25)
- # meander (1)
- # mount (3)
- # other-languages (7)
- # pathom (7)
- # pedestal (1)
- # reagent (5)
- # reitit (20)
- # releases (2)
- # remote-jobs (1)
- # shadow-cljs (77)
- # squint (8)
- # tools-deps (34)
When I am using coercion, does that mean I can set my query-params to validate as keyword and then I get them in my request map as coerced to keywords?
(s/def ::direction (s/and keyword? #{:ascending :descending}))
(s/def ::column (s/and keyword? #{:name :email}))
(s/def ::filter-pattern string?)
["/table" {:get {:handler #(company/companies-table-page company-repo %)
:parameters {:query (s/keys :opt-un [::filter-pattern ::column ::direction])}
:responses {200 {:body string?
:header {:content-type "text/html"}}}}}]
I am not getting back keywords, it's still strings. Do i misunderstand coercion or what am I doing wrong?
{:filter-pattern "foo", :sorting "ascending", :column "name"}
I mean is coercion working with malli the way I intend? (I guess not since you said you are using malli in your project:)
it should work ? I really remember this working with query params. The trick was that you still have the original non-coerced query-params
key in the request map, but the data in {:parameters {:query ...}}
should be coerced
(although i remember migrating my schemas from spec to malli, spec coercion is a bit of a hack, malli was designed with coercion in mind)
I modified their example: https://github.com/metosin/reitit/tree/master/examples/just-coercion-with-ring
(ns example.spec
(:require [clojure.spec.alpha :as s]
[spec-tools.spec :as spec]
[reitit.coercion.spec :as spec-coercion]
[example.middleware :as middleware]))
;; wrap into Spec Records to enable runtime conforming
(s/def ::z keyword?)
(s/def ::request (s/keys :req-un [::z]))
;; read coerced parameters under :parameters
(defn handler [{{{:keys [z]} :query} :parameters}]
{:status 200
:body {:result (str z)
:source :spec}})
I did get a keywordized value:
curl
{"result":":a","source":"spec"}
odd, now it works... I was sure I tested it that way. thanks @U02F0C62TC1