This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-05-10
Channels
- # beginners (35)
- # cider (165)
- # cljsrn (18)
- # clojars (1)
- # clojure (141)
- # clojure-greece (2)
- # clojure-italy (11)
- # clojure-nl (1)
- # clojure-spec (21)
- # clojure-uk (89)
- # clojurescript (56)
- # community-development (3)
- # cursive (3)
- # data-science (55)
- # datomic (13)
- # emacs (12)
- # fulcro (31)
- # graphql (6)
- # jobs-discuss (35)
- # lein-figwheel (10)
- # mount (2)
- # off-topic (3)
- # onyx (22)
- # parinfer (4)
- # portkey (7)
- # re-frame (29)
- # ring-swagger (4)
- # shadow-cljs (37)
- # specter (9)
- # sql (30)
- # tools-deps (15)
- # vim (2)
- # yada (17)
Is there a way to use compojure-api and vanilla specs for validation, and not coercion?
@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 ...
Great, thanks!