From df6ca4f0afbc02b74e8dd86b379b48c5afdc567e Mon Sep 17 00:00:00 2001 From: Pauli Jaakkola Date: Thu, 18 Mar 2021 16:57:27 +0200 Subject: [PATCH 1/5] Reformatting for readability --- src/malli/transform.cljc | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/malli/transform.cljc b/src/malli/transform.cljc index 261662987..19a6738a5 100644 --- a/src/malli/transform.cljc +++ b/src/malli/transform.cljc @@ -380,15 +380,20 @@ ([] (default-value-transformer nil)) ([{:keys [key defaults] :or {key :default}}] - (let [get-default (fn [schema] (let [default (some-> schema m/properties key)] - (if (some? default) default (some->> schema m/type (get defaults) (#(% schema)))))) + (let [get-default (fn [schema] + (let [default (some-> schema m/properties key)] + (if (some? default) + default + (some->> schema m/type (get defaults) (#(% schema)))))) set-default {:compile (fn [schema _] (if-some [default (get-default schema)] (fn [x] (if (nil? x) default x))))} add-defaults {:compile (fn [schema _] (let [defaults (->> (m/children schema) (keep (fn [[k {default key} v]] - (if-some [default (if (some? default) default (get-default v))] + (if-some [default (if (some? default) + default + (get-default v))] [k default]))) (into {}))] (if (seq defaults) From 98429e5f941e5521573592ba0ff60994587ff300 Mon Sep 17 00:00:00 2001 From: Pauli Jaakkola Date: Thu, 18 Mar 2021 17:03:23 +0200 Subject: [PATCH 2/5] Idiomatize conditionals --- src/malli/transform.cljc | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/malli/transform.cljc b/src/malli/transform.cljc index 19a6738a5..6d62f4947 100644 --- a/src/malli/transform.cljc +++ b/src/malli/transform.cljc @@ -381,22 +381,21 @@ (default-value-transformer nil)) ([{:keys [key defaults] :or {key :default}}] (let [get-default (fn [schema] - (let [default (some-> schema m/properties key)] - (if (some? default) - default - (some->> schema m/type (get defaults) (#(% schema)))))) + (if-some [default (some-> schema m/properties key)] + default + (some->> schema m/type (get defaults) (#(% schema))))) set-default {:compile (fn [schema _] - (if-some [default (get-default schema)] + (when-some [default (get-default schema)] (fn [x] (if (nil? x) default x))))} add-defaults {:compile (fn [schema _] (let [defaults (->> (m/children schema) (keep (fn [[k {default key} v]] - (if-some [default (if (some? default) - default - (get-default v))] + (when-some [default (if (some? default) + default + (get-default v))] [k default]))) (into {}))] - (if (seq defaults) + (when (seq defaults) (fn [x] (if (map? x) (reduce-kv From cad4c7263b2e832a9db287a863998cf11663710b Mon Sep 17 00:00:00 2001 From: Pauli Jaakkola Date: Thu, 18 Mar 2021 17:13:55 +0200 Subject: [PATCH 3/5] Make `default-value-transformer` ignore optional keys --- src/malli/transform.cljc | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/malli/transform.cljc b/src/malli/transform.cljc index 6d62f4947..17f9364d4 100644 --- a/src/malli/transform.cljc +++ b/src/malli/transform.cljc @@ -388,13 +388,14 @@ (when-some [default (get-default schema)] (fn [x] (if (nil? x) default x))))} add-defaults {:compile (fn [schema _] - (let [defaults (->> (m/children schema) - (keep (fn [[k {default key} v]] - (when-some [default (if (some? default) - default - (get-default v))] - [k default]))) - (into {}))] + (let [defaults (into {} + (keep (fn [[k {default key :keys [optional]} v]] + (when-not optional + (when-some [default (if (some? default) + default + (get-default v))] + [k default])))) + (m/children schema))] (when (seq defaults) (fn [x] (if (map? x) From 076be3aa9ccd9b5f3a25a148dad273b8e6092591 Mon Sep 17 00:00:00 2001 From: Pauli Jaakkola Date: Thu, 18 Mar 2021 17:22:55 +0200 Subject: [PATCH 4/5] Add test for optional key not being added --- test/malli/transform_test.cljc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/malli/transform_test.cljc b/test/malli/transform_test.cljc index e63a1df1b..ba1a19c88 100644 --- a/test/malli/transform_test.cljc +++ b/test/malli/transform_test.cljc @@ -770,6 +770,11 @@ (is (= {:user/verified false} (m/decode [:map [:user/verified {:default false} boolean?]] {} mt/default-value-transformer))) (is (= false (m/decode [:and {:default false} boolean?] nil mt/default-value-transformer)))) + (testing "optional key" + (is (= {:x 5} (m/decode [:map [:x :int] [:y {:optional true}]] {:x 5} mt/default-value-transformer))) + (is (= {:x 5} (m/decode [:map [:x :int] [:y {:optional true} [:int {:default 0}]]] {:x 5} + mt/default-value-transformer)))) + (testing "with custom options" (is (= false (m/decode [:and {:? false} boolean?] nil (mt/default-value-transformer {:key :?})))) (is (= {:user {:first-name "", :last-name ""}} From 2b6832611f1163e3c8a187dc9c75113f8fe89ff9 Mon Sep 17 00:00:00 2001 From: Pauli Jaakkola Date: Thu, 18 Mar 2021 17:27:29 +0200 Subject: [PATCH 5/5] Fix invalid schema in test --- test/malli/transform_test.cljc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/malli/transform_test.cljc b/test/malli/transform_test.cljc index ba1a19c88..9f2dfc521 100644 --- a/test/malli/transform_test.cljc +++ b/test/malli/transform_test.cljc @@ -771,7 +771,8 @@ (is (= false (m/decode [:and {:default false} boolean?] nil mt/default-value-transformer)))) (testing "optional key" - (is (= {:x 5} (m/decode [:map [:x :int] [:y {:optional true}]] {:x 5} mt/default-value-transformer))) + (is (= {:x 5} (m/decode [:map [:x :int] [:y {:optional true, :default 0} :int]] {:x 5} + mt/default-value-transformer))) (is (= {:x 5} (m/decode [:map [:x :int] [:y {:optional true} [:int {:default 0}]]] {:x 5} mt/default-value-transformer))))