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

Optional argument default-fn to mt/default-value-transformer #582

Merged
merged 3 commits into from
Feb 15, 2022
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
6 changes: 3 additions & 3 deletions src/malli/transform.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,14 @@
(defn default-value-transformer
([]
(default-value-transformer nil))
([{:keys [key defaults] :or {key :default}}]
([{:keys [key default-fn defaults] :or {key :default, default-fn identity}}]
(let [get-default (fn [schema]
(if-some [e (some-> schema m/properties (find key))]
(constantly (val e))
(some->> schema m/type (get defaults) (#(constantly (% schema))))))
set-default {:compile (fn [schema _]
(when-some [f (get-default schema)]
(fn [x] (if (nil? x) (f) x))))}
(fn [x] (if (nil? x) (default-fn (f)) x))))}
add-defaults {:compile (fn [schema _]
(let [defaults (into {}
(keep (fn [[k {:keys [optional] :as p} v]]
Expand All @@ -396,7 +396,7 @@
(when-some [f (if e
(constantly (val e))
(get-default v))]
[k f])))))
[k (comp default-fn f)])))))
(m/children schema))]
(when (seq defaults)
(fn [x]
Expand Down
24 changes: 23 additions & 1 deletion test/malli/transform_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,29 @@
:tuple (constantly nil)
'string? (constantly "")
'int? (constantly nil)
'double? (constantly nil)}}))))))
'double? (constantly nil)}})))))

(testing "default-fn applied on default value, when any"
(let [schema [:map {:default {}}
[:first {:default 1} int?]
[:second {:default 2} int?]]]
(testing "called on each defaulted value"
(let [seen (atom [])]
(is (= {:first 1, :second 2} (m/encode schema nil (mt/default-value-transformer {:default-fn (fn [x] (swap! seen conj x) x)}))))
(is (= [{} 1 2] @seen))))

(testing "only called on defaulted value"
(let [seen (atom [])]
(is (= {:first -1, :second 2} (m/encode schema {:first -1} (mt/default-value-transformer {:default-fn (fn [x] (swap! seen conj x) x)}))))
(is (= [2] @seen)))))

(testing "custom default :key"
(let [schema [:map {}
[:first {:default 1, :name 'one} int?]
[:second {:default 2, :name 'two} int?]]]
(let [seen (atom [])]
(is (= {:first 'one, :second 'two} (m/encode schema {} (mt/default-value-transformer {:key :name, :default-fn (fn [x] (swap! seen conj x) x)}))))
(is (= ['one 'two] @seen)))))))

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