Fork me on GitHub
#reitit
<
2022-01-31
>
DenisMc10:01:20

Hi, I’ve converted to using Malli now for my REST JSON schema definition and so far it’s looking great. Quick question. I have a few enums in my schema, and I want to accept case insensitive input for some of these. So I have something like (:foo [:enum {:decode/string 'str/upper-case} "BAR" "ZZZ"] However, when I try this (with let’s say :foo "bar" , I get a validation error. Is malli validating the value before decoding, and if so how do I allow either lower or upper case for input values?

Ben Sless10:01:44

Did you make sure the string decoder ran?

Ben Sless10:01:46

You need to provide coercion

DenisMc11:01:07

Hmm. I think I’m properly providing coercion, but maybe I’ve misconfigured it or something. Under (ring/router _ {options}, I have

:coercion   (reitit.coercion.malli/create
                          {;; 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
                           ;; strip-extra-keys (effects only predefined transformers)
                           :strip-extra-keys true
                           ;; add/set default values
                           :default-values   true
                           ;; malli options
                           :options          nil})
..but I don’t think the coercion is being applied.

DenisMc11:01:44

e.g. when I print the data in my ring handler, the enum fields I defined in malli are coming up as #object[clojure.lang.AFunction$1 0xe1da5b0 "clojure.lang.AFunction$1@e1da5b0"] and so on - looks like the coercion isn’t being applied. I have just tried to explicitly set the coercion in the route in question as well (i.e. like this:

:post    {:name ::create-foo
          :coercion reitit.coercion.malli/coercion
          :handler    h/create-foo!
          :parameters {:body requests/foo}
          :summary    "Create a new foo."}}]
But same result.

DenisMc12:01:55

I think it may be that my enums are not being transformed. I can see how to add a transformation to a direct malli/decode call (e.g. (m/encode int? 42 mt/string-transformer) , as per documentation at https://github.com/metosin/malli#value-transformation, but I’m not sure how to define such a transformation as part of my ring spec. I think I’m getting there but I’m not there yet!

DenisMc15:02:46

I sorted this out. As with so many problems that baffle us, the root cause of the problem was somewhere other than where I was focussing.

👍 1
Drew Verlee22:01:54

Does metosin/reitit-sieppari {:mvn/version "0.5.15"} only support clj? It seems so, though i don't clearly see why it can't be cljc ... https://github.com/metosin/reitit/blob/master/modules/reitit-sieppari/src/reitit/interceptor/sieppari.clj likely there is a reason.