This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-10-07
Channels
- # announcements (2)
- # babashka (34)
- # beginners (114)
- # biff (7)
- # calva (16)
- # cider (2)
- # clj-kondo (46)
- # clj-on-windows (14)
- # clojars (13)
- # clojure (33)
- # clojure-europe (17)
- # clojure-nl (2)
- # clojure-norway (8)
- # clojure-spec (3)
- # clojure-uk (3)
- # clojurescript (25)
- # community-development (1)
- # datalevin (1)
- # emacs (53)
- # fulcro (31)
- # gratitude (2)
- # jobs (1)
- # lambdaisland (12)
- # lsp (57)
- # malli (3)
- # nbb (1)
- # off-topic (92)
- # pathom (2)
- # pedestal (2)
- # releases (5)
- # shadow-cljs (25)
- # sql (3)
- # squint (1)
- # testing (6)
- # vim (11)
I mean, you could write your own spec-outputting function that works like you describe 🙂
This is not what you want, the failure message would not be very helpful. But just a back-of-the-napkin sketch to inspire you 🙂
(defn map-spec [validation-map]
(s/def (fn [input]
(and (clojure.set/subset? (set (keys input))
(set (keys validation-map)))
(->> input
(filter (fn [[k v]]
(contains? (set (keys validation-map))
k)))
(map (fn [[k v]]
(let [validation-fn (get validation-map k)]
(validation-fn v))))
(every? identity))))))
Actually, it might be a bit difficult to get the usually good feedback messages from the spec validating functions unless you venture into macro territory, so it’s probably a bit harder than the initial impression I gave. Still, might be an interesting exercise. But probably, it’s a good idea to follow the conventions laid out by spec, as @U064X3EF3 says. Wrapping spec generation in macros can also deter tool usage (such as “go to spec definition of keyword” in Cursive - I specifically lived through that 🙂 )