Fork me on GitHub

Hi, Is it possible to write a schema for this vector:

[{:type :work
  :value ""}
 {:type :personal
  :value ""}]
the vector should only contain those 2 items, where the type is kind of static, the value can change. i thought about using this schema:
[:vector [:map [:type keyword?] [:value string?]]]
but it’s not enough for my use case, because i want when i inspect the schema to know how many items i can have in the vector, and their types (the :type ) is this possible? Thank you for the great lib 🙏


@U6AE62UCT I can think of two ways:

(def data-schema [:map
                   [:type [:enum :work :personal]]
                   [:value string?]])
for the maps and then
(def schema [:vector {:min 2 :max 2} data-schema])
or maybe
(def schema [:tuple data-schema data-schema])
for putting together the schema for the vector


I was thinking about something like

     [:map [:type :work] [:value string?]]
     [:map [:type :personal] [:value string?]]]
but it didn’t work Thank you @U6JS7B99S i’ll probably go with your first suggestion


if the :type defines the shape of the data, you can try :multi:

(require '[malli.generator :as mg])

  [:vector {:min 2, :max 2}
   [:multi {:dispatch :type}
    [:work [:map [:type [:= :work]] [:value string?]]]
    [:personal [:map [:type [:= :personal]] [:value string?]]]]])

  [:tuple {:registry
            [:multi {:dispatch :type}
             [:work [:map [:type [:= :work]] [:value string?]]]
             [:personal [:map [:type [:= :personal]] [:value string?]]]]}}
   ::element ::element])


both emit results like:

([{:type :personal, :value ""} {:type :work, :value ""}]
 [{:type :work, :value "l"} {:type :personal, :value ""}]
 [{:type :personal, :value "z1"} {:type :personal, :value ""}]
 [{:type :personal, :value "ZG"} {:type :personal, :value "N"}]
 [{:type :personal, :value "qQ6"} {:type :work, :value "A8h"}]
 [{:type :personal, :value "2588"} {:type :personal, :value "Djv12"}]
 [{:type :work, :value ""} {:type :personal, :value "c"}]
 [{:type :work, :value "U2uHMI"} {:type :personal, :value "376ihr"}]
 [{:type :personal, :value "n6Ct4d"} {:type :personal, :value "V09xL8"}]
 [{:type :work, :value "9"} {:type :personal, :value ""}])


if the first map is always work and second personal, :tuple for the win:

 [:map [:type :work] [:value string?]]
 [:map [:type :personal] [:value string?]]]


Thank you, the :multi works for my use case 🙏