gregspurrier / checked-route

Type-checked Compojure Routes
MIT License
9 stars 0 forks source link

checked-route

checked-route allows you to check--e.g., against a schema defined with clj-schema--Compojure route parameters before handing them over to the route's body. Any check failures result in a 400 error being returned to the caller.

For example, consider this Compojure route that expects two parameters, bar and baz:

(POST "/foo" [bar baz] (do-something bar baz))

If bar and baz have check functions check-bar and check-baz, respectively, that return nil on success and error message(s) on failure, then you can use a checked route to enforce them:

(require '[checked-route.route :as checked])
(checked/POST "/foo" [^{:check check-bar} bar ^{:check check-baz} baz]
  (do-something bar baz))

The checked-route.route namespace provides checked versions of Compojure's route macros (GET, PUT, etc.). If no metadata is associated with a checked route's arguments, then the route will have the same behavior as its unchecked equivalent. Adding metadata tags, such as :check, to an argument enables checking of that argument. See the "Recognized Metadata" section below for the supported tags.

Installation via Leiningen

To use checked-route in your project, add the following to the :dependencies section of your project.clj file:

[checked-route "0.1.1"]

Recognized Metadata

The following fields in an argument's metadata affect the checking process:

If pre- or post-check transform functions are provided, the argument's binding for the body of the route will reflect these transformations.

License

Copyright (c) 2012 Greg Spurrier

Decomatic is distributed under the MIT license. Please see LICENSE.txt for the details.