We don't encourage you to do this, but I don't have an easier solution than this.
On Thursday, February 2, 2017 at 7:05:37 AM UTC-6, Dave Tenny wrote: > > I want to specify in clojure spec that only declared keywords are > permitted in function calls. > This is to catch what are usually mis-spelled or mis-cased keywors passed > via option maps in function calls. > > In the fdefs below, the second fdef will catch an invalid call, e.g. > (f 1 {:a 2 :B 3}) > but the first fdef will not. > > Is there an easier way to specify the more restrictive spec without having > to add my own 'every' and enumeration of the keys? > (or build my own version of s/keys that does this automatically, which I'm > guessing is the answer). > > (require '[clojure.spec :as s]) > (require '[clojure.spec.test :as stest]) > > > (s/def ::x (fn [x] true)) ;for lack of any? > > > ;; Will not catch invalid keywords > (s/fdef f > :args (s/cat :x ::x > :options (s/keys :opt-un [::a ::b])) > :ret nil?) > > > ;; Will catch invalid keywords, but is there an easier way? > (s/fdef f > :args (s/cat :x ::x > :options (s/and (s/keys :opt-un [::a ::b]) > #(every? #{:a :b} (keys %)))) > :ret nil?) > > > (defn f [x {:keys [a b]}]) > > > (stest/instrument `f) > > > > > > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.