Fork me on GitHub
#reitit
<
2021-05-01
>
Alexis Vincent13:05:15

Hi there, I’m trying to add a malli transformer for rest responses to reitit but not having much luck. Have tried a few things but not yet getting anywhere. This is what I’ve arrived at so far:

(def sanatize
  {:compile
   (fn [schema x]
    (println schema x)
    identity)})

(def response-transformer-provider
  (reify reitit.coercion.malli/TransformationProvider
    (-transformer [_ {:keys [strip-extra-keys default-values]}]
      (mt/transformer
       (mt/transformer
        {:default-decoder sanatize
         :default-encoder sanatize})
        ;; {:decoders {:map add-defaults}
        ;;  :encoders {:map add-defaults}})
       (mt/default-value-transformer)))))

(defn router []
  (reitit.ring/router
   [
    ["/v1" {:middleware [auth/auth-middleware]}
     ["/x"
      ["/y" api.y/y-endpoint]]]
   {:exception pretty/exception
    :data {:muuntaja m/instance
           :coercion (reitit.coercion.malli/create
                      {:transformers {:body {:default reitit.coercion.malli/default-transformer-provider
                                             :formats {"application/json" reitit.coercion.malli/json-transformer-provider}}
                                      :string {:default reitit.coercion.malli/string-transformer-provider}
                                      :response {:default response-transformer-provider}}

                       ;; set of keys to include in error messages
                       :error-keys #{:type :coercion :in :schema :value :errors :humanized #_:transformed}
                       ;; schema identity function (default: close all map schemas)
                       :compile mu/closed-schema
                       ;; validate request & response
                       :validate true
                       ;; top-level short-circuit to disable request & response coercion
                       :enabled true
                       ;; strip-extra-keys (effects only predefined transformers)
                       :strip-extra-keys true
                       ;; add/set default values
                       :default-values true
                       ;; malli options
                       :options nil})
           :middleware [
                        ;; query-params & form-params
                        parameters/parameters-middleware
                        ;; content-negotiation
                        muuntaja/format-negotiate-middleware
                        ;; encoding response body
                        muuntaja/format-response-middleware
                        ;; exception handling
                        exception-middleware
                        ;; decoding request body
                        muuntaja/format-request-middleware
                        ;; coercing response bodys
                        coercion/coerce-response-middleware
                        ;; coercing request parameters
                        coercion/coerce-request-middleware
                        ;; multipart
                        multipart/multipart-middleware]}}))

Alexis Vincent13:05:05

I’m expecting a printout of the schema