email_validator 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{email_validator}
3
- s.version = "1.2.1"
3
+ s.version = "1.2.2"
4
4
  s.authors = ["Brian Alexander"]
5
5
  s.date = %q{2011-07-23}
6
6
  s.description = %q{An email validator for Rails 3. See homepage for details: http://github.com/balexand/email_validator}
@@ -1,9 +1,11 @@
1
1
  # Based on work from http://thelucid.com/2010/01/08/sexy-validation-in-edge-rails-rails-3/
2
2
  class EmailValidator < ActiveModel::EachValidator
3
+ VALID_EMAIL_PATTERN = /^([a-z0-9!\#$%&'*+\/=?^_`{|}~-]+(\.[a-z0-9!\#$%&'*+\/=?^_`{|}~-]+)*|"([\040-\041\043-\133\135-\176]|\134[\040-\176])*")@(\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]|((((([a-z0-9]{1}[a-z0-9-]+[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6}))|[a-z0-9]+\.xn\-\-[a-f0-9]+)$/i
4
+
3
5
  def validate_each(record, attribute, value)
4
- address, domain = value ? value.split('@') : ["", ""]
6
+ address, domain = value ? value.split('@', 2) : ["", ""]
5
7
 
6
- unless address.size <= 64 and domain.to_s.size <= 254 and value =~ /^([a-z0-9!\#$%&'*+\/=?^_`{|}~-]+(\.[a-z0-9!\#$%&'*+\/=?^_`{|}~-]+)*|"([\040-\041\043-\133\135-\176]|\134[\040-\176])*")@(\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]|((((([a-z0-9]{1}[a-z0-9-]+[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6}))|[a-z0-9]+\.xn\-\-[a-f0-9]+)$/i
8
+ unless address and domain and address.size <= 64 and domain.to_s.size <= 254 and value =~ VALID_EMAIL_PATTERN
7
9
  record.errors.add(attribute, options[:message] || :invalid)
8
10
  end
9
11
  end
@@ -4,6 +4,14 @@ class TestUser < TestModel
4
4
  validates :email, :email => true
5
5
  end
6
6
 
7
+ class TestUserAllowsNil < TestModel
8
+ validates :email, :email => {:allow_nil => true}
9
+ end
10
+
11
+ class TestUserAllowsNilFalse < TestModel
12
+ validates :email, :email => {:allow_nil => false}
13
+ end
14
+
7
15
  class TestUserWithMessage < TestModel
8
16
  validates :email_address, :email => {:message => 'is not looking very good!'}
9
17
  end
@@ -46,7 +54,8 @@ describe EmailValidator do
46
54
  end
47
55
 
48
56
  context "given the invalid emails" do
49
- [ nil,
57
+ [ "",
58
+ "test@example.com@example.com",
50
59
  "@missing-local.org",
51
60
  "! \#$%\`|@invalid-characters-in-local.org",
52
61
  "(),:;\`|@more-invalid-characters-in-local.org",
@@ -97,4 +106,18 @@ describe EmailValidator do
97
106
  end
98
107
  end
99
108
  end
109
+
110
+ describe "nil email" do
111
+ it "should not be valid when :allow_nil option is missing" do
112
+ TestUser.new(:email => nil).should_not be_valid
113
+ end
114
+
115
+ it "should be valid when :allow_nil options is set to true" do
116
+ TestUserAllowsNil.new(:email => nil).should be_valid
117
+ end
118
+
119
+ it "should not be valid when :allow_nil option is set to false" do
120
+ TestUserAllowsNilFalse.new(:email => nil).should_not be_valid
121
+ end
122
+ end
100
123
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: email_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activemodel
17
- requirement: &70178441880860 !ruby/object:Gem::Requirement
17
+ requirement: &70356959914220 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70178441880860
25
+ version_requirements: *70356959914220
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
- requirement: &70178441880380 !ruby/object:Gem::Requirement
28
+ requirement: &70356959913740 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70178441880380
36
+ version_requirements: *70356959913740
37
37
  description: ! 'An email validator for Rails 3. See homepage for details: http://github.com/balexand/email_validator'
38
38
  email: balexand@gmail.com
39
39
  executables: []