reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
wombawomba 2020-07-18T21:07:17.306Z

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?

wombawomba 2020-07-18T21:29:26.306100Z

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..

wombawomba 2020-07-18T21:30:15.306600Z

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.

wombawomba 2020-07-18T21:36:26.307Z

…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..

wombawomba 2020-07-18T21:42:34.307200Z

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`

wombawomba 2020-07-18T21:43:13.307600Z

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?

👍 1