Fork me on GitHub
#clojure-spec
<
2021-08-20
>
meta-meta16:08:58

Hi, how do I write a spec to validate values in a map depending on the key? Say I have a map {:foo 42 :bar [1 2 3]} I want to validate that if :foo is defined, it's an int and if :bar is defined, it's a coll.

Russell Mull16:08:56

(s/def ::foo int?)
(s/def ::bar (s/coll-of int?))
(s/def ::mymap (s/keys :req-un [::foo ::bar]))

meta-meta16:08:25

thank you! And now I see it's right there in the first part of the spec guide. I was looking at map-of and every, totally forgetting what I read the other day.