Fork me on GitHub
#clojure-spec
<
2018-11-07
>
danielstockton13:11:34

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])?

danielstockton13:11:20

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

jumar13:11:07

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

danielstockton13:11:06

Namespaced keys works, thanks

shaun-mahood19:11:24

@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