ring-swagger

ring-swagger & compojure-api
danielcompton 2018-05-10T03:09:23.000187Z

Is there a way to use compojure-api and vanilla specs for validation, and not coercion?

ikitommi 2018-05-10T06:15:35.000196Z

@danielcompton yes, you need to define a custom Coercion for that, with default-conforming set for all coercion scopes (body, string, response). Something like:

(require '[compojure.api.coercion.spec :as cs])
(require '[ring.util.http-response :as r])
(require '[compojure.api.sweet :as c])
(require '[clojure.spec.alpha :as s])

(def spec-validation
  (cs/create-coercion
    {:body {:default cs/default-conforming}
     :string {:default cs/default-conforming}
     :response {:default cs/default-conforming}}))

(s/def ::x int?)

(def app
  (c/POST "/echo" []
    :coercion spec-validation
    :body [body (s/keys :req-un [::x])]
    (r/ok body)))

(app {:request-method :post, :uri "/echo" :body-params {:x 1, :y 1}})
; {:status 200, :headers {}, :body {:x 1, :y 1}}

(app {:request-method :post, :uri "/echo" :body-params {:x "1"}})
; CompilerException clojure.lang.ExceptionInfo: Request validation failed ...

ikitommi 2018-05-10T06:16:11.000252Z

default-conforming = no-op = just validation.

danielcompton 2018-05-10T06:46:40.000261Z

Great, thanks!

kasuko 2018-05-10T16:44:36.000529Z

Oh whoops, I meant query parameter but I can modify this. Thanks for the help!