This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-06-23
Channels
- # announcements (1)
- # asami (2)
- # aws (15)
- # babashka (4)
- # beginners (42)
- # calva (8)
- # clj-kondo (7)
- # cljsrn (31)
- # clojure (64)
- # clojure-australia (4)
- # clojure-europe (40)
- # clojure-italy (2)
- # clojure-nl (5)
- # clojure-uk (10)
- # clojured (1)
- # clojurescript (16)
- # conjure (4)
- # datomic (5)
- # defnpodcast (2)
- # events (1)
- # fulcro (61)
- # graphql (11)
- # honeysql (9)
- # jobs (3)
- # jobs-discuss (3)
- # lsp (65)
- # malli (3)
- # meander (4)
- # off-topic (5)
- # pathom (32)
- # podcasts-discuss (2)
- # polylith (2)
- # re-frame (30)
- # reitit (6)
- # remote-jobs (3)
- # ring (4)
- # shadow-cljs (19)
- # sql (28)
- # vim (1)
- # xtdb (21)
I’d like to attach error messages to specific parts of my schema. So far I have the following working, but I’m not sure if it’s the best implementation.
(def MySchema
[:map
[:something/one {:error/message "one"} int?]
[:something/two {:error/message "2"} int?]
[:something/three {:error/message "three"} int?]
[:something/four {:error/message "four"} int?]])
(validate MySchema {})
;; => #:something{:one ["one"], :two ["2"], :three ["three"], :four ["four"]}
I am using the following to get this working:
(defn find-error-message [schema k]
(let [value (atom nil)]
(walk/postwalk
(fn [x]
(when (and x (vector? x) (= (first x) k) (map? (second x)))
(reset! value (second x)))
x)
schema)
(:error/message @value)))
;; then, in humanize:
(me/humanize ...
{:errors
(assoc me/default-errors
:malli.core/missing-key
{:error/fn (fn [{:keys [in]} _]
(or
(find-error-message schema (last in)) ;; is (last in) reliable here?
(str "Missing key: '"(last in)"'")))})})