Fork me on GitHub
#malli
<
2021-12-01
>
wcalderipe15:12:16

Can I get the schema of a :multi by passing the dispatched value to a function? Example:

(def Multi
    [:multi {:dispatch :type}
     [:foo [:map [:value :int]]]
     [:bar [:map [:value :string]]]])

  (get-schema Multi {:type :foo}) ;; => [:foo [:map [:value :int]]]

ikitommi17:12:07

@wcalderipe, maybe:

(def Multi
  [:multi {:dispatch :type}
   [:foo [:map [:value :int]]]
   [:bar [:map [:value :string]]]])

(defn get-schema [schema value]
  (some-> schema
          m/properties
          :dispatch
          (apply (list value))
          (->> (mu/get schema))))

(get-schema Multi {:type :foo})
; => [:map [:value :int]]

ikitommi17:12:53

if you want the whole entry, use mu/find:

(defn get-schema [schema value]
  (some-> schema
          m/properties
          :dispatch
          (apply (list value))
          (->> (mu/find schema))))

(get-schema Multi {:type :foo})
; => [:foo nil [:map [:value :int]]]

ikitommi17:12:09

it’s always tuple3 of [key properties value]

wcalderipe07:12:39

@U055NJ5CC thanks a bunch for the help 🙌

👍 1