email_format_validator 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +10 -5
- data/email_format_validator.gemspec +1 -1
- data/lib/email_format_validator.rb +13 -6
- data/test/email_format_validator_test.rb +52 -45
- metadata +4 -4
data/README.md
CHANGED
@@ -14,24 +14,24 @@ Usage
|
|
14
14
|
|
15
15
|
Add the following to one of your models:
|
16
16
|
|
17
|
-
validates :email, :
|
17
|
+
validates :email, email_format: true
|
18
18
|
|
19
19
|
You can turn on RFC 822 compliant email verification by passing on:
|
20
20
|
|
21
|
-
validates :email, :
|
21
|
+
validates :email, email_format: { rfc: true }
|
22
22
|
|
23
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
24
|
|
25
25
|
You can also modify the default message ("is improperly formatted") if validation fails:
|
26
26
|
|
27
|
-
validates :email, :
|
27
|
+
validates :email, email_format: { message: "is not well formatted" }
|
28
28
|
|
29
29
|
I18n
|
30
30
|
----
|
31
31
|
|
32
32
|
If you use I18n, the default key to translate is :improperly_formatted. So if you add to your User model:
|
33
33
|
|
34
|
-
validates :email, :
|
34
|
+
validates :email, email_format: true
|
35
35
|
|
36
36
|
You can translate (or overload) the default message via for e.g. (in english): "en.activerecord.errors.models.user.attributes.email.improperly_formatted"
|
37
37
|
|
@@ -40,7 +40,12 @@ Tests
|
|
40
40
|
|
41
41
|
cd test
|
42
42
|
ruby email_format_validator_test.rb
|
43
|
-
|
43
|
+
|
44
|
+
Compatibility
|
45
|
+
-------------
|
46
|
+
|
47
|
+
Ruby 1.8 is not supported.
|
48
|
+
|
44
49
|
Credits
|
45
50
|
-------
|
46
51
|
|
@@ -3,7 +3,7 @@ $:.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 = "1.0.
|
6
|
+
s.version = "1.0.4"
|
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"
|
@@ -23,13 +23,20 @@ class EmailFormatValidator < ActiveModel::EachValidator
|
|
23
23
|
def validate_each(record, attribute, value)
|
24
24
|
pattern_choosed = options[:rfc] ? Patterns::EMAIL : Patterns::LESS_RFC_COMPLIANT_EMAIL
|
25
25
|
unless value =~ pattern_choosed
|
26
|
-
record.errors.add(
|
27
|
-
attribute,
|
28
|
-
options[:message] || I18n.t(:improperly_formatted,
|
29
|
-
:scope => "#{record.class.i18n_scope}.errors.models.#{record.class.model_name.i18n_key}.attributes.#{attribute}",
|
30
|
-
:default => "is improperly formatted")
|
31
|
-
)
|
26
|
+
record.errors.add(attribute, options[:message] || invalid_message(record, attribute))
|
32
27
|
end
|
33
28
|
end
|
34
29
|
|
30
|
+
#######################
|
31
|
+
### Private methods ###
|
32
|
+
#######################
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def invalid_message(record, attribute)
|
37
|
+
I18n.t :improperly_formatted,
|
38
|
+
scope: "#{record.class.i18n_scope}.errors.models.#{record.class.model_name.i18n_key}.attributes.#{attribute}",
|
39
|
+
default: "is improperly formatted"
|
40
|
+
end
|
41
|
+
|
35
42
|
end
|
@@ -3,26 +3,73 @@
|
|
3
3
|
require_relative 'test_helper'
|
4
4
|
|
5
5
|
class TestUser < TestModel
|
6
|
-
validates :email, :
|
6
|
+
validates :email, email_format: true
|
7
7
|
end
|
8
8
|
|
9
9
|
class TestUserWithRfcCompliantEmail < TestModel
|
10
|
-
validates :email, :
|
10
|
+
validates :email, email_format: { rfc: true }
|
11
11
|
end
|
12
12
|
|
13
13
|
class TestUserAllowsNilToTrue < TestModel
|
14
|
-
validates :email, :
|
14
|
+
validates :email, email_format: { allow_nil: true }
|
15
15
|
end
|
16
16
|
|
17
17
|
class TestUserAllowsNilToFalse < TestModel
|
18
|
-
validates :email, :
|
18
|
+
validates :email, email_format: { allow_nil: false }
|
19
19
|
end
|
20
20
|
|
21
21
|
class TestUserWithMessage < TestModel
|
22
|
-
validates :email, :
|
22
|
+
validates :email, email_format: { message: 'is not well formatted' }
|
23
23
|
end
|
24
24
|
|
25
25
|
class TestEmailFormatValidator < MiniTest::Unit::TestCase
|
26
|
+
|
27
|
+
def test_valid_emails
|
28
|
+
valid_emails.each { |email| assert TestUser.new(email: email).valid? }
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_invalid_emails
|
32
|
+
invalid_emails.each { |email| refute TestUser.new(email: email).valid? }
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_valid_emails_with_rfc_compliant
|
36
|
+
rfc_valid_emails.each { |email| assert TestUserWithRfcCompliantEmail.new(email: email).valid? }
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_invalid_emails_with_rfc_compliant
|
40
|
+
rfc_invalid_emails.each { |email| refute TestUserWithRfcCompliantEmail.new(email: email).valid? }
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_default_message_on_error
|
44
|
+
test_user = TestUser.new(email: "invalid_email@")
|
45
|
+
refute test_user.valid?
|
46
|
+
assert test_user.errors[:email].include?("is improperly formatted")
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_custom_message_on_error
|
50
|
+
test_user = TestUserWithMessage.new(email: "invalid_email@")
|
51
|
+
refute test_user.valid?
|
52
|
+
assert test_user.errors[:email].include?("is not well formatted")
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_nil_email_when_allow_nil_option_is_not_set
|
56
|
+
refute TestUser.new(email: nil).valid?
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_nil_email_when_allow_nil_option_is_set_to_true
|
60
|
+
assert TestUserAllowsNilToTrue.new(email: nil).valid?
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_nil_email_when_allow_nil_option_is_set_to_false
|
64
|
+
refute TestUserAllowsNilToFalse.new(email: nil).valid?
|
65
|
+
end
|
66
|
+
|
67
|
+
#######################
|
68
|
+
### Private methods ###
|
69
|
+
#######################
|
70
|
+
|
71
|
+
private
|
72
|
+
|
26
73
|
def rfc_valid_emails
|
27
74
|
[
|
28
75
|
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@letters-in-local.org',
|
@@ -153,44 +200,4 @@ class TestEmailFormatValidator < MiniTest::Unit::TestCase
|
|
153
200
|
]
|
154
201
|
end
|
155
202
|
|
156
|
-
def test_valid_emails
|
157
|
-
valid_emails.each { |email| assert TestUser.new(:email => email).valid? }
|
158
|
-
end
|
159
|
-
|
160
|
-
def test_invalid_emails
|
161
|
-
invalid_emails.each { |email| refute TestUser.new(:email => email).valid? }
|
162
|
-
end
|
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
|
-
|
172
|
-
def test_default_message_on_error
|
173
|
-
test_user = TestUser.new(:email => "invalid_email@")
|
174
|
-
refute test_user.valid?
|
175
|
-
assert test_user.errors[:email].include?("is improperly formatted")
|
176
|
-
end
|
177
|
-
|
178
|
-
def test_custom_message_on_error
|
179
|
-
test_user = TestUserWithMessage.new(:email => "invalid_email@")
|
180
|
-
refute test_user.valid?
|
181
|
-
assert test_user.errors[:email].include?("is not well formatted")
|
182
|
-
end
|
183
|
-
|
184
|
-
def test_nil_email_when_allow_nil_option_is_not_set
|
185
|
-
refute TestUser.new(:email => nil).valid?
|
186
|
-
end
|
187
|
-
|
188
|
-
def test_nil_email_when_allow_nil_option_is_set_to_true
|
189
|
-
assert TestUserAllowsNilToTrue.new(:email => nil).valid?
|
190
|
-
end
|
191
|
-
|
192
|
-
def test_nil_email_when_allow_nil_option_is_set_to_false
|
193
|
-
refute TestUserAllowsNilToFalse.new(:email => nil).valid?
|
194
|
-
end
|
195
|
-
|
196
203
|
end
|
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: 1.0.
|
4
|
+
version: 1.0.4
|
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-15 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
16
|
-
requirement: &
|
16
|
+
requirement: &70357357707720 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70357357707720
|
25
25
|
description: A Rails 3 email format validator with RFC 822 compliant option
|
26
26
|
email:
|
27
27
|
- axel.vergult@gmail.com
|