Skip to content

Commit aa695d2

Browse files
committed
Move validator to own namespace
1 parent 5c7a0e2 commit aa695d2

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

src/compojure/api/swagger.clj

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
[ring.swagger.core :as swagger]
99
[ring.swagger.ui :as rsui]
1010
[ring.swagger.swagger2 :as swagger2]
11-
[compojure.api.routes :as routes]
12-
[cheshire.core :as cheshire]))
11+
[compojure.api.routes :as routes]))
1312

1413
(defn base-path [request]
1514
(let [context (swagger/context request)]
@@ -95,28 +94,3 @@
9594
(c/routes
9695
(if ui (apply swagger-ui ui (mapcat identity (merge (if spec {:swagger-docs (apply str (remove clojure.string/blank? [(:basePath data) spec]))}) ui-options))))
9796
(if spec (apply swagger-docs spec (mapcat identity data))))))))
98-
99-
(defn validate
100-
"Validates a api. If the api is Swagger-enabled, the swagger-spec
101-
is requested and validated against the JSON Schema. Returns either
102-
the (valid) api or throws an exception. Requires lazily the
103-
ring.swagger.validator -namespace allowing it to be excluded, #227"
104-
[api]
105-
(require 'ring.swagger.validator)
106-
(when-let [uri (swagger-spec-path api)]
107-
(let [validate (resolve 'ring.swagger.validator/validate)
108-
{status :status :as response} (api {:request-method :get
109-
:uri uri
110-
mw/rethrow-exceptions? true})
111-
body (-> response :body slurp (cheshire/parse-string true))]
112-
113-
(when-not (= status 200)
114-
(throw (ex-info (str "Coudn't read swagger spec from " uri)
115-
{:status status
116-
:body body})))
117-
118-
(when-let [errors (seq (validate body))]
119-
(throw (ex-info (str "Invalid swagger spec from " uri)
120-
{:errors errors
121-
:body body})))))
122-
api)

src/compojure/api/validator.clj

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(ns compojure.api.validator
2+
(:require [compojure.api.swagger :as swagger]
3+
[cheshire.core :as cheshire]
4+
[ring.swagger.validator :as rsv]
5+
[compojure.api.middleware :as mw]))
6+
7+
(defn validate
8+
"Validates a api. If the api is Swagger-enabled, the swagger-spec
9+
is requested and validated against the JSON Schema. Returns either
10+
the (valid) api or throws an exception. Requires lazily the
11+
ring.swagger.validator -namespace allowing it to be excluded, #227"
12+
[api]
13+
(when-let [uri (swagger/swagger-spec-path api)]
14+
(let [{status :status :as response} (api {:request-method :get
15+
:uri uri
16+
mw/rethrow-exceptions? true})
17+
body (-> response :body slurp (cheshire/parse-string true))]
18+
19+
(when-not (= status 200)
20+
(throw (ex-info (str "Coudn't read swagger spec from " uri)
21+
{:status status
22+
:body body})))
23+
24+
(when-let [errors (seq (rsv/validate body))]
25+
(throw (ex-info (str "Invalid swagger spec from " uri)
26+
{:errors errors
27+
:body body})))))
28+
api)

0 commit comments

Comments
 (0)