Fork me on GitHub
#reitit
<
2020-01-15
>
bartuka03:01:08

hi, how are you? I'm starting a new app and want to give reitit a try 😃

bartuka03:01:16

I want to use clojure.spec to validate the input of a decimal? data type. How can I extend the middlewares available to provide this? I know ring-json seems to have an option to handle it

ikitommi06:01:49

@iagwanderson for json? You need to configure Muuntaja->jsonista->Jackson to support that. Will paste few links in the thread

ikitommi06:01:34

... so create a new Muuntaja instance, mount it to the route data, should work.

ikitommi06:01:37

imo way too hard currently, and there should be examples how to do this. Doc PR welcome!

ikitommi06:01:06

(def m
  (m/create
    (assoc-in
      m/default-options
      [:formats "application/json" :decoder-opts :bigdecimals]
      true)))

ikitommi06:01:37

imo way too hard currently, and there should be examples how to do this. Doc PR welcome!

bartuka09:01:20

hello @ikitommi!! Yes, actually is too hard indeed rsrsrs I tried it yesterday for a while unsuccessfully rsrsrs I will check the links you provided

bartuka10:01:40

I got the gist of it, but there is something wrong with this new muuntaja instance. I did this experiment isolated to debug the problem:

(def m
  (m/create
   (assoc-in
    m/default-options
    [:formats "application/json" :decoder-opts :bigdecimals]
    true)))

(require '[jsonista.core :as j])

(def mapper (j/object-mapper {:bigdecimals true}))

(-> "{\"kikka\": 32.2}"
    (j/read-value mapper)
    :kikka
    class)
;; => java.math.BigDecimal

(->> "{\"kikka\": 32.2}"
     (m/decode "application/json")
     :kikka
     class)
;; => java.lang.Double

ikitommi10:01:54

(->> "{\"kikka\": 32.2}"
     (m/decode m "application/json")
     :kikka
     class)

ikitommi10:01:04

^:--- pass in the muuntaja instance

bartuka10:01:54

cool, worked! but I keep getting error on the reitit though

"problems": [
    {
      "path": [
        "y"
      ],
      "pred": "clojure.core/decimal?",
      "val": "10.0",
      "via": [
        "secland.services.web/math-request",
        "secland.services.web/y"
      ],
      "in": [
        "y"
      ]

bartuka10:01:12

I used the instance in :muuntaja key inside :data

ikitommi10:01:45

if you can do a minimal repro repo, I can take a look

bartuka12:01:40

I should pass only m? because it is an instance of muuntaja. I just tried, and did not work either anyway.

ikitommi12:01:15

but the spec-coercion doesn’t have coercion for decimal?

ikitommi12:01:40

should be string->decimal , PR welcome

👍 4
bartuka12:01:11

oh! cool! I will open a PR to fix it! thanks

bartuka21:01:43

thanks!! I also open an issue asking for directions towards a string->ratio implementation: https://github.com/metosin/spec-tools/issues/209

bartuka11:01:22

it's basically the ring-swagger example from the oficial repo

bartuka11:01:30

but I changed the spec types to decimals

bartuka12:01:02

I will try to produce an image like this one to understand the inner workings of reitit:

ikitommi14:01:03

@iagwanderson each route a it's unique middleware or interceptor-stack, optimized for that specific route (and other route data too) Otherwise, the pic looks right