Fork me on GitHub
Alex Sky16:03:28

Hello! Is it possible to use malli with compojure + swagger? Couldn’t find any examples. I can only see an option to implement the protocol Coercion


Sadly no. Reitit has built-in support for malli

🙏 1
Alex Sky05:03:53

Is it related to some kind of restrictions? I’m a little surprised since compojure-api as well retitit and malli are created by metosin :)


I have a hack to turn a malli spec into a Schema, if that helps

Alex Sky06:03:30

> I have a hack to turn a malli spec into a Schema, if that helps Yes, could you show me an example?


I haven’t tested that on a compojure-api route yet, but here’s the gist of it:

(require '[schema.core                   :as schema]
         '[schema.spec.core              :as schema.spec]
         '[schema.spec.leaf              :as leaf])

(defn malli->schema [malli-schema]
  (reify schema/Schema
    (spec [this]
       (schema.spec/precondition this
                                 (fn [data]
                                   (m/validate malli-schema data))
                                 (fn [data]
                                   (me/humanize (m/explain malli-schema data))))))
    (explain [this]
     (str "Malli->Schema:" malli-schema))))


but this allows you to use schema like this:

 (malli->schema [:map [:foo int?] [:bar keyword?]])
 {:foo 1 :bar 2})
=> (not {:bar ["should be a keyword"]})

 (malli->schema [:map [:foo int?] [:bar keyword?]])
 {:foo 1 :bar :humbug})


not perfect, and I’m about to use this in :body and :path-params and see if that works

Godwin Ko22:03:53

is it possible to get the current index of a vector spec, so that I can have different validation logic for the exact nth element?


no, but use can use sequence schemas for that

Godwin Ko01:03:00

ok, but it’s a bit clumsy if the validation of a specific item share most but just varies a little bit from the other…… thx a lot for your prompt respond anyway :man-bowing::skin-tone-2:


you can also compose with [:and [:vector :int] [:fn (fn [v] (= (nth v 5) 42))]]

Godwin Ko05:03:52

got it, i.e. raise one level to apply function spec on the entire vector :thinking_face:

Godwin Ko05:03:43

in our use case, we have vector of maps, using function spec to replace the entire map schema still not that straight forward or desirable…… 😅