From 0ab9170aa7e321524ad97cdb5ee0bc0d09c5d4b2 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Tue, 5 Sep 2023 18:37:39 +0300 Subject: [PATCH] allow nil as valid fallback value --- src/malli/core.cljc | 4 ++-- test/malli/core_test.cljc | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/malli/core.cljc b/src/malli/core.cljc index 6cdd31ae4..8808e22f2 100644 --- a/src/malli/core.cljc +++ b/src/malli/core.cljc @@ -560,8 +560,8 @@ (let [x* (transformer x)] (if ((nth validators i) x*) (reduced x*) - (or acc x*)))) - nil transformers)) + (if (-equals acc ::nil) x* acc)))) + ::nil transformers)) (fn [x] (reduce-kv (fn [x i validator] (if (validator x) (reduced ((nth transformers i) x)) x)) diff --git a/test/malli/core_test.cljc b/test/malli/core_test.cljc index 367a1867f..2ef6fba5b 100644 --- a/test/malli/core_test.cljc +++ b/test/malli/core_test.cljc @@ -319,7 +319,15 @@ :string] 1 math) (m/decode [:orn ["kw-math" math-kw-string] - ["string" :string]] 1 math))))) + ["string" :string]] 1 math)))) + + (testing "first branch nil can be selected as a fallback" + (is (= nil (m/decode + [:or + [:keyword {:decode/math (constantly nil)}] + :keyword] + "kikka" + (mt/transformer {:name :math})))))) (testing "top-level transformations are retained" (doseq [schema [[:or {:decode/string {:enter (fn [m] (update m :enter #(or % true)))