Last active
March 8, 2021 12:29
-
-
Save mpenet/1dec18a92e1b1bacc10eed715c465ba3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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