malli
malli copied to clipboard
`mx/defn` improvement - mock interfaces
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:
- better with-redefs in testing
- quickly prototype shapes of data, when working with another team
- "battle harden" malli schemas before having to implement the underlying machinery
- 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?