This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-02-25
Channels
- # beginners (20)
- # boot (25)
- # cider (1)
- # cljs-dev (7)
- # cljsjs (1)
- # cljsrn (1)
- # clojure (79)
- # clojure-austin (2)
- # clojure-berlin (13)
- # clojure-dusseldorf (1)
- # clojure-germany (7)
- # clojure-russia (10)
- # clojure-serbia (1)
- # clojure-spec (18)
- # clojure-uk (4)
- # clojured (1)
- # clojurescript (90)
- # cursive (10)
- # datomic (7)
- # emacs (14)
- # hoplon (6)
- # luminus (16)
- # lumo (4)
- # numerical-computing (2)
- # om (25)
- # om-next (1)
- # onyx (11)
- # pedestal (10)
- # protorepl (1)
- # reagent (11)
- # remote-jobs (1)
- # ring (1)
- # rum (38)
- # spacemacs (5)
- # test-check (7)
- # untangled (122)
- # vim (1)
- # yada (8)
is there a way in spec to define relationships between attributes, like #(> ::foo :bar)
for example?
(s/def ::foo int?)
(s/def ::bar int?)
(s/def ::keyspec (s/and (s/keys :req [::foo ::bar])
#(> (::foo %) (::bar %))))
@joshjones so... is %
in that context going to be the thing being validated?
yes -- for example (s/valid? ::keyspec {::foo 2 ::bar 3})
the map is the argument to the function
@joshjones oh, cool
so then my next q is
if i have :req
, how do i get a list of the keys that are missing?
this is not meant to be a 'best practices', just to show how you can get the data:
(->> (s/explain-data ::keyspec {::foo 2})
:clojure.spec/problems
(map :pred)
(filter #(= 'contains? (first %)))
(map last))
looks a bit weird but i guess it does the job
cheers 🙂
Call explain-data to get a list of all problems
@thedavidmeister hmm, "weird" is about all I got late Friday after evening activities 🙂 but here you go, same as above but rewritten in a function. cheers to you too
(defn keys-missing
[spec data]
(let [explanation (s/explain-data spec data)
problems (:clojure.spec/problems explanation)
xf (comp (map :pred)
(filter #(= 'contains? (first %)))
(map last))]
(into [] xf problems)))
it relies on things you possibly shouldn't rely on, like contains?
implying that a key is missing, works for now but it's not "public" ... but it's enough to get you going I think 😉
@joshjones yup, looks good enough for me to start swapping out my hand-rolled validation code
i forget - was there a collection of attempted/in-progress specs for all the core functions somewhere?
from core, no (beyond the one ticket out there that slightly extends what’s committed)
there is another project someone started, but I’ve intentionally not looked at it