reitit/examples/ring-example/src/example/spec.clj
2018-08-27 09:12:09 +03:00

23 lines
977 B
Clojure

(ns example.spec
(:require [clojure.spec.alpha :as s]
[spec-tools.spec :as spec]
[reitit.coercion.spec]))
;; wrap into Spec Records to enable runtime conforming
(s/def ::x spec/int?)
(s/def ::y spec/int?)
(s/def ::total spec/int?)
(def routes
["/spec" {:coercion reitit.coercion.spec/coercion}
["/plus" {:responses {200 {:body (s/keys :req-un [::total])}}
:get {:summary "plus with query-params"
:parameters {:query (s/keys :req-un [::x ::y])}
:handler (fn [{{{:keys [x y]} :query} :parameters}]
{:status 200
:body {:total (+ x y)}})}
:post {:summary "plus with body-params"
:parameters {:body (s/keys :req-un [::x ::y])}
:handler (fn [{{{:keys [x y]} :body} :parameters}]
{:status 200
:body {:total (+ x y)}})}}]])