Skip to content

Commit 50d2e7c

Browse files
committed
fixup! merge + closed bugfix
1 parent d7c7737 commit 50d2e7c

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/exoscale/coax.cljc

+15-4
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,21 @@
129129
[[_ & spec-forms]]
130130
(fn [x {:as opts :keys [closed]}]
131131
(if (map? x)
132-
(into (cond-> x closed empty)
133-
(map (fn [spec-form]
134-
(coerce spec-form x (assoc opts :closed true))))
135-
spec-forms)
132+
(if closed
133+
(into {}
134+
(map (fn [spec-form]
135+
(coerce spec-form x (assoc opts :closed true))))
136+
spec-forms)
137+
;; not closed, we also have to ensure we don't overwrite values with
138+
;; more loose specs towards the end of the args (ex `any?`
139+
(reduce (fn [m spec-form]
140+
(into m
141+
(remove (fn [[k v]]
142+
(= (get x k) v)))
143+
(coerce spec-form x (assoc opts :closed true))))
144+
x
145+
spec-forms))
146+
136147
:exoscale.coax/invalid)))
137148

138149
(defn gen-coerce-nilable

test/exoscale/coax_test.cljc

+5-2
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,9 @@
373373

374374
(deftest test-or-conditions-in-unqualified-keys
375375
(is (= (sc/coerce ::unqualified {:foo "1" :bar "hi"})
376+
{:foo 1 :bar "hi"}))
377+
378+
(is (= (sc/coerce ::unqualified {:foo "1" :bar "hi"} {:closed true})
376379
{:foo 1 :bar "hi"})))
377380

378381
(deftest test-closed-keys
@@ -402,15 +405,15 @@
402405
any?))
403406
(is (= {:foo 1 :bar "1" :c {:a 2}}
404407
(sc/coerce ::merge {:foo "1" :bar 1 :c {:a 2}}))
405-
"Coerce new vals appropriately")
408+
"Coerce new vals appropriately 1")
406409

407410
(is (= {:foo 1 :bar "1" :c {:a 2}}
408411
(sc/coerce ::merge {:foo 1 :bar "1" :c {:a 2}}))
409412
"Leave out ok vals")
410413

411414
(is (= {:foo 1 :bar "1" :c {:a 2}}
412415
(sc/coerce ::merge {:foo "1" :bar 1 :c {:a 2}}))
413-
"Coerce new vals appropriately")
416+
"Coerce new vals appropriately 2")
414417

415418
(s/def ::merge2 (s/merge (s/keys :req [::foo])
416419
::unqualified))

0 commit comments

Comments
 (0)