2017-12-03 15:43:57 +00:00
|
|
|
(ns example.spec
|
|
|
|
|
(:require [clojure.spec.alpha :as s]
|
|
|
|
|
[spec-tools.spec :as spec]
|
2017-12-09 20:49:32 +00:00
|
|
|
[reitit.ring.coercion-middleware :as coercion]
|
2017-12-03 15:43:57 +00:00
|
|
|
[reitit.ring.coercion.spec :as spec-coercion]
|
|
|
|
|
[example.server :as server]))
|
|
|
|
|
|
|
|
|
|
;; wrap into Spec Records to enable runtime conforming
|
|
|
|
|
(s/def ::x spec/int?)
|
|
|
|
|
(s/def ::y spec/int?)
|
|
|
|
|
(s/def ::request (s/keys :req-un [::x ::y]))
|
|
|
|
|
|
|
|
|
|
;; read coerced parameters under :parameters
|
|
|
|
|
(defn handler [{{{:keys [x y]} :query} :parameters}]
|
|
|
|
|
{:status 200
|
|
|
|
|
:body {:result (+ x y)
|
|
|
|
|
:source :spec}})
|
|
|
|
|
|
|
|
|
|
(def app
|
|
|
|
|
(-> #'handler
|
|
|
|
|
(server/wrap-coercion
|
|
|
|
|
{:parameters {:query ::request}
|
|
|
|
|
:coercion spec-coercion/coercion})))
|