Fork me on GitHub
#clojure-spec
<
2020-10-24
>
wcalderipe17:10:04

hey, is there a way to reuse the same spec for different map keys without having to redefine them over and over?

(s/def ::address (some-pred-to-check-ethereum-addr))

(s/def ::tx (s/keys :req-un [:sender ::address  ;; apply address spec to :sender 
                             :recipient ::address])) ;; apply address spec to :recipient 

borkdude18:10:35

@wcalderipe you can do this:

(def spec (s/....)
(s/def ::a spec)
(s/def ::b spec)

borkdude18:10:06

Note sure if that also works in spec2 - I completely lost track of that

Alex Miller (Clojure team)18:10:59

No, it won’t like that but you could set it quoted and make it work via s/register

Alex Miller (Clojure team)18:10:30

But I don’t think that works the way it’s requested with req-un in either case

borkdude18:10:28

I think select might work better here right in spec2?

Alex Miller (Clojure team)18:10:18

Yes, but will still have repetition

wcalderipe18:10:10

s/keys is the best way to check the shape of a map?

Alex Miller (Clojure team)18:10:56

For stuff like above, yes

👍 3