Validity Checks using Clojure

Knoldus Inc.
Knoldus - Technical Insights
2 min readMar 16, 2014

For putting Validation checks using Clojure code, clojure noir library checks are used.

One needs to first create a form page using basic html for placing the checks.As an Example here, I am using a registration page text boxes to put the validation checks over the required fields.

[code language=”html”]
<label for=”id”>
{% if id-error %}
<div>{{id-error}}</div>
{% endif %}
<input id=”id” name=”id” placeholder=”User Name” type=”text” value={{id}}></input>
</label>
<label for=”email”>
{% if id-error %}
<div>{{email-error}}</div>
{% endif %}
<input id=”email” name=”email” placeholder=”Email” type=”text” value={{email}}></input>
</label>
<label for=”pass”>
{% if pass-error %}
<div>{{pass-error}}</div>
{% endif %}
<input id=”pass” name=”pass” placeholder=”Password” type=”password” value={{pass}}></input>
</label>
<label for=”confirmPass”>
{% if confirmPass-error %}
<div>{{confirmPass-error}}</div>
{% endif %}
<input id=”confirmPass” name=”confirmPass” placeholder=”Confirm Password” type=”password” value={{confirmPass}}></input>
</label>
[/code]

Then we move ahead by creating a register.clj file where all validations are defined under the namespace definition:

[code language=”html”]
(ns projectName.routes.register
(:use compojure.core)
(:require
[noir.session :as session]
[noir.response :as resp]
[noir.validation :as vali])
[/code]

Next is to create the route that defines the register function which is called when the register page is rendered.

[code language=”html”]
(defroutes register-routes
(GET “/register” []
(register))
[/code]

the above route calls the following function:

[code language=”html”]
(defn register [& [id]]
(layout/render
“register.html”
{:id id
:id-error (vali/on-error :id first)
:email-error (vali/on-error :email first)
:pass-error (vali/on-error :pass first)
:confirmPass-error (vali/on-error :confirmPass first)}))
[/code]
where, [id] for any number of arguments passed, (layout/render) renders with that html page where validations are required to be placed, :id-error/email-error are variables placed in the html form that will define the error block, (vali/on-error) denotes noir validation structure.

Next step is to define the errors and put checks over the required fields i.e; defining the errors for above created check blocks.

[code language=”html”]
(defn valid? [id email pass confirmPass]
(vali/rule (vali/has-value? id)
[:id “Username is required”])
(vali/rule (vali/is-email? email)
[:email “Invalid Email Address”])
(vali/rule (vali/min-length? id 8)
[:id “Username must be at least 8 characters long”])
(vali/rule (vali/min-length? pass 8)
[:pass “Password must be at least 8 characters long”])
(vali/rule (= pass confirmPass)
[:confirmPass “Passwords does not match”])
(not (vali/errors? :id :email :pass :confirmPass)))
[/code]

--

--

Knoldus Inc.
Knoldus - Technical Insights

Group of smart Engineers with a Product mindset who partner with your business to drive competitive advantage | www.knoldus.com