This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-05-30
Channels
- # babashka (7)
- # beginners (25)
- # calva (22)
- # cljs-dev (1)
- # clojure (62)
- # clojure-europe (118)
- # clojure-hamburg (4)
- # clojure-israel (2)
- # clojure-nl (2)
- # clojure-uk (6)
- # clojured (1)
- # clojurescript (23)
- # conjure (11)
- # cursive (3)
- # datomic (14)
- # duct (2)
- # emacs (12)
- # figwheel-main (1)
- # gratitude (1)
- # hyperfiddle (4)
- # joyride (72)
- # lsp (46)
- # luminus (1)
- # malli (1)
- # off-topic (54)
- # pathom (19)
- # polylith (11)
- # releases (2)
- # sci (22)
- # shadow-cljs (4)
- # vim (11)
- # xtdb (52)
I've been reminding myself how to use spec and one thing I can't remember is how to get better errors from this sort of thing?
(defn load-order [order-book {:keys [side id] :as order}]
{:pre [(s/valid? :order/order order)]}
(assoc-in order-book [side id] order))
Which does give me this:
Execution error (AssertionError) at fruit-economy.sim.market/load-order (market.clj:23).
Assert failed: (is (s/valid? :order/order order))
But considering it's a spec, I was expecting a little more?
I've hacked it a bit by wrapping it in an is
, which gives me:
FAIL in () (market.clj:2)
expected: (s/valid? :order/order order)
actual: (not
(s/valid?
:order/order
{:price 1,
:size 425.0,
:side :sell,
:id 10229,
:good-kw :inventory}))
But I'm sure there's a better way?Unfortunately, :pre
doesn’t really understand specs so can only assert that a non-truthy value occurred.
Three options spring to mind…
You could use s/assert
outside of your precondition, and enable checking.
If you use fdef
to specify your :args
and turn on instrumentation, you’ll get an error with an explanation.
https://clojuredocs.org/clojure.spec.alpha/fdef
Alternatively, test your specified function and don't call it with the wrong input. Validate data at the boundary (this one might be a little idealistic).