malli

https://github.com/metosin/malli :malli:
elarouss 2020-12-22T22:41:47.237100Z

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

[{:type :work
  :value "<mailto:work@work.com|work@work.com>"}
 {:type :personal
  :value "<mailto:personal@personal.com|personal@personal.com>"}]
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 πŸ™

ikitommi 2020-12-23T08:09:10.239400Z

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

ikitommi 2020-12-23T08:10:55.239600Z

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

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

elarouss 2020-12-23T08:22:52.239800Z

Thank you, the :multi works for my use case πŸ™

alpox 2020-12-22T23:14:43.238400Z

@ichigo 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