Skip to content

Commit

Permalink
feat(clojure): add add-custom-domains
Browse files Browse the repository at this point in the history
  • Loading branch information
choznerol committed Nov 9, 2021
1 parent 1573958 commit 05208e3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
13 changes: 12 additions & 1 deletion platform/clojure/mailchecker.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
(ns mailchecker)

(require '[clojure.string :as str])
(require '[clojure.set :as set])

(def ^:const blacklist (set ["tmail.com", "33mail.com", "guerrillamailblock.com"]))

(def custom_domains (atom #{}))

(defn is-email?
"Returns true if email is an email address"
[email]
Expand Down Expand Up @@ -42,7 +45,10 @@
(defn in-blacklist?
"Returns true if any suffix of the email domain is in the blacklist"
[email]
(some (partial contains? blacklist) (all-domain-suffixes email)))
(let [domains (all-domain-suffixes email)]
(or
(some (partial contains? @custom_domains) domains)
(some (partial contains? blacklist) domains))))

(defn valid?
"Returns true if the email is valid"
Expand All @@ -51,3 +57,8 @@
(is-email? email)
(not
(in-blacklist? email))))

(defn add-custom-domains
"Add more domains to the blacklist"
[domains]
(swap! custom_domains (set/union @custom_domains domains)))
13 changes: 12 additions & 1 deletion platform/clojure/mailchecker.tmpl.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
(ns mailchecker)

(require '[clojure.string :as str])
(require '[clojure.set :as set])

(def ^:const blacklist (set ["tmail.com", "33mail.com", "guerrillamailblock.com"]))

(def custom_domains (atom #{}))

(defn is-email?
"Returns true if email is an email address"
[email]
Expand Down Expand Up @@ -42,7 +45,10 @@
(defn in-blacklist?
"Returns true if any suffix of the email domain is in the blacklist"
[email]
(some (partial contains? blacklist) (all-domain-suffixes email)))
(let [domains (all-domain-suffixes email)]
(or
(some (partial contains? @custom_domains) domains)
(some (partial contains? blacklist) domains))))

(defn valid?
"Returns true if the email is valid"
Expand All @@ -51,3 +57,8 @@
(is-email? email)
(not
(in-blacklist? email))))

(defn add-custom-domains
"Add more domains to the blacklist"
[domains]
(swap! custom_domains (set/union @custom_domains domains)))
13 changes: 12 additions & 1 deletion test/platform.clojure.test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
(:use clojure.test))

(defn expect-valid-result [expected-valid email]
(is (= expected-valid (mailchecker/valid? email))))
(is (= expected-valid (mailchecker/valid? email))
(format "Expected valid?(%s) to be %b" email expected-valid)))

(def expect-invalid (partial expect-valid-result false))
(def expect-valid (partial expect-valid-result true))
Expand Down Expand Up @@ -47,4 +48,14 @@
(expect-valid (str "test@" domain ".gmail.com"))))
mailchecker/blacklist))

(deftest add-custom-domains)
(do
(expect-valid "[email protected]")
(expect-valid "[email protected]")
(expect-valid "[email protected]")
(mailchecker/add-custom-domains #{"youtube.com" "google.com"})
(expect-invalid "[email protected]")
(expect-invalid "[email protected]")
(expect-valid "[email protected]"))

(run-all-tests)

0 comments on commit 05208e3

Please sign in to comment.