Skip to content

Commit

Permalink
Validate all known, validatable methods by default in liberator-mixin…
Browse files Browse the repository at this point in the history
….validation.
  • Loading branch information
tobyclemson committed Sep 17, 2019
1 parent 9085d7c commit c3b928b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,9 @@ and this project adheres to


## [Unreleased]
### Changed
- Validate all known, validatable methods by default in
`liberator-mixin.validation`.

## [0.0.28] — 2019-09-16
### Changed
Expand Down
4 changes: 2 additions & 2 deletions src/liberator_mixin/validation/core.clj
Expand Up @@ -15,14 +15,14 @@

(defn with-validation []
{:validate-methods
[:put :post]
(fn [{:keys [resource]}] ((:known-methods resource)))

:processable?
(fn [{:keys [resource request] :as context}]
(if-let [new-validator (get resource :validator)]
(let [validate-methods (get resource :validate-methods)
request-method (get request :request-method)]
(if (some #(= request-method %) (validate-methods))
(if (some #(= request-method %) (validate-methods context))
(valid? (new-validator context) context)
true))
true))
Expand Down
20 changes: 20 additions & 0 deletions test/liberator_mixin/validation/core_test.clj
Expand Up @@ -45,6 +45,26 @@
(is (= 200 (:status response)))
(is (= "OK" (get-in response [:body :status])))))

(testing "validates all known methods (except options) by default"
(doseq [method #{:get :head :put :patch :spinach}]
(let [resource
(core/build-resource
(json/with-json-media-type)
(validation/with-validation)
{:known-methods [:get :head :put :patch :options :spinach]
:allowed-methods [:get :head :put :patch :options :spinach]
:validator (new-mock-validator false nil)}
{:handle-created (constantly {:status "OK"})
:handle-ok (constantly {:status "OK"})})
response (call-resource
resource
(->
(ring/request method "/")
(ring/header :content-type json/json-media-type)
(ring/body {:key "value"})))]
(is (= 422 (:status response))
(str "Method: " method)))))

(testing "does not validate request if not included in validate-methods"
(let [resource (core/build-resource
(json/with-json-media-type)
Expand Down

0 comments on commit c3b928b

Please sign in to comment.