Fork me on GitHub
#reitit
<
2020-04-06
>
Misha Bohdan15:04:49

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?

caio16:04:20

there's a typo: ecnode -> encode

Misha Bohdan17:04:16

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

ikitommi05:04:23

should it be (spec-tools.core/spec …)instead?

Misha Bohdan08:04:04

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

Misha Bohdan13:04:47

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 Bohdan15:04:10

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