Fork me on GitHub
#clojure-spec
<
2019-01-24
>
dacopare06:01:56

I'm trying to write a spec for a map that satisfies two keys specs.

(s/def ::a string?)
(s/def ::b string?)

(s/def ::a-map (s/keys :req [::a]))
(s/def ::b-map (s/keys :req [::b]))

;; Satisfies both specs
(s/def ::a-and-b (s/and ::a-map ::b-map))

;; Extend an existing spec
(s/def ::another string?)
(s/def ::a-extended
  (s/and ::a (s/keys :req [::another])))
But generation obviously fails. Is there a way to do this that produces a good generator? Perhaps using the and section in s/keys? (s/keys :req [::x ::y (or ::secret (and ::user ::pwd))] :opt [::z])

seancorfield06:01:56

It’s designed to merge multiple s/keys specs.

dacopare06:01:36

Thank you very much @seancorfield, it seems to be the perfect tool for the job.

victorb12:01:00

How can I spec a map where the keywords can be anything (basically IDs in this case) but I do know what I want the values to be

aisamu13:01:00

Something involving #{}, perhaps?

borkdude13:01:21

@victorbjelkholm429 what about (s/map-of any? #{:value1 :value2})

victorb13:01:26

Thanks @aisamu and @borkdude. map-of any? seems to be what I was looking for.

victorb13:01:04

Ended up with (s/map-of keyword? :my/other-spec) πŸ™‚

πŸ™ƒ 1
fmjrey13:01:44

Better to change your spec before coding than changing it after coding!

victorb14:01:43

@fmjrey funny you say that as I currently sit and trying to retrofit clojure.spec after getting the basic mvp features in place πŸ™‚