Skip to content

Commit

Permalink
Don't lose properties when updating a key
Browse files Browse the repository at this point in the history
  • Loading branch information
dmcgillen committed Jun 20, 2022
1 parent f3a7c9b commit b4bde10
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions test/malli/util_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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?]))
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit b4bde10

Please sign in to comment.