Skip to content

Instantly share code, notes, and snippets.

@ryanbaldwin
Last active August 29, 2015 14:10
Show Gist options
  • Save ryanbaldwin/707c12e8b68983731968 to your computer and use it in GitHub Desktop.
Save ryanbaldwin/707c12e8b68983731968 to your computer and use it in GitHub Desktop.
Illustrates the impact on refactoring writing tests can have on a namespace.
(ns hipstr.validators.user-validator
(:require [noir.validation :as v]
[validateur.validation :refer :all]))
(def email-blank-msg
"Email is a required field")
(def email-format-msg
"The email's format is incorrect")
(def email-validator
(validation-set
(validate-with-predicate :email
#(v/is-email? (:email %))
:message-fn (fn [validation-map]
(if (v/has-value? (:email validation-map))
email-format-msg
email-blank-msg)))))
(def username-blank-msg
"Username is a required field")
(def username-invalid-msg
"Only letters, numbers, and underscores allowed.")
(def username-validator
(validation-set
(format-of :username
:format #"^[a-zA-Z0-9_]*$"
:blank-message username-blank-msg
:message username-invalid-msg)))
(def password-blank-msg
"Password is a required field")
(def password-invalid-msg
"Passwords must be between 8 and 100 characters long.")
(def password-validator
(validation-set
(length-of :password
:within (range 8 101)
:message-fn (fn [type m attribute & args]
(if (= type :blank)
password-blank-msg
password-invalid-msg)))))
(defn validate-signup [signup]
((compose-sets email-validator username-validator password-validator) signup))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment