Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Malli is in well matured [alpha](README.md#alpha).
* **BREAKING**: `:gen/fmap` property requires its schema to create a generator.
* previous behavior defaulted to a `nil`-returning generator, even if the schema doesn't accept `nil`
* use `:gen/return nil` property to restore this behavior
* Support decoding map keys into keywords for `[:map` schemas in `json-transformer` [#1135](https://github.com/metosin/malli/issues/1135)
* FIX: `malli.registry/{mode,type}` not respected in Babashka [#1124](https://github.com/metosin/malli/issues/1124)
* Updated dependencies:

Expand Down
19 changes: 16 additions & 3 deletions src/malli/transform.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns malli.transform
#?(:cljs (:refer-clojure :exclude [Inst Keyword UUID]))
(:require [malli.core :as m]
[malli.util :as mu]
[clojure.math :as math]
#?(:cljs [goog.date.UtcDateTime])
#?(:cljs [goog.date.Date]))
Expand Down Expand Up @@ -191,8 +192,11 @@
(catch #?(:clj Exception, :cljs js/Error) _ x))
x))

(defn -transform-map-keys [f]
#(cond->> % (map? %) (into {} (map (fn [[k v]] [(f k) v])))))
(defn -transform-map-keys
([f]
#(cond->> % (map? %) (into {} (map (fn [[k v]] [(f k) v])))))
([ks f]
#(cond->> % (map? %) (into {} (map (fn [[k v]] [(cond-> k (contains? ks k) f) v]))))))

(defn -transform-if-valid [f schema]
(let [validator (m/-validator schema)]
Expand Down Expand Up @@ -403,7 +407,9 @@

(defn json-transformer
([] (json-transformer nil))
([{::keys [json-vectors map-of-key-decoders] :or {map-of-key-decoders (-string-decoders)}}]
([{::keys [json-vectors
keywordize-map-keys
map-of-key-decoders] :or {map-of-key-decoders (-string-decoders)}}]
(transformer
{:name :json
:decoders (-> (-json-decoders)
Expand All @@ -415,6 +421,13 @@
(-transform-if-valid key-schema)
(-transform-map-keys))
(-transform-map-keys m/-keyword->string))))})
(cond-> keywordize-map-keys
(assoc :map {:compile (fn [schema _]
(let [keyword-keys (->> (mu/keys schema)
(filter keyword?)
(map name)
set)]
(-transform-map-keys keyword-keys -string->keyword)))}))
(cond-> json-vectors (assoc :vector -sequential->vector)))
:encoders (-json-encoders)})))

Expand Down
18 changes: 18 additions & 0 deletions test/malli/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,24 @@
(mt/key-transformer
{:decode #(-> % name (str "_key") keyword)}))))

(testing "JSON transformer can decode map schema keys"
(let [schema
[:map
[:a :uuid]
[:b [:enum :x :y :z]]
["s" :boolean [:enum :f1 :f2]]]
value
{"a" "b699671c-d34d-b33f-1337-dbdbfd337e73"
"b" "x"
"s" "f1"}
expected-decoded-value
{:a #uuid "b699671c-d34d-b33f-1337-dbdbfd337e73"
:b :x
"s" :f1}
decoded-value (m/decode schema value (mt/json-transformer {::mt/keywordize-map-keys true}))]
(is (= expected-decoded-value decoded-value))
(is (m/validate schema decoded-value))))

(is (= {:x 32}
(m/decode
[:map {:decode/string '{:enter #(update % :x inc), :leave #(update % :x (partial * 2))}}
Expand Down