Skip to content

Commit

Permalink
Merge pull request #1147 from frenchy64/merge-reducing
Browse files Browse the repository at this point in the history
Propagate options to 1-child -reducing, disallow 0-child
  • Loading branch information
ikitommi authored Dec 12, 2024
2 parents 826dad8 + 7c9941a commit 440de1b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/malli/util.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions test/malli/util_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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))}))))

0 comments on commit 440de1b

Please sign in to comment.