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:
Juho Teperi 2026-02-09 14:18:14 +02:00
parent 7c1544c3ce
commit 0ca4fce984
3 changed files with 63 additions and 53 deletions

View file

@ -65,9 +65,7 @@ modules will continue to be released under `metosin` for compatibility purposes.
All main modules bundled: All main modules bundled:
```clj [![Clojars Project](http://clojars.org/metosin/reitit/latest-version.svg)](http://clojars.org/metosin/reitit)
[metosin/reitit "0.10.0"]
```
Optionally, the parts can be required separately. 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 (def router
(r/router (r/router
[["/api/ping" ::ping] [["/api/ping" ::ping]
    ["/api/orders/:id" ::order]])) ["/api/orders/:id" ::order]]))
(r/match-by-path router "/api/ping") (into {} (r/match-by-path router "/api/ping"))
; #Match{:template "/api/ping" ;; => {:template "/api/ping"
; :data {:name ::ping} ;; :data {:name ::ping}
; :result nil ;; :result nil
; :path-params {} ;; :path-params {}
; :path "/api/ping"} ;; :path "/api/ping"}
(r/match-by-name router ::order {:id 2}) (into {} (r/match-by-name router ::order {:id 2}))
; #Match{:template "/api/orders/:id", ;; => {:template "/api/orders/:id",
;       :data {:name ::order}, ;; :data {:name ::order},
; :result nil, ;; :result nil,
; :path-params {:id 2}, ;; :path-params {:id "2"},
; :path "/api/orders/2"} ;; :path "/api/orders/2"}
``` ```
## Ring example ## Ring example
@ -139,31 +137,32 @@ Valid request:
:uri "/api/math" :uri "/api/math"
:query-params {:x "1", :y "2"}}) :query-params {:x "1", :y "2"}})
(update :body slurp)) (update :body slurp))
; {:status 200 ;; => {:status 200
; :body "{\"total\":3}" ;; :body "{\"total\":3}"
; :headers {"Content-Type" "application/json; charset=utf-8"}} ;; :headers {"Content-Type" "application/json; charset=utf-8"}}
``` ```
Invalid request: Invalid request:
<!-- FIXME: Not asserted because each run gets a new generated spec name -->
```clj ```clj
(-> (app {:request-method :get (-> (app {:request-method :get
:uri "/api/math" :uri "/api/math"
:query-params {:x "1", :y "a"}}) :query-params {:x "1", :y "a"}})
(update :body jsonista.core/read-value)) (update :body jsonista.core/read-value))
; {:status 400 ;; {:status 400
; :headers {"Content-Type" "application/json; charset=utf-8"} ;; :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})" ;; :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" ;; "value" {"x" "1"
; "y" "a"} ;; "y" "a"}
; "problems" [{"via" ["spec$8974/y"] ;; "problems" [{"via" ["spec$8974/y"]
; "path" ["y"] ;; "path" ["y"]
; "pred" "clojure.core/int?" ;; "pred" "clojure.core/int?"
; "in" ["y"] ;; "in" ["y"]
; "val" "a"}] ;; "val" "a"}]
; "type" "reitit.coercion/request-coercion" ;; "type" "reitit.coercion/request-coercion"
; "coercion" "spec" ;; "coercion" "spec"
; "in" ["request" "query-params"]}} ;; "in" ["request" "query-params"]}}
``` ```
## More examples ## More examples
@ -209,6 +208,6 @@ Roadmap is mostly written in [issues](https://github.com/metosin/reitit/issues).
## License ## 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. Distributed under the Eclipse Public License, the same as Clojure.

View file

@ -13,7 +13,7 @@ By default, [Vector Syntax](https://github.com/metosin/malli#vector-syntax) is u
(def router (def router
(r/router (r/router
["/:company/users/:user-id" {:name ::user-view ["/:company/users/:user-id" {:name :user/user-view
:coercion reitit.coercion.malli/coercion :coercion reitit.coercion.malli/coercion
:parameters {:path [:map :parameters {:path [:map
[:company string?] [:company string?]
@ -28,24 +28,21 @@ By default, [Vector Syntax](https://github.com/metosin/malli#vector-syntax) is u
Successful coercion: Successful coercion:
```clj ```clj
(match-by-path-and-coerce! "/metosin/users/123") (-> (into {} (match-by-path-and-coerce! "/metosin/users/123"))
; #Match{:template "/:company/users/:user-id", (dissoc :result :data))
; :data {:name :user/user-view, ;; => {:template "/:company/users/:user-id",
; :coercion <<:malli>> ;; :path-params {:company "metosin", :user-id "123"},
; :parameters {:path [:map ;; :parameters {:path {:company "metosin", :user-id 123}}
; [:company string?] ;; :path "/metosin/users/123"}
; [: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"}
``` ```
Failing coercion: Failing coercion:
```clj ```clj
(match-by-path-and-coerce! "/metosin/users/ikitommi") (try
; => ExceptionInfo Request coercion failed... (match-by-path-and-coerce! "/metosin/users/ikitommi")
(catch clojure.lang.ExceptionInfo e (.getMessage e)))
;; => "Request coercion failed"
``` ```
## Lite Syntax ## Lite Syntax
@ -117,8 +114,14 @@ Malli registry can be configured conveniently via `:options :registry`:
```clj ```clj
(require '[malli.core :as m]) (require '[malli.core :as m])
(reitit.coercion.malli/create (def coercion
(reitit.coercion.malli/create
{:options {:options
{:registry {:registry (merge (m/default-schemas) {:registry (merge (m/default-schemas)
{:my-type :string})}}}) {:my-type :int})}}))
(def coercer (reitit.coercion/-request-coercer coercion :string [:map [:x :my-type]]))
(coercer {:x "5"} :default)
=> {:x 5}
``` ```

View file

@ -156,7 +156,10 @@
"-Dclojure.compiler.direct-linking=true" "-Dclojure.compiler.direct-linking=true"
"-XX:+PrintCompilation" "-XX:+PrintCompilation"
"-XX:+UnlockDiagnosticVMOptions" "-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"] :aliases {"all" ["with-profile" "dev,default"]
"perf" ["with-profile" "default,dev,perf"] "perf" ["with-profile" "default,dev,perf"]
"test-clj" ["all" "do" ["bat-test"] ["check"]] "test-clj" ["all" "do" ["bat-test"] ["check"]]
@ -165,7 +168,12 @@
;; the same way. ;; the same way.
"test-browser" ["doo" "chrome-headless" "test"] "test-browser" ["doo" "chrome-headless" "test"]
"test-advanced" ["doo" "chrome-headless" "advanced-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 :bat-test {:report [:pretty
{:type :junit {:type :junit