diff --git a/doc/coercion/malli_coercion.md b/doc/coercion/malli_coercion.md index b193db11..73f751ac 100644 --- a/doc/coercion/malli_coercion.md +++ b/doc/coercion/malli_coercion.md @@ -96,3 +96,29 @@ Using `create` with options to create the coercion instead of `coercion`: ;; malli options :options nil}) ``` + +## Configuring humanize error messages + +Malli humanized error messages can be configured using `:options :errors`: + +```clj +(reitit.coercion.malli/create + {:options + {:errors (assoc malli.error/default-errors + :malli.core/missing-key {:error/message {:en "MISSING"}})}}) +``` + +See the malli docs for more info. + +## Custom registry + +Malli registry can be configured conveniently via `:options :registry`: + +```clj +(require '[malli.core :as m]) + +(reitit.coercion.malli/create + {:options + {:registry {:registry (merge (m/default-schemas) + {:my-type :string})}}}) +``` diff --git a/modules/reitit-malli/src/reitit/coercion/malli.cljc b/modules/reitit-malli/src/reitit/coercion/malli.cljc index d8fbd663..2e4ab782 100644 --- a/modules/reitit-malli/src/reitit/coercion/malli.cljc +++ b/modules/reitit-malli/src/reitit/coercion/malli.cljc @@ -188,7 +188,8 @@ (-open-model [_ schema] schema) (-encode-error [_ error] (cond-> error - (show? :humanized) (assoc :humanized (me/humanize error {:wrap :message})) + (show? :humanized) (assoc :humanized (me/humanize error (cond-> {:wrap :message} + options (merge options)))) (show? :schema) (update :schema edn/write-string opts) (show? :errors) (-> (me/with-error-messages opts) (update :errors (partial map #(update % :schema edn/write-string opts)))) diff --git a/test/cljc/reitit/ring_coercion_test.cljc b/test/cljc/reitit/ring_coercion_test.cljc index f74f65b9..162d5c59 100644 --- a/test/cljc/reitit/ring_coercion_test.cljc +++ b/test/cljc/reitit/ring_coercion_test.cljc @@ -583,7 +583,40 @@ :request-method :get}))] (is (= {:status 200, :body {:total "FOO: this, BAR: that"}} (call m/schema custom-meta-merge-checking-schema))) - (is (= {:status 200, :body {:total "FOO: this, BAR: that"}} (call identity custom-meta-merge-checking-parameters))))))) + (is (= {:status 200, :body {:total "FOO: this, BAR: that"}} (call identity custom-meta-merge-checking-parameters))))) + + (testing "malli options" + (let [->app (fn [options] + (ring/ring-handler + (ring/router + ["/api" {:get {:parameters {:body [:map + [:i :int] + [:x :string]]} + :handler (fn [{{:keys [body]} :parameters}] + {:status 200 :body body})}}] + {:data {:middleware [rrc/coerce-exceptions-middleware + rrc/coerce-request-middleware + rrc/coerce-response-middleware] + :coercion (malli/create options)}}))) + request {:uri "/api" + :request-method :get + :muuntaja/request {:format "application/json"}}] + (testing "humanize options" + (is (= {:i ["should be an integer"] :x ["missing required key"]} + (-> ((->app nil) (assoc request :body-params {:i "x"})) + :body + :humanized))) + (is (= {:i ["SHOULD INT"] :x ["MISSING"]} + (-> ((->app {:options {:errors {:int {:error/message {:en "SHOULD INT"}} + :malli.core/missing-key {:error/message {:en "MISSING"}}}}}) + (assoc request :body-params {:i "x"})) + :body + :humanized)))) + (testing "overriding registry" + (is (= {:body {:i "x" :x "x"} :status 200} + (-> ((->app {:options {:registry (merge (m/default-schemas) + {:int :string})}}) + (assoc request :body-params {:i "x" :x "x"})))))))))) #?(:clj (deftest per-content-type-test