mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 00:11:11 +00:00
docs
This commit is contained in:
parent
716c845a05
commit
699a8c7cd3
4 changed files with 47 additions and 1 deletions
|
|
@ -1,6 +1,5 @@
|
|||
# reitit [](https://circleci.com/gh/metosin/reitit) [](https://cljdoc.xyz/jump/release/metosin/reitit) [](https://clojurians.slack.com/messages/reitit/)
|
||||
|
||||
|
||||
A fast data-driven router for Clojure(Script).
|
||||
|
||||
* Simple data-driven [route syntax](https://metosin.github.io/reitit/basics/route_syntax.html)
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ To enable parameter coercion, the following things need to be done:
|
|||
|
||||
Reitit ships with the following coercion modules:
|
||||
|
||||
* `reitit.coercion.malli/coercion` for [malli](https://github.com/metosin/malli)
|
||||
* `reitit.coercion.schema/coercion` for [plumatic schema](https://github.com/plumatic/schema)
|
||||
* `reitit.coercion.spec/coercion` for both [clojure.spec](https://clojure.org/about/spec) and [data-specs](https://github.com/metosin/spec-tools#data-specs)
|
||||
|
||||
|
|
|
|||
45
doc/coercion/malli_coercion.md
Normal file
45
doc/coercion/malli_coercion.md
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# Malli Coercion
|
||||
|
||||
[Malli](https://github.com/metosin/malli) is data-driven Schema library for Clojure/Script.
|
||||
|
||||
```clj
|
||||
(require '[reitit.coercion.malli])
|
||||
(require '[reitit.coercion :as coercion])
|
||||
(require '[reitit.core :as r])
|
||||
|
||||
(def router
|
||||
(r/router
|
||||
["/:company/users/:user-id" {:name ::user-view
|
||||
:coercion reitit.coercion.malli/coercion
|
||||
:parameters {:path [:map
|
||||
[:company string?]
|
||||
[:user-id int?]]}]
|
||||
{:compile coercion/compile-request-coercers}))
|
||||
|
||||
(defn match-by-path-and-coerce! [path]
|
||||
(if-let [match (r/match-by-path router path)]
|
||||
(assoc match :parameters (coercion/coerce! match))))
|
||||
```
|
||||
|
||||
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"}
|
||||
```
|
||||
|
||||
Failing coercion:
|
||||
|
||||
```clj
|
||||
(match-by-path-and-coerce! "/metosin/users/ikitommi")
|
||||
; => ExceptionInfo Request coercion failed...
|
||||
```
|
||||
|
|
@ -25,6 +25,7 @@ To enable coercion, the following things need to be done:
|
|||
|
||||
Reitit ships with the following coercion modules:
|
||||
|
||||
* `reitit.coercion.malli/coercion` for [malli](https://github.com/metosin/malli)
|
||||
* `reitit.coercion.schema/coercion` for [plumatic schema](https://github.com/plumatic/schema)
|
||||
* `reitit.coercion.spec/coercion` for both [clojure.spec](https://clojure.org/about/spec) and [data-specs](https://github.com/metosin/spec-tools#data-specs)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue