mirror of
https://github.com/metosin/reitit.git
synced 2025-12-18 08:51:12 +00:00
Simplify Malli coercion for query-params to only encode
This commit is contained in:
parent
7ae118fbb5
commit
7e9116f77e
5 changed files with 23 additions and 38 deletions
|
|
@ -76,6 +76,20 @@
|
||||||
(assoc error :transformed transformed))))
|
(assoc error :transformed transformed))))
|
||||||
value))))))))
|
value))))))))
|
||||||
|
|
||||||
|
(defn- -query-string-coercer
|
||||||
|
"Create coercer for query-parameters, always allows extra params and does
|
||||||
|
encoding using string-transformer."
|
||||||
|
[schema transfomers options]
|
||||||
|
(let [;; Always allow extra paramaters on query-parameters encoding
|
||||||
|
open-schema (mu/open-schema schema)
|
||||||
|
;; Do not remove extra keys
|
||||||
|
string-transformer (-transformer string-transformer-provider (assoc options :strip-extra-keys false))
|
||||||
|
encoder (m/encoder open-schema options string-transformer)]
|
||||||
|
(fn [value format]
|
||||||
|
(if encoder
|
||||||
|
(encoder value)
|
||||||
|
value))))
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; public api
|
;; public api
|
||||||
;;
|
;;
|
||||||
|
|
@ -178,12 +192,6 @@
|
||||||
(-response-coercer [_ schema]
|
(-response-coercer [_ schema]
|
||||||
(-coercer schema :response transformers :encode opts))
|
(-coercer schema :response transformers :encode opts))
|
||||||
(-query-string-coercer [_ schema]
|
(-query-string-coercer [_ schema]
|
||||||
;; TODO: Create encoding function that only does encode, no decoding and validation?
|
(-query-string-coercer schema transformers opts))))))
|
||||||
(-coercer (mu/open-schema schema)
|
|
||||||
:string
|
|
||||||
;; Tune transformer to not strip extra keys
|
|
||||||
{:string {:default (-transformer string-transformer-provider (assoc opts :strip-extra-keys false))}}
|
|
||||||
:encode
|
|
||||||
opts))))))
|
|
||||||
|
|
||||||
(def coercion (create default-options))
|
(def coercion (create default-options))
|
||||||
|
|
|
||||||
|
|
@ -178,9 +178,7 @@
|
||||||
{:encode/string (fn [xs]
|
{:encode/string (fn [xs]
|
||||||
(str/join "," (map name xs)))
|
(str/join "," (map name xs)))
|
||||||
:decode/string (fn [s]
|
:decode/string (fn [s]
|
||||||
(if (string? s)
|
(mapv keyword (str/split s #",")))}
|
||||||
(mapv keyword (str/split s #","))
|
|
||||||
s))}
|
|
||||||
:keyword]]]}}]
|
:keyword]]]}}]
|
||||||
{:compile coercion/compile-request-coercers})
|
{:compile coercion/compile-request-coercers})
|
||||||
match (r/match-by-name! router ::route {:a "olipa", :b "kerran"})]
|
match (r/match-by-name! router ::route {:a "olipa", :b "kerran"})]
|
||||||
|
|
@ -211,12 +209,8 @@
|
||||||
[:x
|
[:x
|
||||||
[:vector
|
[:vector
|
||||||
[:keyword
|
[:keyword
|
||||||
{:decode/string (fn [s]
|
;; For query strings encode only calls encode, so no need to check if decode if value is encoded or not.
|
||||||
;; Encoding coercer calls decoder -> validate -> encoder
|
{:decode/string (fn [s] (keyword (subs s 2)))
|
||||||
;; Decoder doesn't need to do anything as in this case the value is already "decoded"
|
|
||||||
(if (string? s)
|
|
||||||
(keyword (subs s 2))
|
|
||||||
s))
|
|
||||||
:encode/string (fn [k] (str "__" (name k)))}]]]]}}]
|
:encode/string (fn [k] (str "__" (name k)))}]]]]}}]
|
||||||
{:compile coercion/compile-request-coercers})]
|
{:compile coercion/compile-request-coercers})]
|
||||||
(is (= "/olipa/kerran?x=__a&x=__b"
|
(is (= "/olipa/kerran?x=__a&x=__b"
|
||||||
|
|
|
||||||
|
|
@ -308,12 +308,7 @@
|
||||||
(is (= "foo?q=__x"
|
(is (= "foo?q=__x"
|
||||||
(rf/match->path {:data {:coercion rcm/coercion
|
(rf/match->path {:data {:coercion rcm/coercion
|
||||||
:parameters {:query [[:map
|
:parameters {:query [[:map
|
||||||
[:q {:decode/string (fn [s]
|
[:q {:decode/string (fn [s] (keyword (subs s 2)))
|
||||||
(if (string? s)
|
|
||||||
(keyword (if (str/starts-with? s "__")
|
|
||||||
(subs s 2)
|
|
||||||
s))
|
|
||||||
s))
|
|
||||||
:encode/string (fn [k] (str "__" (name k)))}
|
:encode/string (fn [k] (str "__" (name k)))}
|
||||||
:keyword]]]}}
|
:keyword]]]}}
|
||||||
:path "foo"}
|
:path "foo"}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
(ns reitit.frontend.easy-test
|
(ns reitit.frontend.easy-test
|
||||||
(:require [clojure.string :as str]
|
(:require [clojure.test :refer [are async deftest is testing]]
|
||||||
[clojure.test :refer [are async deftest is testing]]
|
|
||||||
[goog.events :as gevents]
|
[goog.events :as gevents]
|
||||||
[reitit.coercion.malli :as rcm]
|
[reitit.coercion.malli :as rcm]
|
||||||
[reitit.core :as r]
|
[reitit.core :as r]
|
||||||
|
|
@ -18,12 +17,7 @@
|
||||||
:parameters {:query [:map
|
:parameters {:query [:map
|
||||||
[:q {:optional true}
|
[:q {:optional true}
|
||||||
[:keyword
|
[:keyword
|
||||||
{:decode/string (fn [s]
|
{:decode/string (fn [s] (keyword (subs s 2)))
|
||||||
(if (string? s)
|
|
||||||
(keyword (if (str/starts-with? s "__")
|
|
||||||
(subs s 2)
|
|
||||||
s))
|
|
||||||
s))
|
|
||||||
:encode/string (fn [k] (str "__" (name k)))}]]]}}]]))
|
:encode/string (fn [k] (str "__" (name k)))}]]]}}]]))
|
||||||
|
|
||||||
;; TODO: Only tests fragment history, also test HTML5?
|
;; TODO: Only tests fragment history, also test HTML5?
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,7 @@
|
||||||
[reitit.frontend.history :as rfh]
|
[reitit.frontend.history :as rfh]
|
||||||
[reitit.frontend.test-utils :refer [capture-console]]
|
[reitit.frontend.test-utils :refer [capture-console]]
|
||||||
[goog.events :as gevents]
|
[goog.events :as gevents]
|
||||||
[reitit.coercion.malli :as rcm]
|
[reitit.coercion.malli :as rcm]))
|
||||||
[clojure.string :as str]))
|
|
||||||
|
|
||||||
(def browser (exists? js/window))
|
(def browser (exists? js/window))
|
||||||
|
|
||||||
|
|
@ -18,12 +17,7 @@
|
||||||
:parameters {:query [:map
|
:parameters {:query [:map
|
||||||
[:q {:optional true}
|
[:q {:optional true}
|
||||||
[:keyword
|
[:keyword
|
||||||
{:decode/string (fn [s]
|
{:decode/string (fn [s] (keyword (subs s 2)))
|
||||||
(if (string? s)
|
|
||||||
(keyword (if (str/starts-with? s "__")
|
|
||||||
(subs s 2)
|
|
||||||
s))
|
|
||||||
s))
|
|
||||||
:encode/string (fn [k] (str "__" (name k)))}]]]}}]]))
|
:encode/string (fn [k] (str "__" (name k)))}]]]}}]]))
|
||||||
|
|
||||||
(deftest fragment-history-test
|
(deftest fragment-history-test
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue