Fork me on GitHub

Currently using ring + reitit and malli coercion. I want to convert all incoming requests keys to snake-case (requests incoming from a JS front-end). I’d like to keep naming idiomatic in both places. I’ve tried defining an instance of Muuntaja to do the encoding / decoding:

;; Create an explicit instance of Muuntaja with case rewriting
(def m
   (-> muun/default-options
        [:formats "application/json" :decoder-opts]
        {:decode-key-fn csk/->kebab-case-keyword})
        [:formats "application/json" :encoder-opts]
        {:encode-key-fn csk/->camelCase}))))
And my middleware now looks like this:
defn service-routes [db]
   {:coercion malli-coercer
    :muuntaja formats/instance
    :swagger {:id ::api}
    :middleware [;; Cors
                 #(wrap-cors %
                             :access-control-allow-origin [#".*"]
                             :access-control-allow-methods [:get :post]) ;; TODO: This is BAD! :(
                 ;; query-params & form-params
                 ;; content-negotiation
                 muuntaja/format-negotiate-middleware {:muuntaja m} ;; Note the use of the explicit instance of Muuntaja here
                 ;; encoding response body
                 muuntaja/format-response-middleware {:muuntaja m} ;; Note the use of the explicit instance of Muuntaja here
                 ;; exception handling
                 ;; decoding request body
                 muuntaja/format-request-middleware {:muuntaja m} ;; Note the use of the explicit instance of Muuntaja here
                 ;; coercing response bodys
                 ;; coercing request parameters
                 ;; multipart

    :compile coercion/compile-request-coercers
    :exception pretty/exception}
The m instance works on it’s own, but all requests still fail validation / coercion because I think that might be done earlier in the chain? (See :coercion malli-coercer) Does anyone know how I can get muuntaja to do it’s transformations before the coercion takes place? Also if there’s a simpler way to achieve the case rewriting I am interested in hearing that too.


I passed the muuntaja instance to :muuntaja root option, like this:


Thanks! I didn’t know you could do that. Now it’s breaking my swagger haha. Ah well


Hello, guys! Some help needed with reitit.frontend.easy I am trying to do a simple redirect on button click using (*rfe/push-state* ::item {:id 210}) but it seems like unless I have my routes are defines in the same namespace, it doesn't work. May somebody suggest whether that is expected behaviour?


When you move the routes to another namespace, I assume you also change the route name (`::item`) to match the route namespace? For example if your routes are in namespace, you can require them as [ :as routes] and then use that alias when referring to individual routes (`::routes/item`)

👍 6

I saw some useful converters in schema-tools.coerce like string->date but I don't know how to use it, with reitit-ring coercion middlewares, do I have to declare my schema as java.util.Date ? Does it require any wiring?