Skip to content

Commit de0e1f4

Browse files
authored
Merge pull request #718 from metosin/doc-param-merge
doc: document nested parameter definitions
2 parents 34b6bb9 + 3fcd6cf commit de0e1f4

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

doc/basics/route_data.md

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ Resolved route tree:
8585
; :roles #{:db-admin}}]]
8686
```
8787

88+
See also [nested parameter definitions for coercions](../ring/coercion.md#nested-parameter-definitions)
89+
8890
## Route Data Fragments
8991

9092
Just like [fragments in React.js](https://reactjs.org/docs/fragments.html), we can create routing tree fragments by using empty path `""`. This allows us to add route data without accumulating to path.

doc/ring/coercion.md

+30
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,36 @@ Handlers can access the coerced parameters via the `:parameters` key in the requ
6363
:body {:total total}}))})
6464
```
6565

66+
67+
### Nested parameter definitions
68+
69+
Parameters are accumulated recursively along the route tree, just like
70+
other [route data](../basics/route_data.md). There is special case
71+
handling for merging eg. malli `:map` schemas.
72+
73+
```clj
74+
(def router
75+
(reitit.ring/router
76+
["/api" {:get {:parameters {:query [:map [:api-key :string]]}}}
77+
["/project/:project-id" {:get {:parameters {:path [:map [:project-id :int]]}}}
78+
["/task/:task-id" {:get {:parameters {:path [:map [:task-id :int]]
79+
:query [:map [:details :boolean]]}
80+
:handler (fn [req] (prn req))}}]]]
81+
{:data {:coercion reitit.coercion.malli/coercion}}))
82+
```
83+
84+
```clj
85+
(-> (r/match-by-path router "/api/project/1/task/2") :result :get :data :parameters)
86+
; {:query [:map
87+
; {:closed true}
88+
; [:api-key :string]
89+
; [:details :boolean]],
90+
; :path [:map
91+
; {:closed true}
92+
; [:project-id :int]
93+
; [:task-id :int]]}
94+
```
95+
6696
## Coercion Middleware
6797

6898
Defining a coercion for a route data doesn't do anything, as it's just data. We have to attach some code to apply the actual coercion. We can use the middleware from `reitit.ring.coercion`:

0 commit comments

Comments
 (0)