Fork me on GitHub
#reitit
<
2017-12-28
>
ikitommi09:12:23

spec-validation of route data just landed on master! I believe this is actually useful: https://metosin.github.io/reitit/basics/route_data_validation.html

ikitommi09:12:41

expound gives really nice errors.

ikitommi09:12:53

Always surprised that spec validates all fully-qualified keys from s/keys specs even if they are not defined in the s/keys. Despite being implicit, here it gives an easy way to extend the models. Just define you own qualified keys, enable spec validation and it works.

ikitommi09:12:05

to enforce that the key should be present, one can re-define the :spec for a router.

ikitommi09:12:04

next up: add support for Middleware/Interceptor :spec => they can define their own requirements for a route. Effective spec for a route(data) is s/merge of ’em all.

ikitommi20:12:30

Mounted middleware can now contribute to current route specs.

ikitommi20:12:40

(s/def ::role #{:admin :user})
(s/def ::roles (s/and (s/coll-of ::role :into #{}) set?))

(def auth-mw
  {:spec (s/keys :opt-un [::roles])
   :wrap (fn [handler]
           (fn [request]
             (handler request)))}]}})

(ring/router
  ["/api" {:get {:handler identity
                 :middleware [auth-mw]
                 :roles #{:adminz}}}]
  {:validate rrs/validate-spec!
   ::rs/explain e/expound-str})
; CompilerException clojure.lang.ExceptionInfo: Invalid route data:
; 
; -- On route -----------------------
; 
; "/api" :any
; 
; -- Spec failed --------------------
; 
; {:middleware ..., :handler ..., :roles #{:adminz}}
;                                          ^^^^^^^
; 
;     should be one of: `:admin`,`:user`