diff --git a/doc/coercion/malli_coercion.md b/doc/coercion/malli_coercion.md index eb25b27c..b193db11 100644 --- a/doc/coercion/malli_coercion.md +++ b/doc/coercion/malli_coercion.md @@ -88,9 +88,9 @@ Using `create` with options to create the coercion instead of `coercion`: ;; strip-extra-keys (affects only predefined transformers) :strip-extra-keys true ;; add/set default values + ;; Can be false, true or a map of options to pass to malli.transform/default-value-transformer, + ;; for example {:malli.transform/add-optional-keys true} :default-values true - ;; add/set defaults also for optional keys. Corresponds to :malli.transform/add-optional-keys - :default-values-for-optional-keys false ;; encode-error :encode-error nil ;; malli options diff --git a/modules/reitit-malli/src/reitit/coercion/malli.cljc b/modules/reitit-malli/src/reitit/coercion/malli.cljc index 568f605d..d8fbd663 100644 --- a/modules/reitit-malli/src/reitit/coercion/malli.cljc +++ b/modules/reitit-malli/src/reitit/coercion/malli.cljc @@ -26,11 +26,11 @@ (defn- -provider [transformer] (reify TransformationProvider - (-transformer [_ {:keys [strip-extra-keys default-values default-values-for-optional-keys]}] + (-transformer [_ {:keys [strip-extra-keys default-values]}] (mt/transformer (if strip-extra-keys (mt/strip-extra-keys-transformer)) transformer - (if default-values (mt/default-value-transformer {:malli.transform/add-optional-keys default-values-for-optional-keys})))))) + (if default-values (mt/default-value-transformer (if (map? default-values) default-values {}))))))) (def string-transformer-provider (-provider (mt/string-transformer))) (def json-transformer-provider (-provider (mt/json-transformer))) @@ -115,10 +115,10 @@ :enabled true ;; strip-extra-keys (affects only predefined transformers) :strip-extra-keys true - ;; add/set default values + ;; add/set default values. + ;; Can be false, true or a map of options to pass to malli.transform/default-value-transformer, + ;; for example {:malli.transform/add-optional-keys true} :default-values true - ;; add/set defaults also for optional keys. Corresponds to :malli.transform/add-optional-keys - :default-values-for-optional-keys false ;; encode-error :encode-error nil ;; malli options diff --git a/test/cljc/reitit/coercion_test.cljc b/test/cljc/reitit/coercion_test.cljc index e1f0cd20..5089ba29 100644 --- a/test/cljc/reitit/coercion_test.cljc +++ b/test/cljc/reitit/coercion_test.cljc @@ -153,13 +153,22 @@ (-> (r/match-by-path (router reitit.coercion.malli/coercion) "/test") (assoc :query-params {}) (coercion/coerce!))))) - (testing "default values for :optional query keys get added when :default-values-for-optional-keys is set" + (testing "default values for :optional query keys get added when :malli.transform/add-optional-keys is set" (is (= {:query {:a "a" :x :a}} (-> (r/match-by-path (router (reitit.coercion.malli/create (assoc reitit.coercion.malli/default-options - :default-values-for-optional-keys true))) "/test") + :default-values {:malli.transform/add-optional-keys true}))) "/test") (assoc :query-params {}) - (coercion/coerce!))))))) + (coercion/coerce!))))) + (testing "default values can be disabled" + (is (thrown-with-msg? + ExceptionInfo + #"Request coercion failed" + (-> (r/match-by-path (router (reitit.coercion.malli/create + (assoc reitit.coercion.malli/default-options + :default-values false))) "/test") + (assoc :query-params {}) + (coercion/coerce!))))))) (defn match-by-path-and-coerce! [router path] (if-let [match (r/match-by-path router path)]