Skip to content

Commit

Permalink
allow nil as valid fallback value
Browse files Browse the repository at this point in the history
  • Loading branch information
ikitommi committed Sep 5, 2023
1 parent 85de30f commit 0ab9170
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/malli/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 9 additions & 1 deletion test/malli/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down

0 comments on commit 0ab9170

Please sign in to comment.