Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

look inside :ref when searching for default for :map child #1146

Merged
merged 2 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/malli/transform.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,9 @@
(when (or (not optional) add-optional-keys)
(let [e (find p key)]
(when-some [f (if e (constantly (val e))
(get-default v))]
(or (get-default v)
(when (m/-ref-schema? v)
(get-default (m/-deref v)))))]
[k (fn [] (default-fn schema (f)))])))))
(m/children schema))]
(when (seq defaults)
Expand Down
14 changes: 13 additions & 1 deletion test/malli/transform_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[clojure.test :refer [are deftest is testing]]
[malli.core :as m]
[malli.core-test]
[malli.registry :as mr]
[malli.transform :as mt])
#?(:clj (:import (java.net URI))))

Expand Down Expand Up @@ -1032,7 +1033,18 @@

(testing ":default/fn property on schema"
(let [schema [:string {:default/fn (fn [] "called")}]]
(is (= "called" (m/decode schema nil mt/default-value-transformer))))))
(is (= "called" (m/decode schema nil mt/default-value-transformer)))))

(testing ":refs"
(let [opts {:registry (mr/composite-registry m/default-registry
{"bing" :int})}
transformer (mt/default-value-transformer {:defaults {:int (constantly 7)}})]
(is (= 7 (m/decode [:ref "bing"] nil opts transformer)))
(is (= [7] (m/decode [:vector [:ref "bing"]] [nil] opts transformer)))
(is (= {:a 7} (m/decode [:map [:a [:ref "bing"]]] {:a nil} opts transformer)))
(is (= {:a 7} (m/decode [:map [:a [:ref "bing"]]] {} opts transformer)))
(is (= {:a 8} (m/decode [:map [:a [:ref {:default 8} "bing"]]] {:a nil} opts transformer)))
(is (= {:a 8} (m/decode [:map [:a [:ref {:default 8} "bing"]]] {} opts transformer))))))

(deftest type-properties-based-transformations
(is (= 12 (m/decode malli.core-test/Over6 "12" mt/string-transformer))))
Expand Down
Loading