malli icon indicating copy to clipboard operation
malli copied to clipboard

`mx/defn` improvement - mock interfaces

Open escherize opened this issue 3 years ago • 0 comments

It'd be nice to be able to easily define functions that don't do anything but check their inputs, and generate their outputs. This is very helpful for a few use cases:

  1. better with-redefs in testing
  2. quickly prototype shapes of data, when working with another team
  3. "battle harden" malli schemas before having to implement the underlying machinery
  4. workflow where users build up functions before implementing them, this is very help

We know it's actually pretty simple, but this should be hooked up with mx/defn.

Here's how one can do it today, manually:

(mx/defn eff :- string? [x :- int?]
  (str x))

(def mock-eff (mg/generate (m/form (:schema (meta #'eff)))))

mock-eff called with good input, returns output that matches the return schema:


(mock-eff 3)
"72eSV3WQpD6d9"

mock-eff throws on bad input

(try (mock-eff "3")
     (catch Exception e (ex-data e)))
;; => {:type :malli.core/invalid-input,
;;     :message :malli.core/invalid-input,
;;     :data {:input [:cat int?], :args ["3"], :schema [:=> [:cat int?] string?]}}

Open questions

Q: Worth including :humanized output?

escherize avatar Jan 23 '23 17:01 escherize