2017-12-10 15:46:29 +00:00
# Clojure.spec Coercion
The [clojure.spec ](https://clojure.org/guides/spec ) library specifies the structure of data, validates or destructures it, and can generate data based on the spec.
2018-09-05 19:18:32 +00:00
## Warning
2018-10-21 17:06:28 +00:00
`clojure.spec` by itself doesn't support coercion. `reitit` uses [spec-tools ](https://github.com/metosin/spec-tools ) that adds coercion to spec. Like `clojure.spec` , it's alpha as it leans both on spec walking and `clojure.spec.alpha/conform` , which is concidered a spec internal, that might be changed or removed later.
2018-09-05 19:18:32 +00:00
2018-10-21 17:06:28 +00:00
## Usage
2018-09-05 19:18:32 +00:00
2018-10-21 17:06:28 +00:00
For simple specs (core predicates, `spec-tools.core/spec` , `s/and` , `s/or` , `s/coll-of` , `s/keys` , `s/map-of` , `s/nillable` and `s/every` ), the transformation is inferred using [spec-walker ](https://github.com/metosin/spec-tools#spec-walker ) and is automatic. To support all specs (like regex-specs), specs need to be wrapped into [Spec Records ](https://github.com/metosin/spec-tools/blob/master/README.md#spec-records ).
There are [CLJ-2116 ](https://dev.clojure.org/jira/browse/CLJ-2116 ) and [CLJ-2251 ](https://dev.clojure.org/jira/browse/CLJ-2251 ) that would help solve this elegantly. Go vote 'em up.
2018-09-05 19:18:32 +00:00
## Example
2017-12-10 15:46:29 +00:00
```clj
(require '[reitit.coercion.spec])
(require '[reitit.coercion :as coercion])
(require '[spec-tools.spec :as spec])
(require '[clojure.spec.alpha :as s])
(require '[reitit.core :as r])
2018-10-21 17:06:28 +00:00
;; simple specs, inferred
(s/def ::company string?)
(s/def ::user-id int?)
2017-12-10 15:46:29 +00:00
(s/def ::path-params (s/keys :req-un [::company ::user-id]))
(def router
(r/router
["/:company/users/:user-id" {:name ::user-view
:coercion reitit.coercion.spec/coercion
:parameters {:path ::path-params}}]
{:compile coercion/compile-request-coercers}))
2017-12-10 19:48:06 +00:00
(defn match-by-path-and-coerce! [path]
2017-12-10 15:46:29 +00:00
(if-let [match (r/match-by-path router path)]
(assoc match :parameters (coercion/coerce! match))))
```
Successful coercion:
```clj
2017-12-10 19:48:06 +00:00
(match-by-path-and-coerce! "/metosin/users/123")
2017-12-10 15:46:29 +00:00
; #Match {:template "/:company/users/:user-id",
; :data {:name :user/user-view,
2017-12-13 16:00:50 +00:00
; :coercion < < :spec > >
2017-12-10 15:46:29 +00:00
; :parameters {:path ::path-params}},
; :result {:path #object [reitit.coercion$request_coercer$]},
2018-02-01 14:23:44 +00:00
; :path-params {:company "metosin", :user-id "123"},
2017-12-10 15:46:29 +00:00
; :parameters {:path {:company "metosin", :user-id 123}}
; :path "/metosin/users/123"}
```
Failing coercion:
```clj
2017-12-10 19:48:06 +00:00
(match-by-path-and-coerce! "/metosin/users/ikitommi")
2017-12-10 15:46:29 +00:00
; => ExceptionInfo Request coercion failed...
```
2019-02-20 06:28:26 +00:00
2019-02-20 06:50:34 +00:00
## Deeply nested
2019-02-20 06:45:24 +00:00
2019-02-20 06:58:29 +00:00
Spec-tools allow deeply nested specs to be coerced. One can test the coercion easily in the REPL.
2019-02-20 06:45:24 +00:00
Define some specs:
2019-02-20 06:28:26 +00:00
```clj
(require '[clojure.spec.alpha :as s])
(require '[spec-tools.core :as st])
(s/def :sku/id keyword?)
(s/def ::sku (s/keys :req-un [:sku/id]))
(s/def ::skus (s/coll-of ::sku :into []))
(s/def :photo/id int?)
(s/def ::photo (s/keys :req-un [:photo/id]))
(s/def ::photos (s/coll-of ::photo :into []))
(s/def ::my-json-api (s/keys :req-un [::skus ::photos]))
2019-02-20 06:45:24 +00:00
```
2019-02-20 06:28:26 +00:00
2019-02-20 06:45:24 +00:00
Apply a string->edn coercion to the data:
```clj
2019-02-20 06:28:26 +00:00
(st/coerce
::my-json-api
{:skus [{:id "123"}]
:photos [{:id "123"}]}
st/string-transformer)
2019-02-20 06:45:24 +00:00
; {:skus [{:id :123}]
; :photos [{:id 123}]}
```
Apply a json->edn coercion to the data:
2019-02-20 06:28:26 +00:00
2019-02-20 06:45:24 +00:00
```clj
2019-02-20 06:28:26 +00:00
(st/coerce
::my-json-api
2019-02-20 06:58:29 +00:00
{:skus [{:id "123"}]
2019-02-20 06:28:26 +00:00
:photos [{:id "123"}]}
st/json-transformer)
2019-02-20 06:45:24 +00:00
; {: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"
2019-02-20 06:58:29 +00:00
:skus [{:id "123"
2019-02-20 06:45:24 +00:00
:INFOR "MATION"}]
:photos [{:id "123"
:HERE "TOO"}]}
rcs/json-transformer)
2019-02-20 06:58:29 +00:00
; {:skus [{:id :123}]
; :photos [{:id "123"}]}
2019-02-20 06:28:26 +00:00
```