diff --git a/src/malli/util.cljc b/src/malli/util.cljc index 2c89517e9..1504efd82 100644 --- a/src/malli/util.cljc +++ b/src/malli/util.cljc @@ -378,8 +378,10 @@ ;; (defn -reducing [f] - (fn [_ [first & rest :as children] options] - (let [children (mapv #(m/schema % options) children)] + (fn [_ children options] + (when (empty? children) + (m/-fail! ::reducing-children-must-be-non-empty)) + (let [[first & rest :as children] (mapv #(m/schema % options) children)] [children (mapv m/form children) (delay (reduce #(f %1 %2 options) first rest))]))) (defn -applying [f] @@ -390,8 +392,8 @@ (defn -util-schema [m] (m/-proxy-schema m)) -(defn -merge [] (-util-schema {:type :merge, :fn (-reducing merge)})) -(defn -union [] (-util-schema {:type :union, :fn (-reducing union)})) +(defn -merge [] (-util-schema {:type :merge, :fn (-reducing merge), :min 1})) +(defn -union [] (-util-schema {:type :union, :fn (-reducing union), :min 1})) (defn -select-keys [] (-util-schema {:type :select-keys, :childs 1, :min 2, :max 2, :fn (-applying select-keys)})) (defn schemas [] {:merge (-merge) diff --git a/test/malli/util_test.cljc b/test/malli/util_test.cljc index b4c9df12b..be7e7ac6e 100644 --- a/test/malli/util_test.cljc +++ b/test/malli/util_test.cljc @@ -1112,3 +1112,15 @@ {} {:registry (merge (mu/schemas) (m/default-schemas))} (mt/default-value-transformer {::mt/add-optional-keys true}))))) + +(deftest -reducing-test + (is (= :map (m/form (m/deref-all (m/schema [:merge [:merge :map]] {:registry (merge (mu/schemas) (m/default-schemas))}))))) + (is (= :map (m/form (m/deref-all (m/schema [:union [:union :map]] {:registry (merge (mu/schemas) (m/default-schemas))}))))) + (is (thrown-with-msg? + #?(:clj Exception, :cljs js/Error) + #":malli\.core/child-error" + (m/schema :merge {:registry (merge (mu/schemas) (m/default-schemas))}))) + (is (thrown-with-msg? + #?(:clj Exception, :cljs js/Error) + #":malli\.core/child-error" + (m/schema :union {:registry (merge (mu/schemas) (m/default-schemas))}))))