Fork me on GitHub
#clojure-spec
<
2017-06-12
>
lambder13:06:08

hi All say I have a ns in which I want to define 2 specs , each for map for example:

(s/def ::a (s/keys :req-un [::value]))
(s/def ::b (s/keys :req-un [::value]))
the problem is that both maps require to have key :value but there should be different specs of ::value for each ::a and ::b how do I do it?

misha13:06:16

@lambder

(s/def :foo/value integer?)
(s/def :bar/value string?)
(s/def ::a (s/keys :req-un [:foo/value]))
(s/def ::b (s/keys :req-un [:bar/value]))

misha14:06:35

@lambder a and b in :a/value are not related to ::a in any implicit way, in case you might wonder

misha14:06:10

there, updated it to avoid any confusion

mpenet14:06:01

@alexmiller should we expect a big reveal for your talk in euroclojure, given the recent mentions of "dependency" related work happening? 🙂

Alex Miller (Clojure team)14:06:58

no one expects the spanish inquisition

Alex Miller (Clojure team)15:06:14

there are a few projects I have worked on over the last year in this area and the abstract is sufficiently vague to cover whatever I can talk about by the time I do the talk

misha15:06:33

@alexmiller multispec assocs dispatch value back to the {} during s/exercise, right? is there a way to re-use same enrich {} with a dispatch value, but during s/conform?

misha15:06:21

oh, I can just call multimethod on an actual {} to get the dispatch value ... assuming defmethod's return keyword, not an inline spec-from

souenzzo19:06:40

I have a query (all-enums-of :my/attr) that return's all my enums. There some how to dynamic gen (s/def :my/attr #{})?

Alex Miller (Clojure team)19:06:37

not sure I understand what the last sentence means - looks like you are trying to spec the empty set, which doesn’t make much sense?

souenzzo19:06:47

I want to dynamic gen the spec of :my/attr. In case, it's a enum so I describe it as a set of keys.... Maybe (s/def :my/attr (all-enums-of db :my/attr)) Should express better what I mean...

souenzzo19:06:15

self reply - Yep. I can do a macro, It's a good way? It will be declared after connect with the db. It's a problem?``

souenzzo19:06:10

Is it a reasonable solution? "Should I use in production"?

Alex Miller (Clojure team)20:06:54

(defn load-enum-specs [db]
  (let [enums (all-enums-of db :my/attr)]
    (eval `(s/def :my/attr ~enums))))

ikitommi20:06:26

Would be nice to have:

(defn load-enum-specs [db]
  (let [enums (all-enums-of db :my/attr)]
    (s/register :my/attr enums)))

Alex Miller (Clojure team)20:06:04

well you’re just a macro away from that :)

mpenet20:06:40

Would be doable with unform with spec of specs too

Alex Miller (Clojure team)20:06:58

(defmacro register [name spec] 
  (let [s (eval spec)] 
    `(s/def ~name ~s)))

Alex Miller (Clojure team)20:06:57

this of course has the significant downside that it only works in a few cases like literal sets

Alex Miller (Clojure team)20:06:36

or maybe wider than just those but not for things like preds, anonymous functions, etc