mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 00:11:11 +00:00
Works with ClojureScript!
This commit is contained in:
parent
132240b422
commit
becd30386d
4 changed files with 13 additions and 21 deletions
|
|
@ -160,7 +160,7 @@
|
|||
(handler request #(respond (coerce-response coercers request %)) raise)))))))}))
|
||||
|
||||
(def gen-wrap-coerce-exceptions
|
||||
"Middleare for coercion exception handling.
|
||||
"Middleware for handling coercion exceptions.
|
||||
Expects a :coercion of type `reitit.coercion.protocol/Coercion`
|
||||
and :parameters or :responses from route data, otherwise does not mount."
|
||||
(middleware/create
|
||||
|
|
@ -176,6 +176,6 @@
|
|||
(handle-coercion-exception e identity #(throw %)))))
|
||||
([request respond raise]
|
||||
(try
|
||||
(handler request respond (fn [e] (handle-coercion-exception e respond raise)))
|
||||
(handler request respond #(handle-coercion-exception % respond raise))
|
||||
(catch #?(:clj Exception :cljs js/Error) e
|
||||
(handle-coercion-exception e respond raise))))))))}))
|
||||
|
|
|
|||
|
|
@ -6,9 +6,7 @@
|
|||
[schema-tools.coerce :as stc]
|
||||
[spec-tools.swagger.core :as swagger]
|
||||
[clojure.walk :as walk]
|
||||
[reitit.ring.coercion.protocol :as protocol])
|
||||
(:import (schema.core OptionalKey RequiredKey)
|
||||
(schema.utils ValidationError NamedError)))
|
||||
[reitit.ring.coercion.protocol :as protocol]))
|
||||
|
||||
(def string-coercion-matcher
|
||||
stc/string-coercion-matcher)
|
||||
|
|
@ -26,12 +24,12 @@
|
|||
(walk/prewalk
|
||||
(fn [x]
|
||||
(cond
|
||||
(class? x) (.getName ^Class x)
|
||||
(instance? OptionalKey x) (pr-str (list 'opt (:k x)))
|
||||
(instance? RequiredKey x) (pr-str (list 'req (:k x)))
|
||||
(and (satisfies? s/Schema x) (record? x)) (try (pr-str (s/explain x)) (catch Exception _ x))
|
||||
(instance? ValidationError x) (str (su/validation-error-explain x))
|
||||
(instance? NamedError x) (str (su/named-error-explain x))
|
||||
#?@(:clj [(class? x) (.getName ^Class x)])
|
||||
(instance? schema.core.OptionalKey x) (pr-str (list 'opt (:k x)))
|
||||
(instance? schema.core.RequiredKey x) (pr-str (list 'req (:k x)))
|
||||
(and (satisfies? s/Schema x) (record? x)) (try (pr-str (s/explain x)) (catch #?(:clj Exception :cljs js/Error) _ x))
|
||||
(instance? schema.utils.ValidationError x) (str (su/validation-error-explain x))
|
||||
(instance? schema.utils.NamedError x) (str (su/named-error-explain x))
|
||||
:else x))
|
||||
schema))
|
||||
|
||||
|
|
@ -45,12 +43,8 @@
|
|||
|
||||
(get-apidocs [_ _ {:keys [parameters responses] :as info}]
|
||||
(cond-> (dissoc info :parameters :responses)
|
||||
parameters (assoc
|
||||
::swagger/parameters
|
||||
parameters)
|
||||
responses (assoc
|
||||
::swagger/responses
|
||||
responses)))
|
||||
parameters (assoc ::swagger/parameters parameters)
|
||||
responses (assoc ::swagger/responses responses)))
|
||||
|
||||
(make-open [_ schema] (st/open-schema schema))
|
||||
|
||||
|
|
|
|||
|
|
@ -51,9 +51,7 @@
|
|||
(memoize #(into-spec %1 (gensym "spec"))))
|
||||
|
||||
(defn stringify-pred [pred]
|
||||
(str (if (instance? clojure.lang.LazySeq pred)
|
||||
(seq pred)
|
||||
pred)))
|
||||
(str (if (seq? pred) (seq pred) pred)))
|
||||
|
||||
(defmulti coerce-response? identity :default ::default)
|
||||
(defmethod coerce-response? ::default [_] true)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
(ns reitit.coercion-test
|
||||
(:require [clojure.test :refer [deftest testing is]]
|
||||
[schema.core :as s]
|
||||
[reitit.ring :as ring]
|
||||
[reitit.ring.coercion :as coercion]
|
||||
[reitit.ring.coercion.spec :as spec]
|
||||
[schema.core :as s]
|
||||
[reitit.ring.coercion.schema :as schema])
|
||||
#?(:clj
|
||||
(:import (clojure.lang ExceptionInfo))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue