reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
Misha Bohdan 2020-04-06T15:54:49.053800Z

Hi guys! Does anyone know how to coerce query param into a record? I have the following spec

(s/def ::my-data
  (ds/spec
    {:name ::my-data
     :spec (s/or :my-data/string (s/and spec/string? (comp (partial = 10) count)))
                 :my-data/struct my-struct?)
     :gen my-data/gen
     :decode/json #(str->my-data %2)
     :ecnode/json #(my-data->str %2)
     :decode/string #(str->my-data %2)
     :ecnode/string #(my-data->str %2)}))
When I calling coercion (spec-tools.core/coerce ::my-data spec-tools.core/string-transformer) or (spec-tools.core/coerce ::my-data spec-tools.core/json-transformer) it works, but if I call throught router – don’t. reitit.coercion.spec/coercion added at the top of router declaration. Any ideas?

Misha Bohdan 2020-04-07T08:45:04.067200Z

Sorry, I looked at my code to check `(spec-tools.core/spec …)` and found that it already used.

Misha Bohdan 2020-04-07T13:18:47.074400Z

Solution found: extending base reitit.coercion.spec/default-options with somethis like this

(def custom-transformer
   (st/type-transformer
     {:name     ::custom-transformer
      :decoders {:my-data #(str->my-data %2)}
      :encoders (:my-data #(my-data->str %2))}))

Misha Bohdan 2020-04-06T15:56:10.054600Z

PS if replace my spec to int? or something common – coercing works fine.

2020-04-06T16:51:20.055100Z

there's a typo: ecnode -> encode

Misha Bohdan 2020-04-06T17:57:16.055300Z

It’s just an example the reason not in typo, unfortunately.