This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-11-16
Channels
- # beginners (103)
- # boot (48)
- # cider (2)
- # clara (12)
- # cljsrn (9)
- # clojure (133)
- # clojure-art (3)
- # clojure-dev (9)
- # clojure-korea (7)
- # clojure-russia (228)
- # clojure-spec (8)
- # clojure-uk (26)
- # clojurescript (131)
- # cursive (8)
- # datomic (30)
- # emacs (4)
- # events (2)
- # hoplon (47)
- # lein-figwheel (5)
- # off-topic (1)
- # om (12)
- # onyx (337)
- # perun (23)
- # planck (15)
- # proton (3)
- # re-frame (5)
- # spacemacs (20)
- # untangled (97)
- # utah-clojurians (1)
- # yada (13)
I wonder if there is something built into spec already, that does the following: conform
a value and throw an exception with an explanation of what violates the spec in case conform
returns invalid
. similar to s/assert
but always on, as it should be used for internal conforming and validation, e.g. after reading from a database. I came up with this:
(defn conform-validate [spec x]
(let [conformed (s/conform spec x)]
(if (s/invalid? conformed)
(throw (ex-info (str "spec violated: " (s/explain-str spec x))
(s/explain-data spec x)))
conformed)))
any simpler approaches that I am missing or helper libraries that do this already?That looks good to me
Nothing in spec for it
^ I ran in to precisely that need almost immediately upon trying spec. I think it would be a good addition to the lib if it had a good name and insightful docstring. Maybe s/enforce and a docstring that contrasts with assert
although, to play devil’s advocate with myself, i found that when i did want this, i wanted to add extra data to the error via ex-info
Rich considered it and ended up with assert so I don't think he plans to add it afaik
yeah, it’s a reasonable assumption that people would use the wrong one and/or it wouldn’t be particularly useful w/o wrapping it w/ extra data
I expect people will make exactly what they want out of the available parts