Fork me on GitHub
#malli
<
2023-08-15
>
jahson10:08:26

Hi! Looks like after https://github.com/metosin/malli/blob/master/CHANGELOG.md#0103-2023-03-18 mt/strip-extra-keys-transformer started to strip all keys from :map if there is no ::m/default provided.

(m/decode
 [:map]
 {1 1, 2 "2", "3" 3, "4" "4"}
 (mt/strip-extra-keys-transformer))
; => {}
Is it working as intended?

Noah Bogart13:08:11

if you don't mind, how do you expect it to work?

jahson13:08:38

I never thought about it, actually.

jahson13:08:53

And I might be biased by the way it worked before.

Noah Bogart13:08:02

how did it work before?

jahson13:08:57

Actually, seems like I don’t really know 🙃

jahson13:08:07

Trying to reproduce )

Noah Bogart13:08:20

that's annoying

jahson13:08:54

Okay, got it.

jahson14:08:17

(m/decode
  [:map]
  {1 1, 2 "2", "3" 3, "4" "4"}
  (mt/strip-extra-keys-transformer))
; => {1 1, 2 "2", "3" 3, "4" "4"}

jahson14:08:37

Seems like my memory not failed me this time.

jahson14:08:00

[metosin/malli "0.10.2"]

Noah Bogart14:08:17

i'm asking because to me, stripping all keys is the only way i can make sense of "map schema with no keys" plus "strip extra keys". if there are no defined keys, then every key is extra. for validation, :map is completely open. but for stripping (coercion), idk if i agree

jahson14:08:05

That is one way to think about it. The other way might be to think of [:map] as an open map that might contain any key with any value. This way you get a map that might evolve for some time, before it could take final form. But as I’ve said, I might be biased, because it just took a couple of hours of my life )

Noah Bogart14:08:21

yeah for sure, i don't mean to discount the effort

jahson14:08:49

Anyway, this change isn’t marked as breaking in changelog and my intent was to know if this effect was intended, and if it was, then propose to mark is as breaking.

👍 2
Noah Bogart14:08:43

I bet the folks at metosin would be willing to do that for you. they're responsive on github

ikitommi16:08:28

Looks fishy. Just ending my 9 week vacation, have no memory of why that is changed. Looking at the CHANGELOG:

* mt/strip-extra-keys-transformer works with :map-of.
might have changed the behavior of :map unintentionally. Please write an issue of this. I’ll check the commit messages if there is clue for this.

👌 2
ikitommi16:08:13

(testing "extra keys from :map are stripped"
    (is (= {:x 1, :y 2}
           (m/decode
            [:map [:x :int] [:y :int]]
            {:x 1, :y 2, :z 3}
            (mt/strip-extra-keys-transformer)))))

ikitommi16:08:16

:thinking_face:

ikitommi16:08:23

as maps are open by default, I think it’s bit odd to strip extra keys. For closed maps, sure.

jahson21:08:42

Another side effect of this change is when you merge two map definitions and the first one has default key:

(def registry (merge (m/default-schemas) (mu/schemas)))
  (def Merged
    (m/schema
     [:merge
      [:map
       [:x :string]
       [::m/default [:map-of :keyword :any]]]
      [:map {:closed true}
       [:y :int]]]
     {:registry registry}))

  (m/validate Merged {:x "kikka", :y 6, :z "invalid"})
; => true
I.e. map is marked as closed, but it is not really closed.