Fork me on GitHub
#clojure-spec
<
2019-01-23
>
victorb12:01:20

Hm, I'm a bit confused about the s/or. Trying to get spec to accept that a key can be either a-map or b-map, both of them are fine, so far got this:

(s/def :test/a-map (s/keys :req-un [::name]))
(s/def :test/b-map (s/keys :req-un [::title]))
(s/def :test/key (s/or :test/a-map :test/b-map))
(s/def :test/map (s/keys :req-un [:test/key]))

(comment
  (s/valid? :test/map {}) ;; false
  (s/valid? :test/map {:key {:name "hello"}}) ;; false / should be true
  (s/valid? :test/map {:key {:title "hello"}}) ;; true
  )

jeroenvandijk12:01:19

you need to label it with a keyword, e.g.

(s/def :test/key (s/or :a :test/a-map :b :test/b-map))

victorb12:01:19

@jeroenvandijk ooh, so simple. Thanks a lot for the fast help! :thumbsup:

👍 5
victorb12:01:04

ugh, should have been obvious if I just read the docs slower, missed the key+pred pairs. I'll blame it on too little coffee

jeroenvandijk12:01:25

I had the same once, no worries

jeroenvandijk12:01:14

It doesn't blow up either so hard to know you did something wrong (without coffee 😉 )

5