So I’m using Reitit with Schema on the backend, and I noticed that Reitit seems to treat query params as an ‘open’ schema (i.e. it permits all keys not specified in the schema). This is a bit surprising to me because by default schemas are closed (to make a schema open, you need to add an entry like Keyword Any
). Now, I’d really like the query parameters to be closed unless otherwise specified, so that any unexpected keys cause an error. How can I make this happen?
Okay, after digging around a bit in the Reitit code, I found that this behavior seems to be defined in https://github.com/metosin/reitit/blob/master/modules/reitit-core/src/reitit/coercion.cljc#L38. Following that backwards it seems like https://github.com/metosin/reitit/blob/master/modules/reitit-core/src/reitit/coercion.cljc#L73-L75 supports an optional :parameter-coercion
key that can be used to override this behavior..
So if I could get something like {:parameter-coercion (assoc reitit.coercion/default-parameter-coercion :query (reitit.coercion->ParameterCoercion :query-params :string true false))}
passed to this function then that should do the trick.
…and it seems like if I put this into the opts
key in the :compile
https://github.com/metosin/reitit/blob/master/modules/reitit-ring/src/reitit/ring/coercion.cljc#L27 in reitit.ring.coercion/coerce-request-middleware
then it should get passed down to reitit.coercion/request-coercer
. The main question remaining then, is how do I control what gets passed to the :compile
method? https://github.com/metosin/reitit/blob/master/doc/ring/data_driven_middleware.md https://github.com/metosin/reitit/blob/master/doc/ring/compiling_middleware.md don’t seem to explain this..
Alright, I figured it out 🙂 The following sentence in the https://github.com/metosin/reitit/blob/master/doc/ring/compiling_middleware.md docs made me realize I was supposed to put the custom :parameter/coercion
config in the config sent to reitit.ring/router
:
> :compile
expects a function of `route-data router-opts => ?IntoMiddleware`
Maybe the docs could be modified somehow to make this more clear? It’d also be helpful if there was an example illustrating the different ways to use middleware. Or maybe the docstring for the middleware should specify which parameters it supports?