Skip to content

Instantly share code, notes, and snippets.

@mpenet
Last active March 8, 2021 12:29
Show Gist options
  • Save mpenet/1dec18a92e1b1bacc10eed715c465ba3 to your computer and use it in GitHub Desktop.
Save mpenet/1dec18a92e1b1bacc10eed715c465ba3 to your computer and use it in GitHub Desktop.
(defn allowed-keys?
[ks]
#(set/superset? ks (set (clojure.core/keys %))))
(defmacro keys
"Like clojure.spec.alpha/keys but allow to pass a `:closed?` parameter
to make the spec strict on allowed keys."
[& {:keys [closed? req req-un opt opt-un _gen] :as args}]
(let [keys-form `(s/keys ~@(apply concat args))]
(if closed?
`(-> (s/and map?
(allowed-keys? ~(into #{}
(filter keyword?)
(concat [opt opt-un]
;; support or/and
(flatten [req req-un]))))
~keys-form)
(s/with-gen #(s/gen ~keys-form)))
keys-form)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment