Skip to content

Instantly share code, notes, and snippets.

@ewilazarus
Last active March 1, 2016 11:57
Show Gist options
  • Save ewilazarus/bbee24d9da3c5d0974fd to your computer and use it in GitHub Desktop.
Save ewilazarus/bbee24d9da3c5d0974fd to your computer and use it in GitHub Desktop.
(ns sagaz-back.validation)
; Helper functions
(defn validate* [field value regexp]
(if-not (re-matches regexp value)
(keyword (str "invalid-" (name field)))))
(defn validate [data criteria]
(-> (reduce-kv (fn [acc k re] (conj acc (validate* k (k data) re)))
[]
criteria)
(#(remove nil? %))))
; Domain specific
(defn user [user-data]
(validate user-data {:username #"^\w{6,10}$"
:email #"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"
:password #"^\w{6,8}$"}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment