malli

Eric Dvorsak 2025-04-08T09:53:57.656389Z

I like to instrument functions there is quite a few benefits, but also some pains. when using malli/schema in the metadata of a var, it doesn't reload the instrumentation when the var changes, unlike mx/defn but mx/defn also has its own issues, it's hard to get your hands on the schema of the function. sometimes I want to run a quicktest using generators. here's the snippet I use:

(defn -check
  ([?schema f] (-check ?schema f nil))
  ([?schema f options]
   (let [schema (m/schema ?schema options)]
     (m/explain (m/-update-options schema #(assoc % ::m/function-checker (fn [schema _] (mg/function-checker schema options)))) f))))

(defmacro check
  "Given a function, it will grab its schema from the registry of
  instrumented functions and run generative testing on it"
  [func]
  `(let [func# (meta (var ~func))
         ns# (:ns func#)
         schema# (get-in (m/function-schemas)
                         [(symbol (str ns#))
                          (symbol (:name func#))
                          :schema])]
     (assert schema# ~(str func " is not instrumented"))
     (tests
      ~(str "Generative testing of " func)
      (-check schema# ~func {:malli.generator/=>iterations 1000}) := nil)))
I anyone found a cleaner solution I'm all ears. What's your experience with function instrumentation/generators?