On 01/04/2012 03:05 AM, Takahiro Hozumi wrote:
In MVC pattern, Model should take responsibility for business logic.
Therefore I write validate function for creating in the model.
If creating a instance of the model should be safe, I must validate a
parameter in the create function.
My problem is that a controller have to validate a parameter twice in
the validate function and the create function.

Ring handler example

(defn handler [{:keys [params] :as req}]
   (if (person/valid? params)
     {:status 200 :body (json/generate-string (person/create params))}
     {:status 400}))

I think this might be suited to monads, which I don't fully
understand.

If you work with self-imposed restrictions that make you call functions with the same parameters twice in short order, you should reconsider those restrictions.

If all the information you need from a validator is true/false, you can just call a model function. One that is written under the assumption that a certain requirement is met, before it is called.

If the validator returns nil or some concrete piece of imformation, you just need to call it, store the result. Then you may call a model function with the result (if it is non-nil).

Moustache makes that very straightforward:

(defn integer [s]
   "returns nil if s does not represent an integer
    (try
      (Integer/parseInt s)
      (catch Exception e)))

(app ["order" [id integer]] my-handler) ; for "/order/134" @id@ will be bind to 134 (not "134"), this route will not match "/order/abc".

Example taken from https://github.com/cgrand/moustache


--
Thorsten Wilms

thorwil's design for free software:
http://thorwil.wordpress.com/

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to