I have a compojure app with a set of routes and handlers.
(defroutes app-routes
(GET "/stuff/:id" [:as request] (stuff/get-stuff request))
(POST "/stuff/" [:as request] (stuff/create-stuff request))
Each handler validates its input, like so
(defn create-stuff
[request]
(my-validation/validate-request
request
my-validation/create-stuff-validator
stuff-ok-fn))
The validation code is based on Metis, and looks like this:
(metis/defvalidator :create-stuff-validator
[:db :presence])
(defn validate-request
[request request-validator ok-function]
(let [validation-result (request-validator request)]
(if (empty? validation-result)
(ok-function request)
(bad-request validation-result))))
My problem is that code in create-stuff is duplicated across each of the route handlers; i.e the get-stuff function looks like the create-stuff handler. The only thing that differs is their validator function and their the-validation-went-well-function.
How can I abstract this duplication in a idiomatic Clojure manner?