Last active
December 26, 2020 04:25
-
-
Save Jared314/6479982 to your computer and use it in GitHub Desktop.
Adventures in Validating Email Addresses with Clojure
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
admin@mailserver1 | |
niceandsimple@example.com | |
very.common@example.com | |
a.little.lengthy.but.fine@dept.example.com | |
disposable.style.email.with+symbol@example.com | |
postbox@com | |
user@[192.168.2.1] | |
user@[IPv6:2001:db8:1ff::a0b:dbd0] | |
"much.more unusual"@example.com | |
" "@example.org | |
"very.unusual.@.unusual.com"@example.com | |
"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com | |
!#$%&'*+-/=?^_`{}|~@example.org | |
"()<>[]:,;@\\\"!#$%&'*+-/=?^_`{}| ~.a"@example.org | |
(comment)user1.(comment)@(comment)example.com(comment) |
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
;; Version 1 | |
(ns validate-email | |
(:require [clojure.test :refer :all])) | |
(defn email-valid? [s] (not (nil? (re-matches #"[a-z\.\+]+@[a-z\.]+" s)))) | |
(deftest filters-invalid-email-addresses | |
(let [invalid-emails ["Abc.example.com" | |
"A@b@c@example.com" | |
"a\"b(c)d,e:f;g<h>i[j\\k]l@example.com" | |
"just\"not\"right@example.com" | |
"this is\"not\\allowed@example.com" | |
"this\\ still\\\"not\\\\allowed@example.com" | |
"not(this)either@example.com"]] | |
(is (every? (complement email-valid?) invalid-emails)))) | |
;; Version 2 | |
(ns validate-email | |
(:require [clojure.test :refer :all] | |
[instaparse.core :as insta])) | |
(def parse-email-address | |
(insta/parser "ADDRESS = LOCAL <'@'> DOMAIN | |
LOCAL = COMMENT? LOCALVALUE COMMENT? | |
LOCALVALUE = (LOCALSANDQUOTES DOT)* LOCALSANDQUOTES | |
<LOCALSANDQUOTES> = (LOCALS | QUOTEDSTRING) | |
<LOCALS> = (LETTER | NUMBER | LOCALSYMBOL)+ | |
QUOTEDSTRING = <'\"'> (LETTER | NUMBER | LOCALSYMBOL | DOT | QUOTEDSYMBOL)* <'\"'> | |
<LOCALSYMBOL> = '!'|'#'|'$'|'%'|'&'|'*'|'+'|'-'|'/'|'?'|'^'|'_'|'`'|'{'|'}'|'~'|'='|'|'|\"'\" | |
<QUOTEDSYMBOL> = ' '|'('|')'|','|':'|';'|'<'|'>'|'@'|'['|']'|QUOTEDESCAPEDSYMBOL | |
QUOTEDESCAPEDSYMBOL = <'\\\\'> ('\\\\' | '\"' | LETTER | NUMBER | HEXENTITY | ' ' | '\0') | |
DOMAIN = COMMENT? DOMAINVALUE COMMENT? | |
DOMAINVALUE = (IPADDRESS | HOSTNAME) | |
IPADDRESS = <'['> (IP4ADDRESS|IP6ADDRESS|DOMAINLITERAL) <']'> | |
IP4ADDRESS = NUMBER NUMBER? NUMBER? '.' NUMBER NUMBER? NUMBER? '.' NUMBER NUMBER? NUMBER? '.' NUMBER NUMBER? NUMBER? | |
IP6ADDRESS = <'IPv6:'> (HEX | ':')+ | |
DOMAINLITERAL = DOMAINLITERALSYMBOL+ | |
DOMAINLITERALSYMBOL = LETTER|NUMBER|'!'|'\"'|'#'|'$'|'%'|'&'|\"'\"|'('|')'|'*'|'+'|','|'-'|'.'|'/'|':'|';'|'<'|'='|'>'|'?'|'@'|'^'|'_'|'`'|'{'|'|'|'}'|'~' | |
HOSTNAME = (LABEL DOT)* LABEL | |
LABEL = &(LETTER | NUMBER) (LETTER | NUMBER | '-')* (LETTER | NUMBER) | |
<DOT> = '.' | |
COMMENT = <'('> (LETTER | COMMENTESCAPEDSYMBOL | HEXENTITY | COMMENT)* <')'> | |
<COMMENTESCAPEDSYMBOL> = '\\\\' ('\\\\' | '\"' | ')') | |
HEXENTITY = <'&'> <'#'> <'x'> HEX+ <';'> | |
<HEX> = NUMBER|'a'|'b'|'c'|'d'|'e'|'f'|'A'|'B'|'C'|'D'|'E'|'F' | |
<NUMBER> = '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9' | |
<LETTER> = 'a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i'|'j'|'k'|'l'|'m'|'n'|'o'|'p'|'q'|'r'|'s'|'t'|'u'|'v'|'w'|'x'|'y'|'z'| | |
'A'|'B'|'C'|'D'|'E'|'F'|'G'|'H'|'I'|'J'|'K'|'L'|'M'|'N'|'O'|'P'|'Q'|'R'|'S'|'T'|'U'|'V'|'W'|'X'|'Y'|'Z' | |
")) | |
(defn email-valid? [e] | |
(not (insta/failure? (parse-email-address e)))) | |
(deftest filters-invalid-email-addresses | |
(let [invalid-emails ["Abc.example.com" | |
"A@b@c@example.com" | |
"a\"b(c)d,e:f;g<h>i[j\\k]l@example.com" | |
"just\"not\"right@example.com" | |
"this is\"not\\allowed@example.com" | |
"this\\ still\\\"not\\\\allowed@example.com" | |
"not(this)either@example.com"]] | |
(is (every? (complement email-valid?) invalid-emails)))) | |
(deftest validates-valid-email-addresses | |
(let [valid-emails ["admin@mailserver1" | |
"niceandsimple@example.com" | |
"very.common@example.com" | |
"disposable.style.email.with+symbol@example.com" | |
"postbox@com" | |
"user@[192.168.2.1]" | |
"user@[IPv6:2001:db8:1ff::a0b:dbd0]" | |
"\"much.more unusual\"@example.com" | |
"\"very.unusual.@.unusual.com\"@example.com" | |
"\"very.(),:;<>[]\\\".VERY.\\\"very@\\\\ \\\"very\\\".unusual\"@strange.example.com" | |
"!#$%&'*+-/=?^_`{}|~@example.org" | |
"\"()<>[]:,;@\\\\\\\"!#$%&'*+-/=?^_`{}| ~.a\"@example.org" | |
"\" \"@example.org" | |
"(comment)john.smith(comment)@(comment)example.com(comment)"]] | |
(is (every? email-valid? valid-emails)))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment