malli

sammy 2026-04-20T20:28:59.761749Z

is there a cannonical way to temporarily override certain aspects of Malli validation when writing unit tests? for example, when writing one specific unit test, I'd like to temporarily turn off (or modify) Malli validation for one specific funciton (e.g. a constructor), so that I can pass in a dummy value that would be considered underspecified in my real-world production setting, but is nonetheless useful for testing. i've come across this a few times, for example when running unification in https://github.com/hyperfiddle/rcf

2026-04-21T13:31:09.901589Z

There's also the :filters argument which looks a little nicer https://github.com/metosin/malli/blob/master/docs/function-schemas.md#defn-instrumentation

2026-04-21T13:45:53.929629Z

This macro seems useful

(defmacro with-unstrument [options & body]
  `(let [options# ~options]
     (try (mi/unstrument! options#)
          (do ~@body)
          (finally
            (mi/instrument! options#)))))

🙂 1
2026-04-21T13:46:39.639199Z

(with-unstrument
 {:filters [;; everything from user ns
            (mi/-filter-ns 'user)
            ;; ... and some vars
            (mi/-filter-var #{#'power})
            ;; all other vars with :always-validate meta
            (mi/-filter-var #(-> % meta :always-validate))]
  ;; scope
  :scope #{:input :output}
  ;; just print
  :report println}
 (run-test))

2026-04-20T22:28:07.790799Z

I'm not aware of anything too slick, but the :data argument here lets you control which vars are instrumented https://github.com/metosin/malli/blob/52ea58a36ff5172b38dfc526ca638afa7226a4a0/src/malli/instrument.clj#L20 for example

(try (mi/unstrument {:data (select-keys (m/function-schemas)
                                        ['foo/bar])})
     (run-test)
     (finally
       (mi/instrument {:data (select-keys (m/function-schemas)
                                          ['foo/bar])})))

sammy 2026-04-21T04:11:04.606149Z

thanks ambrose! this worked for me, though I want to add two notes for people who stumble on this later • mi/unstrument --> mi/unstrument! • function schemas is a nested map of the form [:mapof :namespace [:mapof :symbol :malli-data]], so you need to select the keys sequentially, i.e.

{'foo (select-keys (get (m/function-schmeas) 'foo) ['bar]}
instead of
(select-keys (m/function-schemas) ['foo/bar])

👍 1