This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-10-26
Channels
- # announcements (17)
- # babashka (68)
- # beginners (8)
- # biff (14)
- # calva (25)
- # cherry (10)
- # clj-kondo (1)
- # clj-on-windows (12)
- # cljsrn (6)
- # clojure (134)
- # clojure-berlin (1)
- # clojure-europe (33)
- # clojure-nl (4)
- # clojure-norway (6)
- # clojure-uk (10)
- # clojurescript (9)
- # datalevin (8)
- # datomic (34)
- # docker (1)
- # emacs (31)
- # fulcro (6)
- # honeysql (8)
- # java (7)
- # joyride (14)
- # kaocha (7)
- # malli (11)
- # nbb (4)
- # off-topic (11)
- # pedestal (14)
- # rdf (53)
- # re-frame (6)
- # reagent (39)
- # reitit (2)
- # releases (9)
- # rewrite-clj (14)
- # shadow-cljs (97)
- # specter (1)
- # testing (5)
- # tools-deps (12)
- # vim (4)
- # xtdb (9)
Is this a bug, or am I doing something wrong?
(defonce registry* (atom (m/default-schemas)))
(mr/set-default-registry! (mr/mutable-registry registry*))
(defn sdef
"Defines a new schema in mutable [[registry*]]."
([type schema] (swap! registry* assoc type schema) schema)
([type props schema] (swap! registry* assoc type [:schema props schema]) schema))
(sdef ::thing-nested2 int?)
(sdef ::thing-nested (mu/optional-keys [:map ::thing-nested2]))
(sdef ::thing (mu/optional-keys [:map ::thing-nested]))
(mu/update ::thing ::thing-nested (fn [s] (mu/required-keys s [::thing-nested2])))
Blows up with:
; Evaluating file: malli.clj
; Execution error (ExceptionInfo) at malli.core/-fail! (core.cljc:138).
; :malli.core/invalid-schema {:schema nil}
; Evaluation of file malli.clj failed: class clojure.lang.Compiler$CompilerException
If reading the schema from the registry directly, it seems to work:
(mu/update (get @registry* ::thing) ::thing-nested (fn [s] (mu/required-keys s [::thing-nested2])))
The malli.util
helpers do not deref the schemas => calling mu/get
on a reference tries to get from the reference, not from the value behind it.
calling m/deref-all
on the subject should work, but just for one level. This is unfortunate.
checked if that’s easy to change in malli, doesn’t seem to: https://github.com/metosin/malli/pull/772/files - all tests fail 😞
here’s the list of all paths that are part of the schema:
(mu/subschemas ::thing)
;[{:path [],
; :in [],
; :schema :malli.core-test/thing}
; {:path [0]
; :in []
; :schema [:map [:malli.core-test/thing-nested {:optional true} :malli.core-test/thing-nested]]}
; {:path [0 :malli.core-test/thing-nested]
; :in [:malli.core-test/thing-nested]
; :schema :malli.core-test/thing-nested}
; {:path [0 :malli.core-test/thing-nested 0],
; :in [:malli.core-test/thing-nested],
; :schema [:map [:malli.core-test/thing-nested2 {:optional true} :malli.core-test/thing-nested2]]}
; {:path [0 :malli.core-test/thing-nested 0 :malli.core-test/thing-nested2],
; :in [:malli.core-test/thing-nested :malli.core-test/thing-nested2],
; :schema :malli.core-test/thing-nested2}
; {:path [0 :malli.core-test/thing-nested 0 :malli.core-test/thing-nested2 0],
; :in [:malli.core-test/thing-nested :malli.core-test/thing-nested2],
; :schema int?}]
so, this would work too:
(mu/update-in ::thing [0 ::thing-nested] (fn [s] (mu/required-keys s [::thing-nested2])))
Done (https://github.com/metosin/malli/issues/773) thanks for looking into this @U055NJ5CC 🙏