Skip to content

Commit 440de1b

Browse files
authored
Merge pull request #1147 from frenchy64/merge-reducing
Propagate options to 1-child -reducing, disallow 0-child
2 parents 826dad8 + 7c9941a commit 440de1b

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/malli/util.cljc

+6-4
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,10 @@
378378
;;
379379

380380
(defn -reducing [f]
381-
(fn [_ [first & rest :as children] options]
382-
(let [children (mapv #(m/schema % options) children)]
381+
(fn [_ children options]
382+
(when (empty? children)
383+
(m/-fail! ::reducing-children-must-be-non-empty))
384+
(let [[first & rest :as children] (mapv #(m/schema % options) children)]
383385
[children (mapv m/form children) (delay (reduce #(f %1 %2 options) first rest))])))
384386

385387
(defn -applying [f]
@@ -390,8 +392,8 @@
390392

391393
(defn -util-schema [m] (m/-proxy-schema m))
392394

393-
(defn -merge [] (-util-schema {:type :merge, :fn (-reducing merge)}))
394-
(defn -union [] (-util-schema {:type :union, :fn (-reducing union)}))
395+
(defn -merge [] (-util-schema {:type :merge, :fn (-reducing merge), :min 1}))
396+
(defn -union [] (-util-schema {:type :union, :fn (-reducing union), :min 1}))
395397
(defn -select-keys [] (-util-schema {:type :select-keys, :childs 1, :min 2, :max 2, :fn (-applying select-keys)}))
396398

397399
(defn schemas [] {:merge (-merge)

test/malli/util_test.cljc

+12
Original file line numberDiff line numberDiff line change
@@ -1112,3 +1112,15 @@
11121112
{}
11131113
{:registry (merge (mu/schemas) (m/default-schemas))}
11141114
(mt/default-value-transformer {::mt/add-optional-keys true})))))
1115+
1116+
(deftest -reducing-test
1117+
(is (= :map (m/form (m/deref-all (m/schema [:merge [:merge :map]] {:registry (merge (mu/schemas) (m/default-schemas))})))))
1118+
(is (= :map (m/form (m/deref-all (m/schema [:union [:union :map]] {:registry (merge (mu/schemas) (m/default-schemas))})))))
1119+
(is (thrown-with-msg?
1120+
#?(:clj Exception, :cljs js/Error)
1121+
#":malli\.core/child-error"
1122+
(m/schema :merge {:registry (merge (mu/schemas) (m/default-schemas))})))
1123+
(is (thrown-with-msg?
1124+
#?(:clj Exception, :cljs js/Error)
1125+
#":malli\.core/child-error"
1126+
(m/schema :union {:registry (merge (mu/schemas) (m/default-schemas))}))))

0 commit comments

Comments
 (0)