From b88b713ee07d96f37b861009e496a0395d468d6e Mon Sep 17 00:00:00 2001 From: piotr-yuxuan Date: Tue, 23 Nov 2021 19:22:57 +0000 Subject: [PATCH] Optional argument default-fn to default-value-transformer See an example usage here: https://github.com/piotr-yuxuan/malli-cli/blob/main/src/piotr_yuxuan/malli_cli.clj#L220-L222 This doesn't change existing behaviour but only add `default-fn` optional argument. When set, this function is applied on the default value found under key `key`. This optional, additional indirection allows a seamless way to define `:env-var` defaults. --- src/malli/transform.cljc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/malli/transform.cljc b/src/malli/transform.cljc index a0028012d..ff90e39b3 100644 --- a/src/malli/transform.cljc +++ b/src/malli/transform.cljc @@ -378,7 +378,7 @@ (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)) @@ -402,7 +402,9 @@ (reduce-kv (fn [acc k f] (if-not (contains? x k) - (assoc acc k (f)) + (if-let [d (default-fn (f))] + (assoc acc k d) + acc) acc)) x defaults) x)))))}]