Skip to content

Commit

Permalink
doc: document nested parameter definitions
Browse files Browse the repository at this point in the history
originally implemented in #626 for #422
  • Loading branch information
opqdonut committed Jan 31, 2025
1 parent a19b603 commit 3fcd6cf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/basics/route_data.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ Resolved route tree:
; :roles #{:db-admin}}]]
```

See also [nested parameter definitions for coercions](../ring/coercion.md#nested-parameter-definitions)

## Route Data Fragments

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.
Expand Down
30 changes: 30 additions & 0 deletions doc/ring/coercion.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,36 @@ Handlers can access the coerced parameters via the `:parameters` key in the requ
:body {:total total}}))})
```


### Nested parameter definitions

Parameters are accumulated recursively along the route tree, just like
other [route data](../basics/route_data.md). There is special case
handling for merging eg. malli `:map` schemas.

```clj
(def router
(reitit.ring/router
["/api" {:get {:parameters {:query [:map [:api-key :string]]}}}
["/project/:project-id" {:get {:parameters {:path [:map [:project-id :int]]}}}
["/task/:task-id" {:get {:parameters {:path [:map [:task-id :int]]
:query [:map [:details :boolean]]}
:handler (fn [req] (prn req))}}]]]
{:data {:coercion reitit.coercion.malli/coercion}}))
```

```clj
(-> (r/match-by-path router "/api/project/1/task/2") :result :get :data :parameters)
; {:query [:map
; {:closed true}
; [:api-key :string]
; [:details :boolean]],
; :path [:map
; {:closed true}
; [:project-id :int]
; [:task-id :int]]}
```

## Coercion Middleware

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`:
Expand Down

0 comments on commit 3fcd6cf

Please sign in to comment.