mirror of
https://github.com/metosin/reitit.git
synced 2025-12-18 17:01:11 +00:00
Even better docs!
This commit is contained in:
parent
319c768b35
commit
50ae7b3bc0
1 changed files with 35 additions and 3 deletions
|
|
@ -59,7 +59,12 @@ Failing coercion:
|
||||||
; => ExceptionInfo Request coercion failed...
|
; => ExceptionInfo Request coercion failed...
|
||||||
```
|
```
|
||||||
|
|
||||||
## Deeply nested specs
|
## Running coercions from REPL
|
||||||
|
|
||||||
|
Simplest way to test spec coercion is to run them directly in the REPL.
|
||||||
|
|
||||||
|
|
||||||
|
Define some specs:
|
||||||
|
|
||||||
```clj
|
```clj
|
||||||
(require '[clojure.spec.alpha :as s])
|
(require '[clojure.spec.alpha :as s])
|
||||||
|
|
@ -74,18 +79,45 @@ Failing coercion:
|
||||||
(s/def ::photos (s/coll-of ::photo :into []))
|
(s/def ::photos (s/coll-of ::photo :into []))
|
||||||
|
|
||||||
(s/def ::my-json-api (s/keys :req-un [::skus ::photos]))
|
(s/def ::my-json-api (s/keys :req-un [::skus ::photos]))
|
||||||
|
```
|
||||||
|
|
||||||
|
Apply a string->edn coercion to the data:
|
||||||
|
|
||||||
|
```clj
|
||||||
(st/coerce
|
(st/coerce
|
||||||
::my-json-api
|
::my-json-api
|
||||||
{:skus [{:id "123"}]
|
{:skus [{:id "123"}]
|
||||||
:photos [{:id "123"}]}
|
:photos [{:id "123"}]}
|
||||||
st/string-transformer)
|
st/string-transformer)
|
||||||
; {:skus [{:id :123}], :photos [{:id 123}]}
|
; {:skus [{:id :123}]
|
||||||
|
; :photos [{:id 123}]}
|
||||||
|
```
|
||||||
|
|
||||||
|
Apply a json->edn coercion to the data:
|
||||||
|
|
||||||
|
```clj
|
||||||
(st/coerce
|
(st/coerce
|
||||||
::my-json-api
|
::my-json-api
|
||||||
{:skus [{:id :123}]
|
{:skus [{:id :123}]
|
||||||
:photos [{:id "123"}]}
|
:photos [{:id "123"}]}
|
||||||
st/json-transformer)
|
st/json-transformer)
|
||||||
; {:skus [{:id :123}], :photos [{:id "123"}]}
|
; {:skus [{:id :123}]
|
||||||
|
; :photos [{:id "123"}]}
|
||||||
|
```
|
||||||
|
|
||||||
|
By default, reitit uses custom transformers that also strip out extra keys from `s/keys` specs:
|
||||||
|
|
||||||
|
```clj
|
||||||
|
(require '[reitit.coercion.spec :as rcs])
|
||||||
|
|
||||||
|
(st/coerce
|
||||||
|
::my-json-api
|
||||||
|
{:TOO "MUCH"
|
||||||
|
:skus [{:id :123
|
||||||
|
:INFOR "MATION"}]
|
||||||
|
:photos [{:id "123"
|
||||||
|
:HERE "TOO"}]}
|
||||||
|
rcs/json-transformer)
|
||||||
|
; {:photos [{:id "123"}]
|
||||||
|
; :skus [{:id :123}]}
|
||||||
```
|
```
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue