malli icon indicating copy to clipboard operation
malli copied to clipboard

Map tuples are walked twice during decode

Open esuomi opened this issue 2 years ago • 0 comments

As per this snippet:

(malli.core/decode
  [:map [:a {:foo :bar} :int]]
  {:a "string"}
  (malli.transform/transformer
    {:name :hello
     :default-decoder
     {:compile (fn [schema _]
                 (println (str "node schema: " (malli.core/-form schema)))
                 (fn [x]
                   (println (str "value:" x " / schema at val: " (malli.core/-form schema)))
                   x))}}))
node schema: [:map [:a {:foo :bar} :int]]
node schema: [:malli.core/val {:foo :bar} :int]
node schema: :int
value:{:a "string"} / schema at val: [:map [:a {:foo :bar} :int]]
value:string / schema at val: [:malli.core/val {:foo :bar} :int]
value:string / schema at val: :int
=> {:a "string"}

It seems the :a tuple is walked twice, once for the tuple itself and once for the tuple’s value. I find this confusing, as the associative nature of [:a :int] would imply the properties of the key apply to the value as well.

To get the properties to the value, the schema

[:map [:a [:int {:foo :bar}]]]

does provide properties for the :int which really was my intention, and it sort of makes sense, but it’s of course a bit kludgier.

My assumptions here are

  • if the key is marked optional, then its value is optional as well
  • key properties should apply to the value as well
  • the value shouldn’t be walked on its own

esuomi avatar Jun 07 '22 08:06 esuomi