Fork me on GitHub
#clojure-spec
<
2019-09-22
>
Sebastiano Barrera09:09:28

Hi y'all... I'm a beginner tinkering around with spec (on CLJS) and I was wondering: is there a way to encode a spec so that a certain keyset is required only if another key has a certain value? For example, I might have {:type :assignment, :left ..., :right ...} or {:type :identifier, :name 'some-name}; could I express the requirement that :left and :right must both appear iff :type is :assignment?

Sebastiano Barrera10:09:11

Wait, I think I figured it out... I'm guessing

(defn node-is? [type] #(= (:type %) type))
(s/or
  (s/keys :req-un [::type (node-is? :assignment) ::left ::right])
  (s/keys :req-un [::type (node-is? :identifier) ::name]))

vlaaad10:09:40

@sebastiano.barrera_cl I think what you need is multi-spec

Sebastiano Barrera11:09:26

Oh, let me take a look at that