|
5 | 5 | [compojure.api.coercion.core :as cc] |
6 | 6 | [clojure.walk :as walk] |
7 | 7 | [schema.core :as s] |
8 | | - [compojure.api.common :as common] |
9 | | - ;; side effects |
10 | | - compojure.api.coercion.register-schema) |
| 8 | + [compojure.api.common :as common]) |
11 | 9 | (:import (java.io File) |
12 | 10 | (schema.core OptionalKey RequiredKey) |
13 | 11 | (schema.utils ValidationError NamedError))) |
|
34 | 32 | (common/fifo-memoize sc/coercer 1000)) |
35 | 33 |
|
36 | 34 | ;; don't use coercion for certain types |
37 | | -(defmulti coerce-response? identity :default ::default) |
| 35 | +(defmulti coerce-response? #(if (or (class? %) |
| 36 | + (keyword? %)) |
| 37 | + % |
| 38 | + ::default) |
| 39 | + :default ::default) |
38 | 40 | (defmethod coerce-response? ::default [_] true) |
39 | 41 | (defmethod coerce-response? File [_] false) |
40 | 42 |
|
|
55 | 57 | (update :errors stringify))) |
56 | 58 |
|
57 | 59 | (coerce-request [_ schema value type format request] |
58 | | - (let [type-options (options type)] |
59 | | - (if-let [matcher (or (get (get type-options :formats) format) |
60 | | - (get type-options :default))] |
| 60 | + (let [legacy? (fn? options) |
| 61 | + type-options (if legacy? |
| 62 | + (when-let [provider (options request)] |
| 63 | + (provider type)) |
| 64 | + (options type))] |
| 65 | + (if-let [matcher (if legacy? |
| 66 | + type-options |
| 67 | + (or (get (get type-options :formats) format) |
| 68 | + (get type-options :default)))] |
61 | 69 | (let [coerce (memoized-coercer schema matcher) |
62 | 70 | coerced (coerce value)] |
63 | 71 | (if (su/error? coerced) |
|
83 | 91 | :response {:default (constantly nil)}}) |
84 | 92 |
|
85 | 93 | (defn create-coercion [options] |
| 94 | + {:pre [(or (map? options) |
| 95 | + (fn? options))]} |
86 | 96 | (->SchemaCoercion :schema options)) |
87 | 97 |
|
88 | 98 | (def default-coercion (create-coercion default-options)) |
| 99 | + |
| 100 | +(defmethod cc/named-coercion :schema [_] default-coercion) |
0 commit comments