metosin / malli

High-performance data-driven data specification library for Clojure/Script.
Eclipse Public License 2.0
1.44k stars 205 forks source link

:map schemas should decode their keys #902

Open cap10morgan opened 1 year ago

cap10morgan commented 1 year ago

It would be nice if :map schemas like [:map [:foo :string] [:bar :string]] when decoding w/ the json-transformer a map value like {"foo" "whatever" "bar" "thingy"} turned those keys into keywords (or whatever other coerceable type is in the :map schema) similar to what e.g. [:map-of :keyword :string] does.

Sometimes this can be accomplished by adding in a key-transformer too but sometimes you don't want every key transformed the same way. And the :map schema already implicitly has all of the information you need to decode keys to their desired types (e.g. :my-key vs. "my-key").

You can currently work around this with a custom key schema and decoder fn and something like:

[:and
 [:map-of ::my-map-key-schema :any]
 [:map [:foo :string] [:bar :string] ["qux" :string]]]

...but it's a little clunky and redundant. The :map schema already contains all of the info necessary to do the proper decoding.

I'd be happy to take a crack at implementing this if it seems desirable to others.