clojure-spec

About: http://clojure.org/about/spec Guide: http://clojure.org/guides/spec API: https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html
wcalderipe 2020-10-24T17:16:04.124Z

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 

alexmiller 2020-10-24T17:38:56.124400Z

In short, no

đź‘Ť 1
borkdude 2020-10-24T18:36:35.125200Z

@wcalderipe you can do this:

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

borkdude 2020-10-24T18:37:06.125500Z

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

alexmiller 2020-10-24T18:41:59.127100Z

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

alexmiller 2020-10-24T18:42:30.128Z

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

wcalderipe 2020-10-24T18:47:24.128600Z

thanks @alexmiller and @borkdude

borkdude 2020-10-24T18:47:28.128800Z

I think select might work better here right in spec2?

wcalderipe 2020-10-24T18:48:10.129500Z

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

alexmiller 2020-10-24T18:49:18.130Z

Yes, but will still have repetition

alexmiller 2020-10-24T18:49:56.130500Z

For stuff like above, yes

đź‘Ť 1