feat: reuse :default-values config key instead of adding a new one

This commit is contained in:
Joel Kaasinen 2025-10-13 15:16:16 +03:00
parent f26dc1ab19
commit 67918a3f9c
3 changed files with 19 additions and 10 deletions

View file

@ -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 (affects only predefined transformers)
:strip-extra-keys true :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 :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
:encode-error nil :encode-error nil
;; malli options ;; malli options

View file

@ -26,11 +26,11 @@
(defn- -provider [transformer] (defn- -provider [transformer]
(reify TransformationProvider (reify TransformationProvider
(-transformer [_ {:keys [strip-extra-keys default-values default-values-for-optional-keys]}] (-transformer [_ {:keys [strip-extra-keys default-values]}]
(mt/transformer (mt/transformer
(if strip-extra-keys (mt/strip-extra-keys-transformer)) (if strip-extra-keys (mt/strip-extra-keys-transformer))
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 string-transformer-provider (-provider (mt/string-transformer)))
(def json-transformer-provider (-provider (mt/json-transformer))) (def json-transformer-provider (-provider (mt/json-transformer)))
@ -115,10 +115,10 @@
:enabled true :enabled true
;; strip-extra-keys (affects only predefined transformers) ;; strip-extra-keys (affects only predefined transformers)
:strip-extra-keys true :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 :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
:encode-error nil :encode-error nil
;; malli options ;; malli options

View file

@ -153,13 +153,22 @@
(-> (r/match-by-path (router reitit.coercion.malli/coercion) "/test") (-> (r/match-by-path (router reitit.coercion.malli/coercion) "/test")
(assoc :query-params {}) (assoc :query-params {})
(coercion/coerce!))))) (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}} (is (= {:query {:a "a" :x :a}}
(-> (r/match-by-path (router (reitit.coercion.malli/create (-> (r/match-by-path (router (reitit.coercion.malli/create
(assoc reitit.coercion.malli/default-options (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 {}) (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] (defn match-by-path-and-coerce! [router path]
(if-let [match (r/match-by-path router path)] (if-let [match (r/match-by-path router path)]