From b4bde10e3269802913eff5c279c07d6dec6f266f Mon Sep 17 00:00:00 2001 From: Donovan McGillen Date: Mon, 20 Jun 2022 11:31:20 +0100 Subject: [PATCH] Don't lose properties when updating a key --- src/malli/core.cljc | 4 ++-- test/malli/util_test.cljc | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/malli/core.cljc b/src/malli/core.cljc index e282d0675..dc877c4b0 100644 --- a/src/malli/core.cljc +++ b/src/malli/core.cljc @@ -374,8 +374,8 @@ ;; remove (letfn [(cut [v] (into (subvec v 0 i) (subvec v (inc i))))] (-simple-entry-parser (dissoc keyset k) (cut children) (cut forms))) - (let [c [k p s] - p (if i (if override p (nth (children i) 1)) p) + (let [p (if i (if override p (nth (children i) 1)) p) + c [k p s] f (if (seq p) [k p (-form s)] [k (-form s)])] (if i ;; update diff --git a/test/malli/util_test.cljc b/test/malli/util_test.cljc index af7412f21..e499cd1b2 100644 --- a/test/malli/util_test.cljc +++ b/test/malli/util_test.cljc @@ -466,7 +466,13 @@ [:a int?] [:b {:optional true} int?] [:c string?] - [:d boolean?]])))) + [:d boolean?]])) + + (testing "Optional property is maintained when optional key is updated" + (is (true? (m/validate + (mu/update schema :b identity) + {:a 1 + :c "a string"})))))) (deftest assoc-in-test (is (mu/equals (mu/assoc-in (m/schema [:vector int?]) [0] string?) [:vector string?])) @@ -507,7 +513,17 @@ [:map [:a {:optional false} [:map [:x {:optional false} any?]]]])) (is (mu/equals (mu/update-in (m/schema [:map [:a {:optional true} [:map [:x {:optional true} int?]]]]) [[:a] [:x {:optional false}]] (constantly any?)) - [:map [:a [:map [:x {:optional false} any?]]]]))) + [:map [:a [:map [:x {:optional false} any?]]]])) + + (testing "Optional property is maintained when optional key is updated" + (let [schema [:map {:title "map"} + [:a int?] + [:b {:optional true} int?] + [:c string?]]] + (is (true? (m/validate + (mu/update-in schema [:b] identity) + {:a 1 + :c "a string"})))))) (deftest transform-entries-test (let [registry (mr/composite-registry {:a/x int?} (m/default-schemas))