Created
March 27, 2017 02:51
-
-
Save maxp/f91799f90ac854f2029672955e280ee7 to your computer and use it in GitHub Desktop.
Input form parameters validation example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn validate-first_name [params] | |
(if-let [nm (-> params :first_name str s/trim norm-first_name)] | |
{:first_name nm} | |
{::err "wrong first_name" :fld :first_name})) | |
; | |
(defn validate-last_name [params] | |
(if-let [nm (-> params :last_name str s/trim norm-last_name)] | |
{:last_name nm} | |
{::err "wrong last_name" :fld :last_name})) | |
; | |
(defn validate-town [params] | |
(let [town (-> params :town str s/trim)] | |
(if (re-matches #"(?i)[a-z0-9\-\.\,\ ]{2,60}" town) | |
{:town (cap-first town)} | |
{::err "wrong town" :fld :town}))) | |
; | |
(defn handler [params] | |
(let [data | |
(reduce | |
(fn [data validator] | |
(let [res (validator params)] | |
;; stop on ::err or collect validated params | |
(if (::err res) | |
(reduced res) | |
(into data res)))) | |
{} | |
[ validate-first_name | |
validate-last_name | |
validate-town])] | |
; | |
(if-let [errmsg (::err data)] | |
(error-response {:err :param :fld (:fld data) :msg errmsg}) | |
(process-valid-data data) | |
; | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment