File tree 2 files changed +26
-16
lines changed
2 files changed +26
-16
lines changed Original file line number Diff line number Diff line change 127
127
128
128
(defn gen-coerce-merge
129
129
[[_ & spec-forms]]
130
- (fn [x opts]
130
+ (fn [x { :as opts :keys [closed]} ]
131
131
(if (map? x)
132
- (reduce (fn [m spec-form]
133
- ; ; for every spec-form coerce to new value;
134
- ; ; we need to compare key by key what changed so that
135
- ; ; defaults do not overwrite coerced values
136
- (into m
137
- (keep (fn [[spec v]]
138
- ; ; new-val doesn't match default, keep it
139
- (when-not (= (get x spec) v)
140
- [spec v])))
141
- (coerce spec-form x (assoc opts :closed true ))))
142
- x
132
+ (if closed
133
+ (into {}
134
+ (map (fn [spec-form]
135
+ (coerce spec-form x (assoc opts :closed true ))))
143
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
+
144
147
:exoscale.coax/invalid )))
145
148
146
149
(defn gen-coerce-nilable
Original file line number Diff line number Diff line change 373
373
374
374
(deftest test-or-conditions-in-unqualified-keys
375
375
(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 })
376
379
{:foo 1 :bar " hi" })))
377
380
378
381
(deftest test-closed-keys
402
405
any?))
403
406
(is (= {:foo 1 :bar " 1" :c {:a 2 }}
404
407
(sc/coerce ::merge {:foo " 1" :bar 1 :c {:a 2 }}))
405
- " Coerce new vals appropriately" )
408
+ " Coerce new vals appropriately 1 " )
406
409
407
410
(is (= {:foo 1 :bar " 1" :c {:a 2 }}
408
411
(sc/coerce ::merge {:foo 1 :bar " 1" :c {:a 2 }}))
409
412
" Leave out ok vals" )
410
413
411
414
(is (= {:foo 1 :bar " 1" :c {:a 2 }}
412
415
(sc/coerce ::merge {:foo " 1" :bar 1 :c {:a 2 }}))
413
- " Coerce new vals appropriately" )
416
+ " Coerce new vals appropriately 2 " )
414
417
415
418
(s/def ::merge2 (s/merge (s/keys :req [::foo ])
416
419
::unqualified ))
417
420
418
- (is (= {::foo 1 :bar " 1" :c {:a 2 }
419
- :foo 1 }
421
+ (is (= {::foo 1 :bar " 1" :c {:a 2 } :foo 1 }
420
422
(sc/coerce ::merge2 {::foo " 1" :foo " 1" :bar " 1" :c {:a 2 }}))
421
423
" Leave out ok vals" )
422
424
425
+ (is (= {::foo 1 :bar " 1" :foo 1 }
426
+ (sc/coerce ::merge2 {::foo " 1" :foo " 1" :bar " 1" :c {:a 2 }}
427
+ {:closed true }))
428
+ " Remove extras" )
429
+
423
430
(is (= " garbage" (sc/coerce ::merge " garbage" ))
424
431
" garbage is passthrough" )
425
432
You can’t perform that action at this time.
0 commit comments