Fork me on GitHub

Not sure how to achieve something. If I want to check a map for :key, but define the spec with ::some-type-of-key (different spec name) in (s/keys :req-un [::key])?


More concretely, I have a :type key on multiple things, and the spec is slightly different for each.


You should probably use namespace-qualified keys in that case (that is :req) You can also use a level of indirection and define specs like this:

(s/def :a/type int?)
(s/def ::a-type (s/keys :req-un [:a/type]))

(s/def :b/type string?)
(s/def ::b-type (s/keys :req-un [:b/type]))

(def int {:type 10})
(def b {:type "number"})

(s/valid? ::a-type a)
;; => true
(s/valid? ::a-type b)
;; => false

(s/valid? ::b-type b)
;; => true
(s/valid? ::b-type a)
;; => false


Namespaced keys works, thanks


@alexmiller: I'm doing my first really rigorous spec of something with a lot of data, and this morning I'm on my 4th or 5th time understanding why a certain design decision makes more sense than I realized with either smaller data or less complete specs. It's pretty great to be using something with so much depth of thought and experience behind it!

👍 8