Fork me on GitHub
#malli
<
2022-10-26
>
bortexz14:10:18

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

bortexz16:10:49

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

ikitommi17:10:58

hmm. that’s not good.

ikitommi17:10:25

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.

ikitommi17:10:19

calling m/deref-all on the subject should work, but just for one level. This is unfortunate.

ikitommi17:10:44

checked if that’s easy to change in malli, doesn’t seem to: https://github.com/metosin/malli/pull/772/files - all tests fail 😞

ikitommi17:10:35

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?}]

ikitommi17:10:09

e.g. each reference schema contributes to the path with 0.

ikitommi17:10:59

so, this would work too:

(mu/update-in ::thing [0 ::thing-nested] (fn [s] (mu/required-keys s [::thing-nested2])))

ikitommi17:10:07

please write an issue, will think about this.