email_format_validator 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.md +38 -0
- data/email_format_validator.gemspec +3 -3
- data/lib/email_format_validator.rb +12 -10
- data/test/email_format_validator_test.rb +97 -7
- metadata +8 -6
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Axel Vergult
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
Installation
|
2
|
+
------------
|
3
|
+
|
4
|
+
Add this line to your Gemfile:
|
5
|
+
|
6
|
+
gem 'email_format_validator'
|
7
|
+
|
8
|
+
Then run:
|
9
|
+
|
10
|
+
bundle install
|
11
|
+
|
12
|
+
Usage
|
13
|
+
-----
|
14
|
+
|
15
|
+
Add the following to one of your models:
|
16
|
+
|
17
|
+
validates :email, :email_format => true
|
18
|
+
|
19
|
+
You can turn on RFC 822 compliant email verification by passing on:
|
20
|
+
|
21
|
+
validates :email, :email_format => { :rfc => true }
|
22
|
+
|
23
|
+
Be aware that this is disabled by default as websites will often need a less permissive email check than the RFC one (check out tests for more details).
|
24
|
+
|
25
|
+
You can also modify the default message if validation fails:
|
26
|
+
|
27
|
+
validates :email, :email_format => { :message => "is not formatted properly" }
|
28
|
+
|
29
|
+
Tests
|
30
|
+
-----
|
31
|
+
|
32
|
+
cd test
|
33
|
+
ruby email_format_validator_test.rb
|
34
|
+
|
35
|
+
Credits
|
36
|
+
-------
|
37
|
+
|
38
|
+
Regular Expression tests based on [Comparing E-mail Address Validating Regular Expressions](http://fightingforalostcause.net/misc/2006/compare-email-regex.php)
|
@@ -3,12 +3,12 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "email_format_validator"
|
6
|
-
s.version = "0.0.
|
6
|
+
s.version = "0.0.2"
|
7
7
|
s.authors = ["Axel Vergult"]
|
8
8
|
s.email = ["axel.vergult@gmail.com"]
|
9
9
|
s.homepage = "https://github.com/episko/email_format_validator"
|
10
|
-
s.summary = %q{A Rails 3 email format validator
|
11
|
-
s.description = %q{A Rails 3 email format validator
|
10
|
+
s.summary = %q{A Rails 3 email format validator with RFC 822 compliant option}
|
11
|
+
s.description = %q{A Rails 3 email format validator with RFC 822 compliant option}
|
12
12
|
|
13
13
|
s.files = `git ls-files`.split("\n")
|
14
14
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
@@ -8,20 +8,22 @@ class EmailFormatValidator < ActiveModel::EachValidator
|
|
8
8
|
def self.create_regexp(string)
|
9
9
|
Regexp.new string, nil, 'n'
|
10
10
|
end
|
11
|
-
ATOM
|
12
|
-
QTEXT
|
13
|
-
QPAIR
|
14
|
-
QSTRING
|
15
|
-
WORD
|
16
|
-
LOCAL_PT
|
17
|
-
ADDRESS
|
11
|
+
ATOM = create_regexp "[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+"
|
12
|
+
QTEXT = create_regexp "[^\\x0d\\x22\\x5c\\x80-\\xff]"
|
13
|
+
QPAIR = create_regexp "\\x5c[\\x00-\\x7f]"
|
14
|
+
QSTRING = create_regexp "\\x22(?:#{QTEXT}|#{QPAIR})*\\x22"
|
15
|
+
WORD = create_regexp "(?:#{ATOM}|#{QSTRING})"
|
16
|
+
LOCAL_PT = create_regexp "#{WORD}(?:\\x2e#{WORD})*"
|
17
|
+
ADDRESS = create_regexp "#{LOCAL_PT}\\x40#{URI::REGEXP::PATTERN::HOSTNAME}"
|
18
18
|
|
19
|
-
EMAIL
|
19
|
+
EMAIL = /\A#{ADDRESS}\z/
|
20
|
+
LESS_RFC_COMPLIANT_EMAIL = /\A[\w\-_\.]+@((?:[\w\-\.]+\.)+[a-z]{2,})\Z/i
|
20
21
|
end
|
21
22
|
|
22
23
|
def validate_each(record, attribute, value)
|
23
|
-
|
24
|
-
|
24
|
+
pattern_choosed = options[:rfc] ? Patterns::EMAIL : Patterns::LESS_RFC_COMPLIANT_EMAIL
|
25
|
+
unless value =~ pattern_choosed
|
26
|
+
record.errors.add(attribute, options[:message] || :invalid)
|
25
27
|
end
|
26
28
|
end
|
27
29
|
|
@@ -6,20 +6,24 @@ class TestUser < TestModel
|
|
6
6
|
validates :email, :email_format => true
|
7
7
|
end
|
8
8
|
|
9
|
+
class TestUserWithRfcCompliantEmail < TestModel
|
10
|
+
validates :email, :email_format => { :rfc => true }
|
11
|
+
end
|
12
|
+
|
9
13
|
class TestUserAllowsNilToTrue < TestModel
|
10
|
-
validates :email, :email_format => {:allow_nil => true}
|
14
|
+
validates :email, :email_format => { :allow_nil => true }
|
11
15
|
end
|
12
16
|
|
13
17
|
class TestUserAllowsNilToFalse < TestModel
|
14
|
-
validates :email, :email_format => {:allow_nil => false}
|
18
|
+
validates :email, :email_format => { :allow_nil => false }
|
15
19
|
end
|
16
20
|
|
17
21
|
class TestUserWithMessage < TestModel
|
18
|
-
validates :email, :email_format => {:message => 'is not
|
22
|
+
validates :email, :email_format => { :message => 'is not formatted properly' }
|
19
23
|
end
|
20
24
|
|
21
25
|
class TestEmailFormatValidator < MiniTest::Unit::TestCase
|
22
|
-
def
|
26
|
+
def rfc_valid_emails
|
23
27
|
[
|
24
28
|
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@letters-in-local.org',
|
25
29
|
'01234567890@numbers-in-local.net',
|
@@ -46,12 +50,18 @@ class TestEmailFormatValidator < MiniTest::Unit::TestCase
|
|
46
50
|
'the-total-length@of-an-entire-address-cannot-be-longer-than-two-hundred-and-fifty-four-characters' +
|
47
51
|
'-and-this-address-is-254-characters-exactly-so-it-should-be-valid-and-im-going-to-add-some-more' +
|
48
52
|
'-words-here-to-increase-the-lenght-blah-blah-blah-blah-bla.org',
|
53
|
+
# 'bracketed-IP-instead-of-domain@[127.0.0.1]',
|
49
54
|
'local@sub.domains.com'
|
50
55
|
]
|
51
56
|
end
|
52
57
|
|
53
|
-
def
|
58
|
+
def rfc_invalid_emails
|
54
59
|
[
|
60
|
+
'',
|
61
|
+
# 'foo@bar',
|
62
|
+
# 'foo@bar.c',
|
63
|
+
'foo@bar.com@bar.com',
|
64
|
+
'foo@',
|
55
65
|
'@missing-local.org',
|
56
66
|
'! #$%`|@invalid-characters-in-local.org',
|
57
67
|
'(),:;`|@more-invalid-characters-in-local.org',
|
@@ -59,10 +69,82 @@ class TestEmailFormatValidator < MiniTest::Unit::TestCase
|
|
59
69
|
'.local-starts-with-dot@sld.com',
|
60
70
|
'local-ends-with-dot.@sld.com',
|
61
71
|
'two..consecutive-dots@sld.com',
|
72
|
+
# 'partially."quoted"@sld.com',
|
73
|
+
# 'the-local-part-is-invalid-if-it-is-longer-than-sixty-four-characters@sld.net',
|
62
74
|
'missing-sld@.com',
|
63
75
|
'sld-starts-with-dashsh@-sld.com',
|
64
76
|
'sld-ends-with-dash@sld-.com',
|
65
77
|
'invalid-characters-in-sld@! "#$%(),/;<>_[]`|.org',
|
78
|
+
# 'missing-dot-before-tld@com',
|
79
|
+
# 'missing-tld@sld.',
|
80
|
+
# 'the-total-length@of-an-entire-address-cannot-be-longer-than-two-hundred-and-fifty-four-characters-and' +
|
81
|
+
# '-this-address-is-255-characters-exactly-so-it-should-be-invalid-and-im-going-to-add-some-more-words' +
|
82
|
+
# '-here-to-increase-the-lenght-blah-blah-blah-blah-bl.org',
|
83
|
+
'missing-at-sign.net',
|
84
|
+
'unbracketed-IP@127.0.0.1',
|
85
|
+
'invalid-ip@127.0.0.1.26',
|
86
|
+
'another-invalid-ip@127.0.0.256',
|
87
|
+
'IP-and-port@127.0.0.1:25'
|
88
|
+
]
|
89
|
+
end
|
90
|
+
|
91
|
+
def valid_emails
|
92
|
+
[
|
93
|
+
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@letters-in-local.org',
|
94
|
+
'01234567890@numbers-in-local.net',
|
95
|
+
# '&\'*+-./=?^_{}~@other-valid-characters-in-local.net',
|
96
|
+
# 'mixed-1234-in-{+^}-local@sld.net',
|
97
|
+
'a@single-character-in-local.org',
|
98
|
+
# '"quoted"@sld.com',
|
99
|
+
# '"\e\s\c\a\p\e\d"@sld.com',
|
100
|
+
# '"quoted-at-sign@sld.org"@sld.com',
|
101
|
+
# '"escaped\"quote"@sld.com',
|
102
|
+
# '"back\slash"@sld.com',
|
103
|
+
'one-character-third-level@a.example.com',
|
104
|
+
'single-character-in-sld@x.org',
|
105
|
+
'local@dash-in-sld.com',
|
106
|
+
'letters-in-sld@123.com',
|
107
|
+
'one-letter-sld@x.org',
|
108
|
+
'uncommon-tld@sld.museum',
|
109
|
+
'uncommon-tld@sld.travel',
|
110
|
+
'uncommon-tld@sld.mobi',
|
111
|
+
'country-code-tld@sld.uk',
|
112
|
+
'country-code-tld@sld.rw',
|
113
|
+
'local@sld.newTLD',
|
114
|
+
# 'punycode-numbers-in-tld@sld.xn--3e0b707e',
|
115
|
+
'the-total-length@of-an-entire-address-cannot-be-longer-than-two-hundred-and-fifty-four-characters' +
|
116
|
+
'-and-this-address-is-254-characters-exactly-so-it-should-be-valid-and-im-going-to-add-some-more' +
|
117
|
+
'-words-here-to-increase-the-lenght-blah-blah-blah-blah-bla.org',
|
118
|
+
# 'bracketed-IP-instead-of-domain@[127.0.0.1]',
|
119
|
+
'local@sub.domains.com'
|
120
|
+
]
|
121
|
+
end
|
122
|
+
|
123
|
+
def invalid_emails
|
124
|
+
[
|
125
|
+
'',
|
126
|
+
'foo@bar',
|
127
|
+
'foo@bar.c',
|
128
|
+
'foo@bar.com@bar.com',
|
129
|
+
'foo@',
|
130
|
+
'@missing-local.org',
|
131
|
+
'! #$%`|@invalid-characters-in-local.org',
|
132
|
+
'(),:;`|@more-invalid-characters-in-local.org',
|
133
|
+
'<>@[]\`|@even-more-invalid-characters-in-local.org',
|
134
|
+
# '.local-starts-with-dot@sld.com',
|
135
|
+
# 'local-ends-with-dot.@sld.com',
|
136
|
+
# 'two..consecutive-dots@sld.com',
|
137
|
+
'partially."quoted"@sld.com',
|
138
|
+
# 'the-local-part-is-invalid-if-it-is-longer-than-sixty-four-characters@sld.net',
|
139
|
+
'missing-sld@.com',
|
140
|
+
# 'sld-starts-with-dashsh@-sld.com',
|
141
|
+
# 'sld-ends-with-dash@sld-.com',
|
142
|
+
'invalid-characters-in-sld@! "#$%(),/;<>_[]`|.org',
|
143
|
+
'missing-dot-before-tld@com',
|
144
|
+
'missing-tld@sld.',
|
145
|
+
# 'the-total-length@of-an-entire-address-cannot-be-longer-than-two-hundred-and-fifty-four-characters-and' +
|
146
|
+
# '-this-address-is-255-characters-exactly-so-it-should-be-invalid-and-im-going-to-add-some-more-words' +
|
147
|
+
# '-here-to-increase-the-lenght-blah-blah-blah-blah-bl.org',
|
66
148
|
'missing-at-sign.net',
|
67
149
|
'unbracketed-IP@127.0.0.1',
|
68
150
|
'invalid-ip@127.0.0.1.26',
|
@@ -79,16 +161,24 @@ class TestEmailFormatValidator < MiniTest::Unit::TestCase
|
|
79
161
|
invalid_emails.each { |email| refute TestUser.new(:email => email).valid? }
|
80
162
|
end
|
81
163
|
|
164
|
+
def test_valid_emails_with_rfc_compliant
|
165
|
+
rfc_valid_emails.each { |email| assert TestUserWithRfcCompliantEmail.new(:email => email).valid? }
|
166
|
+
end
|
167
|
+
|
168
|
+
def test_invalid_emails_with_rfc_compliant
|
169
|
+
rfc_invalid_emails.each { |email| refute TestUserWithRfcCompliantEmail.new(:email => email).valid? }
|
170
|
+
end
|
171
|
+
|
82
172
|
def test_default_message_on_error
|
83
173
|
test_user = TestUser.new(:email => "invalid_email@")
|
84
174
|
refute test_user.valid?
|
85
|
-
assert test_user.errors[:email].include?("is
|
175
|
+
assert test_user.errors[:email].include?("is invalid")
|
86
176
|
end
|
87
177
|
|
88
178
|
def test_custom_message_on_error
|
89
179
|
test_user = TestUserWithMessage.new(:email => "invalid_email@")
|
90
180
|
refute test_user.valid?
|
91
|
-
assert test_user.errors[:email].include?("is not
|
181
|
+
assert test_user.errors[:email].include?("is not formatted properly")
|
92
182
|
end
|
93
183
|
|
94
184
|
def test_nil_email_when_allow_nil_option_is_not_set
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: email_format_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-11-
|
12
|
+
date: 2011-11-10 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
16
|
-
requirement: &
|
16
|
+
requirement: &70169645444580 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,8 +21,8 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
25
|
-
description: A Rails 3 email format validator
|
24
|
+
version_requirements: *70169645444580
|
25
|
+
description: A Rails 3 email format validator with RFC 822 compliant option
|
26
26
|
email:
|
27
27
|
- axel.vergult@gmail.com
|
28
28
|
executables: []
|
@@ -31,6 +31,8 @@ extra_rdoc_files: []
|
|
31
31
|
files:
|
32
32
|
- .gitignore
|
33
33
|
- Gemfile
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
34
36
|
- Rakefile
|
35
37
|
- email_format_validator.gemspec
|
36
38
|
- lib/email_format_validator.rb
|
@@ -59,7 +61,7 @@ rubyforge_project:
|
|
59
61
|
rubygems_version: 1.8.10
|
60
62
|
signing_key:
|
61
63
|
specification_version: 3
|
62
|
-
summary: A Rails 3 email format validator
|
64
|
+
summary: A Rails 3 email format validator with RFC 822 compliant option
|
63
65
|
test_files:
|
64
66
|
- test/email_format_validator_test.rb
|
65
67
|
- test/test_helper.rb
|