email_validator 1.6.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +11 -2
- data/Changes.md +4 -0
- data/README.md +19 -26
- data/email_validator.gemspec +3 -3
- data/lib/email_validator.rb +15 -15
- data/lib/email_validator/strict.rb +7 -3
- data/spec/email_validator_spec.rb +16 -73
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: da5f6cc330adef4786045236ce8a34a49ad638a5b42c7447e7585711e0dccca4
|
4
|
+
data.tar.gz: 04eff8f220eaafdb748f8ba0bc1f8890f450b0620731b5c3a10c9125954b6e7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b39cbd7c9d87a3274f6d4b570a7a774ef0a232e5171f561a384e2c287d3242d832e91fb0240437484b5a500da98ee9715a20a5a15247075fbfd4100d596b0b2
|
7
|
+
data.tar.gz: 8907894a088e8a2c53c53eb346331abc561643b2316bd4f5132f8961bc9cb98e08dc3ea4cb930d3a038ad8199922c429047b314e75ff96f034f506936ae1621b
|
data/.travis.yml
CHANGED
data/Changes.md
CHANGED
data/README.md
CHANGED
@@ -17,47 +17,40 @@ bundle install
|
|
17
17
|
Then add the following to your model:
|
18
18
|
|
19
19
|
```ruby
|
20
|
-
validates :my_email_attribute, :
|
20
|
+
validates :my_email_attribute, email: true
|
21
21
|
```
|
22
22
|
|
23
|
-
##
|
24
|
-
|
25
|
-
In order to have stricter validation (according to http://www.remote.org/jochen/mail/info/chars.html) enable strict mode. You can do this globally by adding the following to your Gemfile:
|
26
|
-
|
27
|
-
```ruby
|
28
|
-
gem 'email_validator', :require => 'email_validator/strict'
|
29
|
-
```
|
23
|
+
## Validation outside a model
|
30
24
|
|
31
|
-
|
25
|
+
If you'd like to validate an email outside of a model then here are some class methods that you can use:
|
32
26
|
|
33
27
|
```ruby
|
34
|
-
|
28
|
+
EmailValidator.regexp # returns the regex
|
29
|
+
EmailValidator.valid?('narf@example.com') # => true
|
30
|
+
EmailValidator.invalid?('narf@example.com') # => false
|
35
31
|
```
|
36
32
|
|
37
|
-
## Validation
|
38
|
-
|
39
|
-
If you need to validate an email outside a model, you can get the regexp :
|
33
|
+
## Validation philosophy
|
40
34
|
|
41
|
-
|
35
|
+
The validation provided by this gem is loose. It just checks that there's an `@` with something before and after it. See [this article by David Gilbertson](https://hackernoon.com/the-100-correct-way-to-validate-email-addresses-7c4818f24643) for an explanation of why.
|
42
36
|
|
43
|
-
|
44
|
-
EmailValidator.regexp # returns the regex
|
45
|
-
EmailValidator.valid?('narf@example.com') # boolean
|
46
|
-
```
|
37
|
+
## Trimming whitespace
|
47
38
|
|
48
|
-
|
39
|
+
Your users may accidentally submit leading or trailing whitespace when submitting a form. You may want to automatically trim this. This is not the job of a validator gem but it's trivial to implement yourself by adding a setter in your model:
|
49
40
|
|
50
41
|
```ruby
|
51
|
-
|
42
|
+
def email=(e)
|
43
|
+
e = e.strip if e
|
44
|
+
super
|
45
|
+
end
|
52
46
|
```
|
53
47
|
|
54
|
-
|
55
|
-
|
56
|
-
This gem is thread safe, with one caveat: `EmailValidator.default_options` must be configured before use in a multi-threaded environment. If you configure `default_options` in a Rails initializer file, then you're good to go since initializers are run before worker threads are spawned.
|
48
|
+
You may also want to convert emails to lowercase in the same way.
|
57
49
|
|
58
|
-
##
|
50
|
+
## Alternative gems
|
59
51
|
|
60
|
-
|
52
|
+
Do you prefer a different email validation gem? If so, open an issue with a brief explanation of how it differs from this gem. I'll add a link to it in this README.
|
61
53
|
|
62
|
-
|
54
|
+
## Thread safety
|
63
55
|
|
56
|
+
This gem is thread safe.
|
data/email_validator.gemspec
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = %q{email_validator}
|
3
|
-
s.version = "
|
3
|
+
s.version = "2.0.0"
|
4
4
|
s.authors = ["Brian Alexander"]
|
5
|
-
s.description = %q{
|
5
|
+
s.description = %q{Email validator for Rails and ActiveModel.}
|
6
6
|
s.email = %q{balexand@gmail.com}
|
7
7
|
s.extra_rdoc_files = [
|
8
8
|
"LICENSE",
|
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
|
|
12
12
|
|
13
13
|
s.homepage = %q{https://github.com/balexand/email_validator}
|
14
14
|
s.require_paths = %w(lib)
|
15
|
-
s.summary = %q{An email validator for Rails
|
15
|
+
s.summary = %q{An email validator for Rails and ActiveModel.}
|
16
16
|
|
17
17
|
s.add_dependency("activemodel", ">= 0")
|
18
18
|
|
data/lib/email_validator.rb
CHANGED
@@ -1,28 +1,28 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
class EmailValidator < ActiveModel::EachValidator
|
4
|
-
@@default_options = {}
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'active_model'
|
5
3
|
|
4
|
+
class EmailValidator < ActiveModel::EachValidator
|
6
5
|
def self.regexp(options = {})
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
if options[:strict_mode]
|
7
|
+
ActiveSupport::Deprecation.warn(
|
8
|
+
'Strict mode has been deprecated in email_validator 2.0. To fix this warning, '\
|
9
|
+
'remove `strict_mode: true` from your validation call.'
|
10
|
+
)
|
11
|
+
end
|
10
12
|
|
11
|
-
|
13
|
+
/[^\s]@[^\s]/
|
12
14
|
end
|
13
15
|
|
14
|
-
def self.valid?(value, options =
|
15
|
-
|
16
|
+
def self.valid?(value, options = nil)
|
17
|
+
!invalid?(value)
|
16
18
|
end
|
17
19
|
|
18
|
-
def self.
|
19
|
-
|
20
|
+
def self.invalid?(value, options = nil)
|
21
|
+
!(value =~ regexp)
|
20
22
|
end
|
21
23
|
|
22
24
|
def validate_each(record, attribute, value)
|
23
|
-
|
24
|
-
|
25
|
-
unless self.class.valid?(value, self.options)
|
25
|
+
if self.class.invalid?(value)
|
26
26
|
record.errors.add(attribute, options[:message] || :invalid)
|
27
27
|
end
|
28
28
|
end
|
@@ -1,5 +1,9 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
ActiveSupport::Deprecation.warn(
|
4
|
+
'Strict mode has been deprecated in email_validator 2.0. You are receiving '\
|
5
|
+
'this warning because your project is requiring "email_validator/strict", '\
|
6
|
+
'most likely in your Gemfile.'
|
7
|
+
)
|
4
8
|
|
5
|
-
|
9
|
+
require 'email_validator'
|
@@ -5,10 +5,6 @@ class TestUser < TestModel
|
|
5
5
|
validates :email, :email => true
|
6
6
|
end
|
7
7
|
|
8
|
-
class StrictUser < TestModel
|
9
|
-
validates :email, :email => {:strict_mode => true}
|
10
|
-
end
|
11
|
-
|
12
8
|
class TestUserAllowsNil < TestModel
|
13
9
|
validates :email, :email => {:allow_nil => true}
|
14
10
|
end
|
@@ -49,25 +45,30 @@ describe EmailValidator do
|
|
49
45
|
"nigel.worthington@big.co.uk",
|
50
46
|
"f@c.com",
|
51
47
|
"areallylongnameaasdfasdfasdfasdf@asdfasdfasdfasdfasdf.ab.cd.ef.gh.co.ca",
|
52
|
-
"ящик@яндекс.рф"
|
48
|
+
"ящик@яндекс.рф",
|
49
|
+
"test@test.testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttes",
|
50
|
+
"hans,peter@example.com",
|
51
|
+
"hans(peter@example.com",
|
52
|
+
"hans)peter@example.com",
|
53
|
+
"partially.\"quoted\"@sld.com",
|
54
|
+
"&'*+-./=?^_{}~@other-valid-characters-in-local.net",
|
55
|
+
"mixed-1234-in-{+^}-local@sld.net",
|
56
|
+
"user@domain.рф",
|
57
|
+
"寿司@example.com"
|
53
58
|
].each do |email|
|
54
59
|
|
55
60
|
it "#{email.inspect} should be valid" do
|
56
61
|
expect(TestUser.new(:email => email)).to be_valid
|
57
62
|
end
|
58
63
|
|
59
|
-
it "#{email.inspect} should be
|
60
|
-
expect(
|
64
|
+
it "#{email.inspect} should not be invalid" do
|
65
|
+
expect(TestUser.new(:email => email)).not_to be_invalid
|
61
66
|
end
|
62
67
|
|
63
68
|
it "#{email.inspect} should match the regexp" do
|
64
69
|
expect(email =~ EmailValidator.regexp).to be_truthy
|
65
70
|
end
|
66
71
|
|
67
|
-
it "#{email.inspect} should match the strict regexp" do
|
68
|
-
expect(email =~ EmailValidator.regexp(:strict_mode => true)).to be_truthy
|
69
|
-
end
|
70
|
-
|
71
72
|
it "#{email.inspect} should pass the class tester" do
|
72
73
|
expect(EmailValidator.valid?(email)).to be_truthy
|
73
74
|
end
|
@@ -79,80 +80,32 @@ describe EmailValidator do
|
|
79
80
|
context "given the invalid emails" do
|
80
81
|
[
|
81
82
|
"",
|
82
|
-
"f@s",
|
83
|
-
"f@s.c",
|
84
83
|
"@bar.com",
|
85
|
-
"
|
84
|
+
" @bar.com",
|
86
85
|
"test@",
|
87
|
-
"@
|
88
|
-
"a b@space-in-local.com",
|
89
|
-
"! \#$%\`|@invalid-characters-in-local.org",
|
90
|
-
"<>@[]\`|@even-more-invalid-characters-in-local.org",
|
91
|
-
"missing-sld@.com",
|
92
|
-
"invalid-characters-in-sld@! \"\#$%(),/;<>_[]\`|.org",
|
93
|
-
"missing-dot-before-tld@com",
|
94
|
-
"missing-tld@sld.",
|
86
|
+
"test@ ",
|
95
87
|
" ",
|
96
88
|
"missing-at-sign.net",
|
97
|
-
"unbracketed-IP@127.0.0.1",
|
98
|
-
"invalid-ip@127.0.0.1.26",
|
99
|
-
"another-invalid-ip@127.0.0.256",
|
100
|
-
"IP-and-port@127.0.0.1:25",
|
101
|
-
"the-local-part-is-invalid-if-it-is-longer-than-sixty-four-characters@sld.net",
|
102
|
-
"user@example.com\n<script>alert('hello')</script>"
|
103
89
|
].each do |email|
|
104
90
|
|
105
91
|
it "#{email.inspect} should not be valid" do
|
106
92
|
expect(TestUser.new(:email => email)).not_to be_valid
|
107
93
|
end
|
108
94
|
|
109
|
-
it "#{email.inspect} should
|
110
|
-
expect(
|
95
|
+
it "#{email.inspect} should be invalid" do
|
96
|
+
expect(TestUser.new(:email => email)).to be_invalid
|
111
97
|
end
|
112
98
|
|
113
99
|
it "#{email.inspect} should not match the regexp" do
|
114
100
|
expect(email =~ EmailValidator.regexp).to be_falsy
|
115
101
|
end
|
116
102
|
|
117
|
-
it "#{email.inspect} should not match the strict regexp" do
|
118
|
-
expect(email =~ EmailValidator.regexp(:strict_mode => true)).to be_falsy
|
119
|
-
end
|
120
|
-
|
121
103
|
it "#{email.inspect} should fail the class tester" do
|
122
104
|
expect(EmailValidator.valid?(email)).to be_falsy
|
123
105
|
end
|
124
106
|
|
125
107
|
end
|
126
108
|
end
|
127
|
-
|
128
|
-
context "given the emails that should be invalid in strict_mode but valid in normal mode" do
|
129
|
-
[
|
130
|
-
"hans,peter@example.com",
|
131
|
-
"hans(peter@example.com",
|
132
|
-
"hans)peter@example.com",
|
133
|
-
"partially.\"quoted\"@sld.com",
|
134
|
-
"&'*+-./=?^_{}~@other-valid-characters-in-local.net",
|
135
|
-
"mixed-1234-in-{+^}-local@sld.net"
|
136
|
-
].each do |email|
|
137
|
-
|
138
|
-
it "#{email.inspect} should be valid" do
|
139
|
-
expect(TestUser.new(:email => email)).to be_valid
|
140
|
-
end
|
141
|
-
|
142
|
-
it "#{email.inspect} should not be valid in strict_mode" do
|
143
|
-
expect(StrictUser.new(:email => email)).not_to be_valid
|
144
|
-
end
|
145
|
-
|
146
|
-
it "#{email.inspect} should match the regexp" do
|
147
|
-
expect(email =~ EmailValidator.regexp).to be_truthy
|
148
|
-
end
|
149
|
-
|
150
|
-
it "#{email.inspect} should not match the strict regexp" do
|
151
|
-
expect(email =~ EmailValidator.regexp(:strict_mode => true)).to be_falsy
|
152
|
-
end
|
153
|
-
|
154
|
-
end
|
155
|
-
end
|
156
109
|
end
|
157
110
|
|
158
111
|
describe "error messages" do
|
@@ -188,14 +141,4 @@ describe EmailValidator do
|
|
188
141
|
expect(TestUserAllowsNilFalse.new(:email => nil)).not_to be_valid
|
189
142
|
end
|
190
143
|
end
|
191
|
-
|
192
|
-
describe "default_options" do
|
193
|
-
context "when 'email_validator/strict' has been required" do
|
194
|
-
before { require 'email_validator/strict' }
|
195
|
-
|
196
|
-
it "should validate using strict mode" do
|
197
|
-
expect(TestUser.new(:email => "&'*+-./=?^_{}~@other-valid-characters-in-local.net")).not_to be_valid
|
198
|
-
end
|
199
|
-
end
|
200
|
-
end
|
201
144
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: email_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Alexander
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-03-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description:
|
55
|
+
description: Email validator for Rails and ActiveModel.
|
56
56
|
email: balexand@gmail.com
|
57
57
|
executables: []
|
58
58
|
extensions: []
|
@@ -90,9 +90,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
90
|
- !ruby/object:Gem::Version
|
91
91
|
version: '0'
|
92
92
|
requirements: []
|
93
|
-
|
94
|
-
rubygems_version: 2.4.5
|
93
|
+
rubygems_version: 3.0.1
|
95
94
|
signing_key:
|
96
95
|
specification_version: 4
|
97
|
-
summary: An email validator for Rails
|
96
|
+
summary: An email validator for Rails and ActiveModel.
|
98
97
|
test_files: []
|