Fork me on GitHub
#malli
<
2022-08-09
>
bortexz23:08:18

Hi, recently started playing with Malli, I am trying to understand why this works:

(def Map1
  (mu/optional-keys
   (m/schema
    [:map ::a ::b]
    {:registry (merge
                (m/default-schemas)
                (mu/schemas)
                {::a string?
                 ::b [:merge
                      [:map [::a [:ref ::a]]]
                      [:map [:y string?]]]})})
   [::b]))
but this fails with enum child error:
(def Map2
  (mu/optional-keys
   [:map {:registry (merge
                     (m/default-schemas)
                     (mu/schemas)
                     {::a string?
                      ::b [:merge
                           [:map [::a [:ref ::a]]]
                           [:map [:y string?]]]})}
    ::a
    ::b]
   [::b]))
=> 
; Execution error (ExceptionInfo) at malli.core/-fail! (core.cljc:138).
; :malli.core/child-error {:type :enum, :properties nil, :children nil, :min 1, :max nil}
Is it a bug? Am I missing something? Edit: Another question that is slightly related (involves registries too) malli.util derivative transforms like mu/optional-keys obfuscate the refs when used inside registry:
(def Map1 
  [:map {:registry {::b string?
                    ::a (mu/optional-keys
                         [:map [::b [:ref ::b]]])}}
   ::a])
=>
; Execution error (ExceptionInfo) at malli.core/-fail! (core.cljc:138).
; :malli.core/invalid-ref {:type :ref, :ref :malli-domain-lab/b}
Is there any workaround for this?

ikitommi06:08:54

Hi. I believe it’s a bug, please write an issue.

ikitommi06:08:10

Robust way to define a schema space for an application is to use the global scope, e.g.:

(mr/set-default-registry!
  ;; linear search
  (mr/composite-registry
    ;; default schemas
    (m/default-schemas)
    ;; utils
    (mu/schemas)))

(mu/optional-keys
 [:map {:registry {::a string?
                   ::b [:merge
                        [:map [::a [:ref ::a]]]
                        [:map [:y string?]]]}}
  ::a
  ::b]
 [::b])

bortexz08:08:14

Done! https://github.com/metosin/malli/issues/736 Thank you, I’ll rearrange how the registries are defined to make it work