Fork me on GitHub
#clojure-spec
<
2022-07-05
>
Lycheese14:07:30

I have a map which needs to have at least one of three possible keys. Sometimes one of those keys is present but with an empty map. I want to not count that as fulfilling the requirement in the or in :test/map but also not invalidate the entire map if one of the other two keys is present and valid. Any way of doing that without first dissoc'ing those keys with empty values?

(s/def :test/b ;; c and d look similar
  (s/and not-empty
         (s/map-of number? (s/multi-spec mm :test/type))))

(s/def :test/map
  (s/keys :req [:test/a
                (or :test/b
                    :test/c
                    :test/d)]
          :opt [:test/e
                :test/f]))

Alex Miller (Clojure team)15:07:16

with spec, your only option would be a conformer that manipulates the map ahead of the spec. or you can s/and an arbitrary predicate that does anything you want.

👍 1