Skip to content

Commit ea1557b

Browse files
committed
Stdlib::Email type
Add new type to validate emails. The regex has been taken from the [html5 spec][1] [1]: https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
1 parent 57b339a commit ea1557b

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

spec/type_aliases/email_spec.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'spec_helper'
2+
3+
# Test cases are a combination of the test cases used in MediaWiki[1] and a
4+
# Reference found on line[2]. Some of the test cases in the later list have
5+
# been dropped as the regex used in the HTML5 specification[3] (and in this type)
6+
# allows for wilful violation of the RFC's
7+
#
8+
# [1]https://github.com/wikimedia/mediawiki/blob/master/tests/phpunit/integration \
9+
# /includes/SanitizerValidateEmailTest.php
10+
# [2]https://gist.github.com/cjaoude/fd9910626629b53c4d25
11+
# [3]https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
12+
13+
describe 'Stdlib::Email' do
14+
describe 'valid handling' do
15+
['email@example.com',
16+
'EMAIL@example.com',
17+
'email@EXAMPLE.com',
18+
'email@192.0.2.1',
19+
'_______@example.com',
20+
'firstname.lastname@example.com',
21+
'firstname+lastname@example.com',
22+
'firstname-lastname@example.com',
23+
'1234567890@example.com',
24+
'email@subdomain.example.com',
25+
'email@example-one.com',
26+
'email@example.name',
27+
'email@example.museum',
28+
'email@example.co.jp',
29+
'email@example',
30+
'user@example.1234',
31+
'user@a'].each do |value|
32+
describe value.inspect do
33+
it { is_expected.to allow_value(value) }
34+
end
35+
end
36+
end
37+
describe 'invalid handling' do
38+
['plainaddress',
39+
'#@%^%#$@#$@#.com',
40+
'@example.com',
41+
' email@example.com',
42+
'email@example.com ',
43+
"email@example.com\t",
44+
'user email@example.com',
45+
'useremail@example com',
46+
'user,email@example.com',
47+
'useremail@example,com',
48+
'useremail@.',
49+
'useremail@.example.org',
50+
'useremail@a......',
51+
'useràexample.com',
52+
'Joe Smith <email@example.com>',
53+
'email.example.com',
54+
'email@example@example.com',
55+
'あいうえお@example.com',
56+
'email@example.com (Joe Smith)',
57+
'email@-example.com',
58+
'email@example..com',
59+
'”(),:;<>[\]@example.com',
60+
'just”not”right@example.com',
61+
'this\ is"really"not\allowed@example.com'].each do |value|
62+
describe value.inspect do
63+
it { is_expected.not_to allow_value(value) }
64+
end
65+
end
66+
end
67+
end

types/email.pp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
2+
type Stdlib::Email = Pattern[/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/]

0 commit comments

Comments
 (0)