Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge + closed bugfix #32

Merged
merged 4 commits into from
Jan 6, 2025
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
27 changes: 15 additions & 12 deletions src/exoscale/coax.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,23 @@

(defn gen-coerce-merge
[[_ & spec-forms]]
(fn [x opts]
(fn [x {:as opts :keys [closed]}]
(if (map? x)
(reduce (fn [m spec-form]
;; for every spec-form coerce to new value;
;; we need to compare key by key what changed so that
;; defaults do not overwrite coerced values
(into m
(keep (fn [[spec v]]
;; new-val doesn't match default, keep it
(when-not (= (get x spec) v)
[spec v])))
(coerce spec-form x (assoc opts :closed true))))
x
(if closed
(into {}
(map (fn [spec-form]
(coerce spec-form x (assoc opts :closed true))))
spec-forms)
;; not closed, we also have to ensure we don't overwrite values with
;; more loose specs towards the end of the args (ex `any?`
(reduce (fn [m spec-form]
(into m
(remove (fn [[k v]]
(= (get x k) v)))
(coerce spec-form x (assoc opts :closed true))))
x
spec-forms))

:exoscale.coax/invalid)))

(defn gen-coerce-nilable
Expand Down
15 changes: 11 additions & 4 deletions test/exoscale/coax_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@

(deftest test-or-conditions-in-unqualified-keys
(is (= (sc/coerce ::unqualified {:foo "1" :bar "hi"})
{:foo 1 :bar "hi"}))

(is (= (sc/coerce ::unqualified {:foo "1" :bar "hi"} {:closed true})
{:foo 1 :bar "hi"})))

(deftest test-closed-keys
Expand Down Expand Up @@ -402,24 +405,28 @@
any?))
(is (= {:foo 1 :bar "1" :c {:a 2}}
(sc/coerce ::merge {:foo "1" :bar 1 :c {:a 2}}))
"Coerce new vals appropriately")
"Coerce new vals appropriately 1")

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

(is (= {:foo 1 :bar "1" :c {:a 2}}
(sc/coerce ::merge {:foo "1" :bar 1 :c {:a 2}}))
"Coerce new vals appropriately")
"Coerce new vals appropriately 2")

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

(is (= {::foo 1 :bar "1" :c {:a 2}
:foo 1}
(is (= {::foo 1 :bar "1" :c {:a 2} :foo 1}
(sc/coerce ::merge2 {::foo "1" :foo "1" :bar "1" :c {:a 2}}))
"Leave out ok vals")

(is (= {::foo 1 :bar "1" :foo 1}
(sc/coerce ::merge2 {::foo "1" :foo "1" :bar "1" :c {:a 2}}
{:closed true}))
"Remove extras")

(is (= "garbage" (sc/coerce ::merge "garbage"))
"garbage is passthrough")

Expand Down
Loading