metosin / malli

High-performance data-driven data specification library for Clojure/Script.
Eclipse Public License 2.0
1.46k stars 208 forks source link

`mx/defn` improvement - mock interfaces #824

Open escherize opened 1 year ago

escherize commented 1 year ago

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?