Is it possible to create a key in a map using decode
?
something like {:a 1 :b 2} => {:c 3}
Sure it is
(m/decode [:map {:decode/abc (fn [m]
(if (and (map? m) (contains? m :a) (contains? m :b))
(-> m
(dissoc :a :b)
(assoc :c (+ (:a m) (:b m))))
m))}
[:a :int] [:b :int]]
{:a 1, :b 2}
(mt/transformer {:name :abc}))
=> {:c 3}
https://github.com/metosin/malli#value-transformation does have this info although the explanations are a bit sparse
I've read it, but somehow it didn't occur to me that I could use decode/encode on the whole map and not on an individual key
great!
Yes that is the "key" to changing keys :drum_with_drumsticks:
👍 small nitpick: the example could be encode
as it transforms the value out of schema. Or the schema should not require :a
& :b
as they are not in the result.
Indeed