mirror of
https://github.com/metosin/reitit.git
synced 2026-02-18 00:35:55 +00:00
Add test-doc-blocks
I suspected the malli coercion custom registry example was broken, so I wanted to get it tested and the coercion Malli options indeed had an extra layer of map.
This commit is contained in:
parent
7c1544c3ce
commit
0ca4fce984
3 changed files with 63 additions and 53 deletions
65
README.md
65
README.md
|
|
@ -65,9 +65,7 @@ modules will continue to be released under `metosin` for compatibility purposes.
|
|||
|
||||
All main modules bundled:
|
||||
|
||||
```clj
|
||||
[metosin/reitit "0.10.0"]
|
||||
```
|
||||
[](http://clojars.org/metosin/reitit)
|
||||
|
||||
Optionally, the parts can be required separately.
|
||||
|
||||
|
|
@ -83,21 +81,21 @@ Reitit is tested with the LTS releases Java 11, 17, 21 and 25
|
|||
(def router
|
||||
(r/router
|
||||
[["/api/ping" ::ping]
|
||||
["/api/orders/:id" ::order]]))
|
||||
["/api/orders/:id" ::order]]))
|
||||
|
||||
(r/match-by-path router "/api/ping")
|
||||
; #Match{:template "/api/ping"
|
||||
; :data {:name ::ping}
|
||||
; :result nil
|
||||
; :path-params {}
|
||||
; :path "/api/ping"}
|
||||
(into {} (r/match-by-path router "/api/ping"))
|
||||
;; => {:template "/api/ping"
|
||||
;; :data {:name ::ping}
|
||||
;; :result nil
|
||||
;; :path-params {}
|
||||
;; :path "/api/ping"}
|
||||
|
||||
(r/match-by-name router ::order {:id 2})
|
||||
; #Match{:template "/api/orders/:id",
|
||||
; :data {:name ::order},
|
||||
; :result nil,
|
||||
; :path-params {:id 2},
|
||||
; :path "/api/orders/2"}
|
||||
(into {} (r/match-by-name router ::order {:id 2}))
|
||||
;; => {:template "/api/orders/:id",
|
||||
;; :data {:name ::order},
|
||||
;; :result nil,
|
||||
;; :path-params {:id "2"},
|
||||
;; :path "/api/orders/2"}
|
||||
```
|
||||
|
||||
## Ring example
|
||||
|
|
@ -139,31 +137,32 @@ Valid request:
|
|||
:uri "/api/math"
|
||||
:query-params {:x "1", :y "2"}})
|
||||
(update :body slurp))
|
||||
; {:status 200
|
||||
; :body "{\"total\":3}"
|
||||
; :headers {"Content-Type" "application/json; charset=utf-8"}}
|
||||
;; => {:status 200
|
||||
;; :body "{\"total\":3}"
|
||||
;; :headers {"Content-Type" "application/json; charset=utf-8"}}
|
||||
```
|
||||
|
||||
Invalid request:
|
||||
|
||||
<!-- FIXME: Not asserted because each run gets a new generated spec name -->
|
||||
```clj
|
||||
(-> (app {:request-method :get
|
||||
:uri "/api/math"
|
||||
:query-params {:x "1", :y "a"}})
|
||||
(update :body jsonista.core/read-value))
|
||||
; {:status 400
|
||||
; :headers {"Content-Type" "application/json; charset=utf-8"}
|
||||
; :body {"spec" "(spec-tools.core/spec {:spec (clojure.spec.alpha/keys :req-un [:spec$8974/x :spec$8974/y]), :type :map, :leaf? false})"
|
||||
; "value" {"x" "1"
|
||||
; "y" "a"}
|
||||
; "problems" [{"via" ["spec$8974/y"]
|
||||
; "path" ["y"]
|
||||
; "pred" "clojure.core/int?"
|
||||
; "in" ["y"]
|
||||
; "val" "a"}]
|
||||
; "type" "reitit.coercion/request-coercion"
|
||||
; "coercion" "spec"
|
||||
; "in" ["request" "query-params"]}}
|
||||
;; {:status 400
|
||||
;; :headers {"Content-Type" "application/json; charset=utf-8"}
|
||||
;; :body {"spec" "(spec-tools.core/spec {:spec (clojure.spec.alpha/keys :req-un [:spec$8974/x :spec$8974/y]), :type :map, :leaf? false})"
|
||||
;; "value" {"x" "1"
|
||||
;; "y" "a"}
|
||||
;; "problems" [{"via" ["spec$8974/y"]
|
||||
;; "path" ["y"]
|
||||
;; "pred" "clojure.core/int?"
|
||||
;; "in" ["y"]
|
||||
;; "val" "a"}]
|
||||
;; "type" "reitit.coercion/request-coercion"
|
||||
;; "coercion" "spec"
|
||||
;; "in" ["request" "query-params"]}}
|
||||
```
|
||||
|
||||
## More examples
|
||||
|
|
@ -209,6 +208,6 @@ Roadmap is mostly written in [issues](https://github.com/metosin/reitit/issues).
|
|||
|
||||
## License
|
||||
|
||||
Copyright © 2017-2023 [Metosin Oy](http://www.metosin.fi)
|
||||
Copyright © 2017-2026 [Metosin Oy](http://www.metosin.fi)
|
||||
|
||||
Distributed under the Eclipse Public License, the same as Clojure.
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ By default, [Vector Syntax](https://github.com/metosin/malli#vector-syntax) is u
|
|||
|
||||
(def router
|
||||
(r/router
|
||||
["/:company/users/:user-id" {:name ::user-view
|
||||
["/:company/users/:user-id" {:name :user/user-view
|
||||
:coercion reitit.coercion.malli/coercion
|
||||
:parameters {:path [:map
|
||||
[:company string?]
|
||||
|
|
@ -28,24 +28,21 @@ By default, [Vector Syntax](https://github.com/metosin/malli#vector-syntax) is u
|
|||
Successful coercion:
|
||||
|
||||
```clj
|
||||
(match-by-path-and-coerce! "/metosin/users/123")
|
||||
; #Match{:template "/:company/users/:user-id",
|
||||
; :data {:name :user/user-view,
|
||||
; :coercion <<:malli>>
|
||||
; :parameters {:path [:map
|
||||
; [:company string?]
|
||||
; [:user-id int?]]}},
|
||||
; :result {:path #object[reitit.coercion$request_coercer$]},
|
||||
; :path-params {:company "metosin", :user-id "123"},
|
||||
; :parameters {:path {:company "metosin", :user-id 123}}
|
||||
; :path "/metosin/users/123"}
|
||||
(-> (into {} (match-by-path-and-coerce! "/metosin/users/123"))
|
||||
(dissoc :result :data))
|
||||
;; => {:template "/:company/users/:user-id",
|
||||
;; :path-params {:company "metosin", :user-id "123"},
|
||||
;; :parameters {:path {:company "metosin", :user-id 123}}
|
||||
;; :path "/metosin/users/123"}
|
||||
```
|
||||
|
||||
Failing coercion:
|
||||
|
||||
```clj
|
||||
(match-by-path-and-coerce! "/metosin/users/ikitommi")
|
||||
; => ExceptionInfo Request coercion failed...
|
||||
(try
|
||||
(match-by-path-and-coerce! "/metosin/users/ikitommi")
|
||||
(catch clojure.lang.ExceptionInfo e (.getMessage e)))
|
||||
;; => "Request coercion failed"
|
||||
```
|
||||
|
||||
## Lite Syntax
|
||||
|
|
@ -117,8 +114,14 @@ 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})}}})
|
||||
(def coercion
|
||||
(reitit.coercion.malli/create
|
||||
{:options
|
||||
{:registry (merge (m/default-schemas)
|
||||
{:my-type :int})}}))
|
||||
|
||||
(def coercer (reitit.coercion/-request-coercer coercion :string [:map [:x :my-type]]))
|
||||
|
||||
(coercer {:x "5"} :default)
|
||||
=> {:x 5}
|
||||
```
|
||||
|
|
|
|||
12
project.clj
12
project.clj
|
|
@ -156,7 +156,10 @@
|
|||
"-Dclojure.compiler.direct-linking=true"
|
||||
"-XX:+PrintCompilation"
|
||||
"-XX:+UnlockDiagnosticVMOptions"
|
||||
"-XX:+PrintInlining"]}}
|
||||
"-XX:+PrintInlining"]}
|
||||
|
||||
:gen-doc-tests {:test-paths ^:replace ["target/test-doc-blocks/test"]
|
||||
:dependencies [[com.github.lread/test-doc-blocks "1.2.21"]]}}
|
||||
:aliases {"all" ["with-profile" "dev,default"]
|
||||
"perf" ["with-profile" "default,dev,perf"]
|
||||
"test-clj" ["all" "do" ["bat-test"] ["check"]]
|
||||
|
|
@ -165,7 +168,12 @@
|
|||
;; the same way.
|
||||
"test-browser" ["doo" "chrome-headless" "test"]
|
||||
"test-advanced" ["doo" "chrome-headless" "advanced-test"]
|
||||
"test-node" ["doo" "node" "node-test"]}
|
||||
"test-node" ["doo" "node" "node-test"]
|
||||
|
||||
"test-docs" ["with-profile" "dev,gen-doc-tests" "do"
|
||||
["run" "-m" "lread.test-doc-blocks" "gen-tests" "--platform" "clj"
|
||||
"README.md" "doc/coercion/malli_coercion.md"]
|
||||
["test"]]}
|
||||
|
||||
:bat-test {:report [:pretty
|
||||
{:type :junit
|
||||
|
|
|
|||
Loading…
Reference in a new issue