clojure-spec

meta-meta 2021-08-20T16:29:58.061900Z

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.

2021-08-20T16:41:56.062Z

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

meta-meta 2021-08-20T16:52:25.062200Z

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.