reitit.spec/wrap spec-tools.spec/closed

This commit is contained in:
Tommi Reiman 2019-04-29 00:37:28 +03:00
parent c3de6ff3dd
commit a9bdceeeb6
6 changed files with 14 additions and 17 deletions

View file

@ -112,9 +112,9 @@
{:status 200 {:status 200
:body {:total (- x y)}})}}]]] :body {:total (- x y)}})}}]]]
{;;:reitit.interceptor/transform dev/print-context-diffs {;:reitit.interceptor/transform dev/print-context-diffs ;; pretty context diffs
:wrap-spec spell/closed-keys :validate spec/validate ;; enable spec validation for route data
:validate spec/validate :reitit.spec/wrap spell/closed ;; strict top-level validation (alpha)
:exception pretty/exception :exception pretty/exception
:data {:coercion spec-coercion/coercion :data {:coercion spec-coercion/coercion
:muuntaja m/instance :muuntaja m/instance

View file

@ -116,14 +116,14 @@
(defrecord Problem [path scope data spec problems]) (defrecord Problem [path scope data spec problems])
(defn validate-route-data [routes wrap-spec spec] (defn validate-route-data [routes wrap spec]
(some->> (for [[p d _] routes] (some->> (for [[p d _] routes]
(when-let [problems (and spec (s/explain-data (wrap-spec spec) d))] (when-let [problems (and spec (s/explain-data (wrap spec) d))]
(->Problem p nil d spec problems))) (->Problem p nil d spec problems)))
(keep identity) (seq) (vec))) (keep identity) (seq) (vec)))
(defn validate [routes {:keys [spec wrap-spec] :or {spec ::default-data, wrap-spec identity}}] (defn validate [routes {:keys [spec ::wrap] :or {spec ::default-data, wrap identity}}]
(when-let [problems (validate-route-data routes wrap-spec spec)] (when-let [problems (validate-route-data routes wrap spec)]
(exception/fail! (exception/fail!
::invalid-route-data ::invalid-route-data
{:problems problems}))) {:problems problems})))

View file

@ -230,9 +230,6 @@
:cljs "unknown")] :cljs "unknown")]
(ex-info (exception-str message source (printer)) (assoc (or data {}) ::exception/cause e)))) (ex-info (exception-str message source (printer)) (assoc (or data {}) ::exception/cause e))))
;; FIXME
(def closed-keys spec-tools.spell/closed-keys)
(defn de-expound-colors [^String s mappings] (defn de-expound-colors [^String s mappings]
(let [s' (reduce (let [s' (reduce
(fn [s [from to]] (fn [s [from to]]

View file

@ -19,8 +19,8 @@
;; ;;
(defn validate (defn validate
[routes {:keys [spec wrap-spec] :or {spec ::data, wrap-spec identity}}] [routes {:keys [spec ::rs/wrap] :or {spec ::data, wrap identity}}]
(when-let [problems (rrs/validate-route-data routes :interceptors wrap-spec spec)] (when-let [problems (rrs/validate-route-data routes :interceptors wrap spec)]
(exception/fail! (exception/fail!
::rs/invalid-route-data ::rs/invalid-route-data
{:problems problems}))) {:problems problems})))

View file

@ -35,21 +35,21 @@
:invalid non-specs})) :invalid non-specs}))
(s/merge-spec-impl (vec specs) (vec specs) nil)) (s/merge-spec-impl (vec specs) (vec specs) nil))
(defn validate-route-data [routes key wrap-spec spec] (defn validate-route-data [routes key wrap spec]
(->> (for [[p _ c] routes (->> (for [[p _ c] routes
[method {:keys [data] :as endpoint}] c [method {:keys [data] :as endpoint}] c
:when endpoint :when endpoint
:let [target (key endpoint) :let [target (key endpoint)
component-specs (seq (keep :spec target)) component-specs (seq (keep :spec target))
specs (keep identity (into [spec] component-specs)) specs (keep identity (into [spec] component-specs))
spec (wrap-spec (merge-specs specs))]] spec (wrap (merge-specs specs))]]
(when-let [problems (and spec (s/explain-data spec data))] (when-let [problems (and spec (s/explain-data spec data))]
(rs/->Problem p method data spec problems))) (rs/->Problem p method data spec problems)))
(keep identity) (seq))) (keep identity) (seq)))
(defn validate (defn validate
[routes {:keys [spec wrap-spec] :or {spec ::data, wrap-spec identity}}] [routes {:keys [spec ::rs/wrap] :or {spec ::data, wrap identity}}]
(when-let [problems (validate-route-data routes :middleware wrap-spec spec)] (when-let [problems (validate-route-data routes :middleware wrap spec)]
(exception/fail! (exception/fail!
::rs/invalid-route-data ::rs/invalid-route-data
{:problems problems}))) {:problems problems})))

View file

@ -27,7 +27,7 @@
[metosin/reitit-sieppari "0.3.1"] [metosin/reitit-sieppari "0.3.1"]
[metosin/reitit-pedestal "0.3.1"] [metosin/reitit-pedestal "0.3.1"]
[metosin/ring-swagger-ui "2.2.10"] [metosin/ring-swagger-ui "2.2.10"]
[metosin/spec-tools "0.9.1"] [metosin/spec-tools "0.9.2-SNAPSHOT"]
[metosin/schema-tools "0.11.0"] [metosin/schema-tools "0.11.0"]
[metosin/muuntaja "0.6.4"] [metosin/muuntaja "0.6.4"]
[metosin/jsonista "0.2.2"] [metosin/jsonista "0.2.2"]