Fork me on GitHub
#reitit
<
2023-07-05
>
timo15:07:36

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"}

dharrigan15:07:48

I found that no-matter what, they aren't coerced, so I had to do it manually

timo15:07:01

thanks, took me two hours now come up with this suspicion. is it working with malli?

dharrigan15:07:12

yes, the startrek project is all malli

dharrigan15:07:17

but I suppose it would work with spec too 🙂

timo15:07:52

I mean is coercion working with malli the way I intend? (I guess not since you said you are using malli in your project:)

dharrigan15:07:09

ah, no. had to do it manually in the middleware as shown

dharrigan15:07:18

malli didn't coerce

dharrigan15:07:21

the query parameters.

timo15:07:28

alright, thanks for the help

timo15:07:43

good project btw, will take a look

dharrigan15:07:56

thanks. needs a bit of updating/tlc 🙂

rolt16:07:04

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

rolt16:07:46

(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)

rolt16:07:27

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"}

timo07:07:03

mmh :thinking_face:

timo08:07:50

odd, now it works... I was sure I tested it that way. thanks @U02F0C62TC1