This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-12-13
Channels
- # adventofcode (84)
- # aleph (1)
- # announcements (2)
- # aws (27)
- # beginners (52)
- # braveandtrue (2)
- # calva (440)
- # cider (7)
- # clara (2)
- # cljdoc (26)
- # cljs-dev (70)
- # clojure (131)
- # clojure-berlin (4)
- # clojure-brasil (1)
- # clojure-europe (2)
- # clojure-greece (4)
- # clojure-hamburg (1)
- # clojure-italy (4)
- # clojure-losangeles (6)
- # clojure-nl (14)
- # clojure-spec (7)
- # clojure-uk (25)
- # clojurescript (26)
- # component (2)
- # cursive (13)
- # datomic (60)
- # dirac (59)
- # docker (1)
- # figwheel (1)
- # figwheel-main (2)
- # fulcro (12)
- # graphql (5)
- # juxt (33)
- # leiningen (19)
- # nrepl (1)
- # off-topic (37)
- # protorepl (2)
- # re-frame (18)
- # reagent (46)
- # remote-jobs (1)
- # ring-swagger (1)
- # shadow-cljs (88)
- # sql (10)
- # tools-deps (64)
- # vim (24)
Let's say I have following config spec:
(s/def ::my-config-spec
(s/keys :req-un [::host
::port
]))
(s/explain-data ::my-config-spec {:host ""})
;; => #:clojure.spec.alpha{:problems (
;; {:path [], :pred (clojure.core/fn [%] (clojure.core/contains? % :port)),
;; :val {:host ""}, :via [:cacsremoteservice.config/my-config-spec], :in []}), :spec :cacsremoteservice.config/my-config-spec, :value {:host ""}}
I used explain-data and returned :path
key to indicate which keys are in the config are invalid/missing.
What I didn't realize is that when the key is missing then the path is empty (instead of pointing to the particular key).
Is there a nice way how to get the name of a missing required key?Not nice but you can pattern match :pred
with destructuring since it has that particular shape and extract the missing field name from the contains?
call.
Thanks for the idea! I'm sure there's a better way but here's my quick solution:
(defn extract-key [spec-problem]
(let [[_fn _args [_contains _map req-key]] (:pred spec-problem)]
req-key))
(let [ed (s/explain-data ::my-config-spec {:host ""})]
(->> ed :clojure.spec.alpha/problems
(map extract-key)))
(And it might change in a future version of spec)