Fork me on GitHub
#malli
<
2023-04-26
>
fuad13:04:45

Hello! I have a scenario where malli.transform/strip-extra-keys-transformer is behaving in a way that seems unintuitive to me and I'd like to understand it a bit better

fuad13:04:10

Minimal repro case:

(def schema [:map-of :string :int])

(malli/coerce schema {"foo" 1 "bar" :baz} malli.transform/strip-extra-keys-transformer) ; => {"foo" 1}
Initially I expected the coercion to fail because the key itself (`"bar"`) conforms to the schema but the value doesn't. But I guess the implementation treats map-of by evaluating each map entry tuple as a whole and striping them if they don't conform as a whole.

fuad13:04:19

Now I don't know if there's a right or wrong way to think about this, maybe both perspectives are valid but I'd like to verify my understanding of how coercion of map-of is supposed to behave.

opqdonut13:04:08

I think there's a discussion of this in a ticket

opqdonut13:04:41

I guess it was implemented in this PR but I can't see the discussion I'm remembering

opqdonut13:04:53

Perhaps it was me&Tommi in a chat somewhere...

opqdonut13:04:00

anyway, what you're seeing is the intended behaviour: all entries that don't validate are stripped

fuad13:04:54

Thanks for all the context!

fuad13:04:10

Given that it is the intended behavior, and I think that's a perfectly valid choice, what would be the way to opt-out of it? I would guess a custom transformer, right?

opqdonut13:04:37

Yeah that's certainly one way

opqdonut13:04:54

I'm thinking about some way to override it case-by-case but I didn't figure out anything yet

ikitommi15:04:53

I think there are at least 3 good options: 1. per schema override, e.g. ::mt/strip-extra-keys false property 2. per transformer, like 1 but as a option for mt/strip-extra-keys-transformer 3. both of 1 & 2

Noah Bogart20:04:46

Is there predicate for vars? I have a map of keywords to vars and it feels bad to use :any. I posted an https://github.com/metosin/malli/issues/732 but that's a while ago and it's possible something changed that I missed

1
rolt15:04:45

would this work ? [:map-of keyword? [:fn var?]]

👍 2
Noah Bogart17:04:45

oh yeah, interesting, let me try that

Noah Bogart15:05:45

That works great, idk why I didn't think of it. Thank you!