-
-
Save MichaelDrogalis/4562107 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(ns dire-examples.bad-core | |
(:require [dire.core :refer [with-precondition! with-postcondition!]])) | |
(defn save-user [& {:keys [username email password state]}] | |
(if (and (and (> (count username) 4) (< (count username) 11)) | |
(and (> (count password) 6) (re-matches #".*\d.*" password)) | |
(re-matches #"\w+@\w+\.\w+" "[email protected]") | |
(re-matches #"[A-Z]{2}" state)) | |
(let [result :ok] ; Or :not-ok | |
"Persist the user to a database here." | |
(if (not= result :ok) (throw (Throwable. "Save unsuccessful")) :ok)) | |
(throw (Throwable. "Something terrible has gone wrong")))) | |
(save-user :username "XPherior" :email "[email protected]" :password "mikemike4" :state "PA") |
This file contains hidden or 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
(ns dire-examples.core | |
(:require [dire.core :refer [with-precondition! with-postcondition!]])) | |
(defn save-user [& {:keys [username email password state]}] | |
"Persist the user to a database here." | |
:ok ;Or :not-ok | |
) | |
(with-precondition! #'save-user | |
:legal-username-length | |
(fn [& {:keys [username]}] | |
(let [length (count username)] | |
(and (> length 4) (< length 11))))) | |
(with-precondition! #'save-user | |
:well-formed-password | |
(fn [& {:keys [password]}] | |
(and (> (count password) 6) (re-matches #".*\d.*" password)))) | |
(with-precondition! #'save-user | |
:well-formed-email | |
(fn [& {:keys [email]}] | |
(re-matches #"\w+@\w+\.\w+" "[email protected]"))) | |
(with-precondition! #'save-user | |
:legal-state-abbreviation | |
(fn [& {:keys [state]}] | |
(re-matches #"[A-Z]{2}" state))) | |
(with-postcondition! #'save-user | |
:successful-save | |
(fn [result] | |
(= result :ok))) | |
(save-user :username "XPherior" :email "[email protected]" :password "mikemike4" :state "PA") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment