/
validator.clj
29 lines (26 loc) · 1.23 KB
/
validator.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
(ns compojure.api.validator
(:require [compojure.api.swagger :as swagger]
[compojure.api.impl.json :as json]
[ring.swagger.validator :as rsv]
[muuntaja.core :as m]
[compojure.api.middleware :as mw]))
(defn validate
"Validates an api. If the api is Swagger-enabled, the swagger-spec
is requested and validated against the JSON Schema. Returns either
the (valid) api or throws an exception. Requires lazily the
ring.swagger.validator -namespace allowing it to be excluded, #227"
[api]
(when-let [uri (swagger/swagger-spec-path api)]
(let [{status :status :as response} (api {:request-method :get
:uri uri
::mw/rethrow-exceptions? true})
body (->> response :body (m/decode json/muuntaja "application/json"))]
(when-not (= status 200)
(throw (ex-info (str "Coudn't read swagger spec from " uri)
{:status status
:body body})))
(when-let [errors (seq (rsv/validate body))]
(throw (ex-info (str "Invalid swagger spec from " uri)
{:errors errors
:body body})))))
api)