Fork me on GitHub
#clojure-spec
<
2022-10-07
>
reefersleep10:10:56

I mean, you could write your own spec-outputting function that works like you describe 🙂

reefersleep10:10:34

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))))))

reefersleep08:10:03

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 🙂 )