validates_email_format_of 1.5.3 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/.gitignore +8 -0
- data/.rspec +2 -0
- data/.travis.yml +18 -0
- data/Gemfile +3 -0
- data/README.rdoc +51 -48
- data/config/locales/en.yml +6 -0
- data/config/locales/pl.yml +6 -0
- data/gemfiles/Gemfile.active_model_2_1 +5 -0
- data/gemfiles/Gemfile.active_model_3_0 +5 -0
- data/gemfiles/Gemfile.active_model_3_1 +5 -0
- data/gemfiles/Gemfile.active_model_3_2 +5 -0
- data/gemfiles/Gemfile.active_model_3_3 +5 -0
- data/gemfiles/Gemfile.active_model_4_0 +5 -0
- data/gemfiles/Gemfile.active_model_4_1 +5 -0
- data/lib/validates_email_format_of.rb +35 -78
- data/lib/validates_email_format_of/active_model.rb +24 -0
- data/lib/validates_email_format_of/railtie.rb +7 -0
- data/lib/validates_email_format_of/rspec_matcher.rb +15 -0
- data/lib/validates_email_format_of/version.rb +3 -0
- data/rakefile.rb +1 -27
- data/spec/rspec_matcher_spec.rb +14 -0
- data/spec/spec_helper.rb +29 -0
- data/spec/validates_email_format_of_spec.rb +308 -0
- data/validates_email_format_of.gemspec +21 -0
- metadata +65 -39
- data/init.rb +0 -1
- data/test/database.yml +0 -3
- data/test/fixtures/people.yml +0 -3
- data/test/fixtures/person.rb +0 -33
- data/test/schema.rb +0 -5
- data/test/test_helper.rb +0 -38
- data/test/validates_email_format_of_test.rb +0 -242
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
Nzc3NWIyMDUwNDM0NWRiMTViYmE2MjU4MWNiNjQwM2I1MjBiYTZiYw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MjJiOTNiNzBiNjcyZTlkY2I3NTZjMmRlZjcwZTZiYTNiNDg4ZmE2Yg==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NDk4ZmFjYzgzN2Y1N2FiOTE1ZjUwZGU0YjQzOTY4YzE3ZTY5NTYyZDVlNDIw
|
10
|
+
YTRmN2JjYzQ3N2M3ZDZhOTc4OTU3YjFjMWMyNjY4Mzg5YjkyNmM3ZjFlZWYw
|
11
|
+
ZDlhNzQ2MTIwNGYyODJiMDA0OWQxODM0ZmM0OGEwMzliMmFlYTI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
N2Q1OTQyMjQ4ZWFkMWMzODU2MmMxMTc0MmZmYzhhMzY2ZTU3ZjhkMWQzZDY2
|
14
|
+
Y2VmYjZmMzRkY2I4NzEzYjAwN2Y4YmZmMjIzM2U0MDkwNjMzNDBkZWRkMzFj
|
15
|
+
NjU4NTMzODU3OTkyMzRiMTM5OWVhNzc5ZWUxZGUyZWJkYjAyMTA=
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
rvm:
|
2
|
+
- 1.8.7
|
3
|
+
- 1.9.2
|
4
|
+
- 1.9.3
|
5
|
+
- 2.0.0
|
6
|
+
- 2.1.2
|
7
|
+
- jruby
|
8
|
+
- ree
|
9
|
+
gemfile:
|
10
|
+
- Gemfile
|
11
|
+
- gemfiles/active_model_2_1
|
12
|
+
- gemfiles/active_model_3_0
|
13
|
+
- gemfiles/active_model_3_1
|
14
|
+
- gemfiles/active_model_3_2
|
15
|
+
- gemfiles/active_model_4_0
|
16
|
+
- gemfiles/active_model_4_1
|
17
|
+
install: bundle install
|
18
|
+
script: bundle exec rspec
|
data/Gemfile
ADDED
data/README.rdoc
CHANGED
@@ -8,75 +8,76 @@ Installing as a gem:
|
|
8
8
|
|
9
9
|
gem install validates_email_format_of
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
./script/plugin install http://github.com/alexdunae/validates_email_format_of.git
|
14
|
-
|
15
|
-
Or in your Rails 3 Gemfile
|
16
|
-
|
17
|
-
gem 'validates_email_format_of', :git => 'git://github.com/alexdunae/validates_email_format_of.git'
|
11
|
+
Or in your Gemfile:
|
18
12
|
|
13
|
+
gem 'validates_email_format_of'
|
19
14
|
|
20
15
|
== Usage
|
21
16
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
As of version 1.4, it's possible to run e-mail validation tests (including MX
|
32
|
-
checks) without using ActiveRecord or even touching a database. The
|
33
|
-
<tt>validate_email_format</tt> method will return <tt>nil</tt> on a valid
|
34
|
-
e-mail address or an array of error messages for invalid addresses.
|
35
|
-
|
36
|
-
results = ValidatesEmailFormatOf::validate_email_format(email, options)
|
17
|
+
# Rails
|
18
|
+
# I18n locales are loaded automatically.
|
19
|
+
class Person < ActiveRecord::Base
|
20
|
+
validates_email_format_of :email, :message => 'is not looking good'
|
21
|
+
# OR
|
22
|
+
validates :email, :email_format => { :message => 'is not looking good' }
|
23
|
+
end
|
37
24
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
25
|
+
# Now you can test your model using RSpec:
|
26
|
+
require "validates_email_format_of/rspec_matcher"
|
27
|
+
describe Person do
|
28
|
+
it { should validate_email_format_of(:email).with_message('is not looking good') }
|
42
29
|
end
|
43
30
|
|
31
|
+
# If you're not using Rails (which really means, if you're not using ActiveModel::Validations)
|
32
|
+
ValidatesEmailFormatOf::load_i18n_locales # Optional, if you want error messages to be in your language
|
33
|
+
I18n.locale = :pl # If, for example, you want Polish error messages.
|
34
|
+
ValidatesEmailFormatOf::validate_email_format("example@mydomain.com") # => nil
|
35
|
+
ValidatesEmailFormatOf::validate_email_format("invalid_because_there_is_no_at_symbol") # => ["does not appear to be a valid e-mail address"]
|
44
36
|
|
45
37
|
=== Options
|
46
38
|
|
47
39
|
:message
|
48
|
-
String. A custom error message (default is: "
|
49
|
-
:on
|
50
|
-
Symbol. Specifies when this validation is active (default is :save, other options :create, :update)
|
51
|
-
:allow_nil
|
52
|
-
Boolean. Allow nil values (default is false)
|
53
|
-
:allow_blank
|
54
|
-
Boolean. Allow blank values (default is false)
|
40
|
+
String. A custom error message when the email format is invalid (default is: "does not appear to be a valid e-mail address")
|
55
41
|
:check_mx
|
56
42
|
Boolean. Check domain for a valid MX record (default is false)
|
57
|
-
:
|
58
|
-
|
59
|
-
(e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The method,
|
60
|
-
proc or string should return or evaluate to a true or false value.
|
61
|
-
:unless
|
62
|
-
See :if option.
|
43
|
+
:mx_message
|
44
|
+
String. A custom error message when the domain does not match a valid MX record (default is: "is not routable"). Ignored unless :check_mx option is true.
|
63
45
|
:local_length
|
64
|
-
Maximum number of characters allowed in the local part (default is 64)
|
46
|
+
Maximum number of characters allowed in the local part (everything before the '@') (default is 64)
|
65
47
|
:domain_length
|
66
|
-
Maximum number of characters allowed in the domain part (default is 255)
|
67
|
-
|
48
|
+
Maximum number of characters allowed in the domain part (everything after the '@') (default is 255)
|
49
|
+
:with
|
50
|
+
Specify a custom Regex as the valid email format.
|
51
|
+
:on, :if, :unless, :allow_nil, :allow_blank, :strict
|
52
|
+
Standard ActiveModel validation options. These work in the ActiveModel/ActiveRecord/Rails syntax only.
|
53
|
+
See http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates for details.
|
68
54
|
|
69
55
|
== Testing
|
70
56
|
|
71
|
-
To execute the unit tests run <tt>
|
57
|
+
To execute the unit tests run <tt>rspec</tt>.
|
58
|
+
|
59
|
+
Tested in Ruby 1.8.7, 1.9.2, 1.9.3, 2.0.0, 2.1.2, JRuby and REE 1.8.7.
|
72
60
|
|
73
|
-
|
61
|
+
== Contributing
|
74
62
|
|
75
|
-
|
63
|
+
If you think we're letting some rules about valid email formats slip through the cracks, don't just update the Regex.
|
64
|
+
Instead, add a failing test, and demonstrate that the described email address should be treated differently. A link to an appropriate RFC is the best way to do this.
|
65
|
+
Then change the gem code to make the test pass.
|
76
66
|
|
77
|
-
|
67
|
+
describe "i_think_this_is_not_a_v@lid_email_addre.ss" do
|
68
|
+
# According to http://..., this email address IS NOT valid.
|
69
|
+
it { should have_errors_on_email.because("does not appear to be valid") }
|
70
|
+
end
|
71
|
+
describe "i_think_this_is_a_v@lid_email_addre.ss" do
|
72
|
+
# According to http://..., this email address IS valid.
|
73
|
+
it { should_not have_errors_on_email }
|
74
|
+
end
|
78
75
|
|
79
|
-
|
76
|
+
Yes, our Rspec syntax is that simple!
|
77
|
+
|
78
|
+
== Homepage
|
79
|
+
|
80
|
+
* https://github.com/validates-email-format-of/validates_email_format_of
|
80
81
|
|
81
82
|
== Credits
|
82
83
|
|
@@ -90,4 +91,6 @@ Thanks to Travis Sinnott for creating the 1.3 update.
|
|
90
91
|
|
91
92
|
Thanks to Denis Ahearn at Riverock Technologies (http://www.riverocktech.com/) for creating the 1.4 update.
|
92
93
|
|
93
|
-
Thanks to George Anderson (http://github.com/george) and 'history' (http://github.com/history) for creating the 1.4.1 update.
|
94
|
+
Thanks to George Anderson (http://github.com/george) and 'history' (http://github.com/history) for creating the 1.4.1 update.
|
95
|
+
|
96
|
+
Thanks to Isaac Betesh (https://github.com/betesh) for converting tests to Rspec and refactoring for version 1.6.0.
|
@@ -1,15 +1,18 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'resolv'
|
2
|
+
require 'validates_email_format_of/version'
|
4
3
|
|
5
|
-
|
4
|
+
module ValidatesEmailFormatOf
|
5
|
+
def self.load_i18n_locales
|
6
|
+
require 'i18n'
|
7
|
+
I18n.load_path += Dir.glob(File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'locales', '*.yml')))
|
8
|
+
end
|
6
9
|
|
7
|
-
|
10
|
+
require 'resolv'
|
8
11
|
|
9
12
|
LocalPartSpecialChars = /[\!\#\$\%\&\'\*\-\/\=\?\+\-\^\_\`\{\|\}\~]/
|
10
13
|
|
11
14
|
def self.validate_email_domain(email)
|
12
|
-
domain = email.match(/\@(.+)/)[1]
|
15
|
+
domain = email.to_s.downcase.match(/\@(.+)/)[1]
|
13
16
|
Resolv::DNS.open do |dns|
|
14
17
|
@mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX) + dns.getresources(domain, Resolv::DNS::Resource::IN::A)
|
15
18
|
end
|
@@ -26,10 +29,16 @@ module ValidatesEmailFormatOf
|
|
26
29
|
# * <tt>with</tt> The regex to use for validating the format of the email address (deprecated)
|
27
30
|
# * <tt>local_length</tt> Maximum number of characters allowed in the local part (default is 64)
|
28
31
|
# * <tt>domain_length</tt> Maximum number of characters allowed in the domain part (default is 255)
|
32
|
+
DEFAULT_MESSAGE = "does not appear to be valid"
|
33
|
+
DEFAULT_MX_MESSAGE = "is not routable"
|
34
|
+
def self.default_message
|
35
|
+
defined?(I18n) ? I18n.t(:invalid_email_address, :scope => [:activemodel, :errors, :messages], :default => DEFAULT_MESSAGE) : DEFAULT_MESSAGE
|
36
|
+
end
|
37
|
+
|
29
38
|
def self.validate_email_format(email, options={})
|
30
|
-
default_options = { :message =>
|
39
|
+
default_options = { :message => default_message,
|
31
40
|
:check_mx => false,
|
32
|
-
:mx_message => I18n.t(:email_address_not_routable, :scope => [
|
41
|
+
:mx_message => defined?(I18n) ? I18n.t(:email_address_not_routable, :scope => [:activemodel, :errors, :messages], :default => DEFAULT_MX_MESSAGE) : DEFAULT_MX_MESSAGE,
|
33
42
|
:domain_length => 255,
|
34
43
|
:local_length => 64
|
35
44
|
}
|
@@ -64,7 +73,7 @@ module ValidatesEmailFormatOf
|
|
64
73
|
|
65
74
|
return nil # represents no validation errors
|
66
75
|
end
|
67
|
-
|
76
|
+
|
68
77
|
|
69
78
|
def self.validate_local_part_syntax(local)
|
70
79
|
in_quoted_pair = false
|
@@ -74,27 +83,27 @@ module ValidatesEmailFormatOf
|
|
74
83
|
ord = local[i].ord
|
75
84
|
|
76
85
|
# accept anything if it's got a backslash before it
|
77
|
-
if in_quoted_pair
|
86
|
+
if in_quoted_pair
|
78
87
|
in_quoted_pair = false
|
79
88
|
next
|
80
89
|
end
|
81
|
-
|
90
|
+
|
82
91
|
# backslash signifies the start of a quoted pair
|
83
92
|
if ord == 92 and i < local.length - 1
|
84
93
|
return false if not in_quoted_string # must be in quoted string per http://www.rfc-editor.org/errata_search.php?rfc=3696
|
85
94
|
in_quoted_pair = true
|
86
95
|
next
|
87
96
|
end
|
88
|
-
|
97
|
+
|
89
98
|
# double quote delimits quoted strings
|
90
99
|
if ord == 34
|
91
100
|
in_quoted_string = !in_quoted_string
|
92
101
|
next
|
93
|
-
end
|
102
|
+
end
|
94
103
|
|
95
104
|
next if local[i,1] =~ /[a-z0-9]/i
|
96
105
|
next if local[i,1] =~ LocalPartSpecialChars
|
97
|
-
|
106
|
+
|
98
107
|
# period must be followed by something
|
99
108
|
if ord == 46
|
100
109
|
return false if i == 0 or i == local.length - 1 # can't be first or last char
|
@@ -103,86 +112,34 @@ module ValidatesEmailFormatOf
|
|
103
112
|
|
104
113
|
return false
|
105
114
|
end
|
106
|
-
|
115
|
+
|
107
116
|
return false if in_quoted_string # unbalanced quotes
|
108
117
|
|
109
118
|
return true
|
110
119
|
end
|
111
|
-
|
120
|
+
|
112
121
|
def self.validate_domain_part_syntax(domain)
|
113
122
|
parts = domain.downcase.split('.', -1)
|
114
|
-
|
123
|
+
|
115
124
|
return false if parts.length <= 1 # Only one domain part
|
116
125
|
|
117
126
|
# Empty parts (double period) or invalid chars
|
118
|
-
return false if parts.any? {
|
119
|
-
|part|
|
120
|
-
part.nil? or
|
121
|
-
part.empty? or
|
127
|
+
return false if parts.any? {
|
128
|
+
|part|
|
129
|
+
part.nil? or
|
130
|
+
part.empty? or
|
122
131
|
not part =~ /\A[[:alnum:]\-]+\Z/ or
|
123
132
|
part[0,1] == '-' or part[-1,1] == '-' # hyphen at beginning or end of part
|
124
|
-
}
|
125
|
-
|
133
|
+
}
|
134
|
+
|
126
135
|
# ipv4
|
127
136
|
return true if parts.length == 4 and parts.all? { |part| part =~ /\A[0-9]+\Z/ and part.to_i.between?(0, 255) }
|
128
|
-
|
129
|
-
return false if parts[-1].length < 2 or not parts[-1] =~ /[a-z\-]/ # TLD is too short or does not contain a char or hyphen
|
130
|
-
|
131
|
-
return true
|
132
|
-
end
|
133
137
|
|
134
|
-
|
135
|
-
# Validates whether the value of the specified attribute is a valid email address
|
136
|
-
#
|
137
|
-
# class User < ActiveRecord::Base
|
138
|
-
# validates_email_format_of :email, :on => :create
|
139
|
-
# end
|
140
|
-
#
|
141
|
-
# Configuration options:
|
142
|
-
# * <tt>message</tt> - A custom error message (default is: "does not appear to be valid")
|
143
|
-
# * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update)
|
144
|
-
# * <tt>allow_nil</tt> - Allow nil values (default is false)
|
145
|
-
# * <tt>allow_blank</tt> - Allow blank values (default is false)
|
146
|
-
# * <tt>check_mx</tt> - Check for MX records (default is false)
|
147
|
-
# * <tt>mx_message</tt> - A custom error message when an MX record validation fails (default is: "is not routable.")
|
148
|
-
# * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should
|
149
|
-
# occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
|
150
|
-
# method, proc or string should return or evaluate to a true or false value.
|
151
|
-
# * <tt>unless</tt> - See <tt>:if</tt>
|
152
|
-
def validates_email_format_of(*attr_names)
|
153
|
-
options = { :on => :save,
|
154
|
-
:allow_nil => false,
|
155
|
-
:allow_blank => false }
|
156
|
-
options.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
157
|
-
|
158
|
-
validates_each(attr_names, options) do |record, attr_name, value|
|
159
|
-
v = value.to_s
|
160
|
-
errors = ValidatesEmailFormatOf::validate_email_format(v, options)
|
161
|
-
errors.each do |error|
|
162
|
-
record.errors.add(attr_name, error)
|
163
|
-
end unless errors.nil?
|
164
|
-
end
|
165
|
-
end
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
if defined?(ActiveModel)
|
170
|
-
class EmailFormatValidator < ActiveModel::EachValidator
|
171
|
-
def validate_each(record, attribute, value)
|
172
|
-
err = ValidatesEmailFormatOf::validate_email_format(value, options)
|
173
|
-
record.errors[attribute] << err unless err.nil?
|
174
|
-
record.errors[attribute].flatten!
|
175
|
-
end
|
176
|
-
end
|
138
|
+
return false if parts[-1].length < 2 or not parts[-1] =~ /[a-z\-]/ # TLD is too short or does not contain a char or hyphen
|
177
139
|
|
178
|
-
|
179
|
-
def validates_email_format_of(*attr_names)
|
180
|
-
validates_with EmailFormatValidator, _merge_attributes(attr_names)
|
181
|
-
end
|
182
|
-
end
|
183
|
-
else
|
184
|
-
class ActiveRecord::Base
|
185
|
-
extend ValidatesEmailFormatOf::Validations
|
140
|
+
return true
|
186
141
|
end
|
187
142
|
end
|
188
143
|
|
144
|
+
require 'validates_email_format_of/active_model' if defined?(::ActiveModel) && !(ActiveModel::VERSION::MAJOR < 2 || (2 == ActiveModel::VERSION::MAJOR && ActiveModel::VERSION::MINOR < 1))
|
145
|
+
require 'validates_email_format_of/railtie' if defined?(::Rails)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'validates_email_format_of'
|
2
|
+
require 'active_model'
|
3
|
+
|
4
|
+
if ActiveModel::VERSION::MAJOR < 2 || (2 == ActiveModel::VERSION::MAJOR && ActiveModel::VERSION::MINOR < 1)
|
5
|
+
puts "WARNING: ActiveModel validation helper methods in validates_email_format_of gem are not compatible with ActiveModel < 2.1.0. Please use ValidatesEmailFormatOf::validate_email_format(email, options) or upgrade ActiveModel"
|
6
|
+
end
|
7
|
+
|
8
|
+
module ActiveModel
|
9
|
+
module Validations
|
10
|
+
class EmailFormatValidator < EachValidator
|
11
|
+
def validate_each(record, attribute, value)
|
12
|
+
(ValidatesEmailFormatOf::validate_email_format(value, options) || []).each do |error|
|
13
|
+
record.errors.add(attribute, error)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module HelperMethods
|
19
|
+
def validates_email_format_of(*attr_names)
|
20
|
+
validates_with EmailFormatValidator, _merge_attributes(attr_names)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|