validates_as_email_address 0.1.1 → 0.2.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.
- data/CHANGELOG.rdoc +4 -0
- data/Rakefile +1 -1
- data/lib/validates_as_email_address.rb +50 -52
- data/lib/validates_as_email_address/rfc_1035.rb +23 -25
- data/lib/validates_as_email_address/rfc_822.rb +31 -33
- data/test/unit/validates_as_email_address_test.rb +4 -4
- metadata +10 -10
data/CHANGELOG.rdoc
CHANGED
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ require 'rake/contrib/sshpublisher'
|
|
5
5
|
|
6
6
|
spec = Gem::Specification.new do |s|
|
7
7
|
s.name = 'validates_as_email_address'
|
8
|
-
s.version = '0.
|
8
|
+
s.version = '0.2.0'
|
9
9
|
s.platform = Gem::Platform::RUBY
|
10
10
|
s.summary = 'Adds support for validating the format/length of email addresses'
|
11
11
|
|
@@ -1,62 +1,60 @@
|
|
1
1
|
require 'validates_as_email_address/rfc_822'
|
2
2
|
require 'validates_as_email_address/rfc_1035'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
validates_length_of attr_names, length_configuration
|
54
|
-
end
|
4
|
+
# Adds validations for email addresses
|
5
|
+
module ValidatesAsEmailAddress
|
6
|
+
# Validates whether the value of the specific attribute matches against the
|
7
|
+
# RFC822/RFC1035 specification.
|
8
|
+
#
|
9
|
+
# class Person < ActiveRecord::Base
|
10
|
+
# validates_as_email_address :email, :on => :create
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# This will also validate that the email address is within the specification
|
14
|
+
# limits, i.e. between 3 and 320 characters in length.
|
15
|
+
#
|
16
|
+
# Configuration options for length:
|
17
|
+
# * +minimum+ - The minimum size of the attribute
|
18
|
+
# * +maximum+ - The maximum size of the attribute
|
19
|
+
# * +is+ - The exact size of the attribute
|
20
|
+
# * +within+ - A range specifying the minimum and maximum size of the attribute
|
21
|
+
# * +in+ - A synonym(or alias) for :within
|
22
|
+
# * +too_long+ - The error message if the attribute goes over the maximum (default is: "is too long (maximum is %d characters)")
|
23
|
+
# * +too_short+ - The error message if the attribute goes under the minimum (default is: "is too short (minimum is %d characters)")
|
24
|
+
# * +wrong_length+ - The error message if using the :is method and the attribute is the wrong size (default is: "is the wrong length (should be %d characters)")
|
25
|
+
#
|
26
|
+
# Configuration options for format:
|
27
|
+
# * +wrong_format+ - A custom error message (default is: "is an invalid email address")
|
28
|
+
#
|
29
|
+
# Miscellaneous configuration options:
|
30
|
+
# * +allow_nil+ - Attribute may be nil; skip validation.
|
31
|
+
# * +on+ - Specifies when this validation is active (default is :save, other options :create, :update)
|
32
|
+
# * +if+ - Specifies a method, proc or string to call to determine if the validation should
|
33
|
+
# occur (e.g. :if => :allow_validation, or :if => lambda { |user| user.signup_step > 2 }). The
|
34
|
+
# method, proc or string should return or evaluate to a true or false value.
|
35
|
+
# * +strict+ - Specifies if the domain part of the email should be compliant to RFC 1035 (default is true). If set to false domains such as '-online.com', '[127.0.0.1]' become valid.
|
36
|
+
def validates_as_email_address(*attr_names)
|
37
|
+
configuration = attr_names.last.is_a?(Hash) ? attr_names.pop : {}
|
38
|
+
configuration.reverse_merge!(
|
39
|
+
:wrong_format => ActiveRecord::Errors.default_error_messages[:invalid_email],
|
40
|
+
:strict => true
|
41
|
+
)
|
42
|
+
|
43
|
+
# Add format validation
|
44
|
+
format_configuration = configuration.dup
|
45
|
+
format_configuration[:message] = configuration.delete(:wrong_format)
|
46
|
+
format_configuration[:with] = configuration[:strict] ? RFC1035::EmailAddress : RFC822::EmailAddress
|
47
|
+
validates_format_of attr_names, format_configuration
|
48
|
+
|
49
|
+
# Add length validation
|
50
|
+
length_configuration = configuration.dup
|
51
|
+
length_configuration.reverse_merge!(:within => 3..320) unless ([:minimum, :maximum, :is, :within, :in] & configuration.keys).any?
|
52
|
+
validates_length_of attr_names, length_configuration
|
55
53
|
end
|
56
54
|
end
|
57
55
|
|
58
56
|
ActiveRecord::Base.class_eval do
|
59
|
-
extend
|
57
|
+
extend ValidatesAsEmailAddress
|
60
58
|
end
|
61
59
|
|
62
60
|
# Add error messages specific to this validation
|
@@ -1,29 +1,27 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
domain = "(?:#{subdomain}|\\x20)"
|
15
|
-
|
16
|
-
/#{domain}/
|
17
|
-
end
|
1
|
+
module ValidatesAsEmailAddress
|
2
|
+
# The standard describing the format of domains
|
3
|
+
module RFC1035
|
4
|
+
# Matches domain according to the RFC 1035 standard, with the exception
|
5
|
+
# that domains can start with a letter *or* digit
|
6
|
+
Domain = begin
|
7
|
+
digit = "[\\d]"
|
8
|
+
letter = "[\\x61-\\x7a\\x41-\\x5a]"
|
9
|
+
let_dig = "(?:#{letter}|#{digit})"
|
10
|
+
let_dig_hyp = "(?:#{let_dig}|[\\x2d])"
|
11
|
+
label = "#{let_dig}(?:#{let_dig_hyp}*#{let_dig})?"
|
12
|
+
subdomain = "(?:#{label}\\.)*#{label}"
|
13
|
+
domain = "(?:#{subdomain}|\\x20)"
|
18
14
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
15
|
+
/#{domain}/
|
16
|
+
end
|
17
|
+
|
18
|
+
# Matches email addresses with domains that follow the RFC 1035 standard
|
19
|
+
EmailAddress = begin
|
20
|
+
local_part = RFC822::LocalPart.source
|
21
|
+
domain = Domain.source
|
22
|
+
addr_spec = "(#{local_part})\\x40(#{domain})"
|
23
|
+
|
24
|
+
/\A#{addr_spec}\z/
|
27
25
|
end
|
28
26
|
end
|
29
27
|
end
|
@@ -1,37 +1,35 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
|
14
|
-
|
15
|
-
# Local part
|
16
|
-
word = "(?:#{atom}|#{quoted_string})"
|
17
|
-
local_part = "#{word}(?:\\x2e#{word})*"
|
18
|
-
|
19
|
-
# Domain
|
20
|
-
domain_ref = atom
|
21
|
-
sub_domain = "(?:#{domain_ref}|#{domain_literal})"
|
22
|
-
domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
|
23
|
-
|
24
|
-
[/#{local_part}/, /#{domain}/]
|
25
|
-
end
|
1
|
+
module ValidatesAsEmailAddress
|
2
|
+
# The standard describing the format of email addresses
|
3
|
+
module RFC822
|
4
|
+
# Matches the two parts of an email address (before/after @)
|
5
|
+
LocalPart, Domain = begin
|
6
|
+
# Shared
|
7
|
+
qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
|
8
|
+
dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
|
9
|
+
atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
|
10
|
+
quoted_pair = '\\x5c[\\x00-\\x7f]'
|
11
|
+
domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
|
12
|
+
quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
|
26
13
|
|
27
|
-
#
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
14
|
+
# Local part
|
15
|
+
word = "(?:#{atom}|#{quoted_string})"
|
16
|
+
local_part = "#{word}(?:\\x2e#{word})*"
|
17
|
+
|
18
|
+
# Domain
|
19
|
+
domain_ref = atom
|
20
|
+
sub_domain = "(?:#{domain_ref}|#{domain_literal})"
|
21
|
+
domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
|
22
|
+
|
23
|
+
[/#{local_part}/, /#{domain}/]
|
24
|
+
end
|
25
|
+
|
26
|
+
# Matches email addresses according to the RFC822 standard
|
27
|
+
EmailAddress = begin
|
28
|
+
local_part = LocalPart.source
|
29
|
+
domain = Domain.source
|
30
|
+
addr_spec = "(#{local_part})\\x40(#{domain})"
|
31
|
+
|
32
|
+
/\A#{addr_spec}\z/
|
35
33
|
end
|
36
34
|
end
|
37
35
|
end
|
@@ -8,7 +8,7 @@ class ValidatesAsEmailAddressByDefaultTest < Test::Unit::TestCase
|
|
8
8
|
def test_should_not_allow_email_addresses_shorter_than_3_characters
|
9
9
|
user = new_user(:email => 'a@')
|
10
10
|
assert !user.valid?
|
11
|
-
|
11
|
+
assert user.errors.invalid?(:email)
|
12
12
|
|
13
13
|
user.email = 'a@a'
|
14
14
|
assert user.valid?
|
@@ -20,7 +20,7 @@ class ValidatesAsEmailAddressByDefaultTest < Test::Unit::TestCase
|
|
20
20
|
|
21
21
|
user.email += 'a'
|
22
22
|
assert !user.valid?
|
23
|
-
|
23
|
+
assert user.errors.invalid?(:email)
|
24
24
|
end
|
25
25
|
|
26
26
|
def test_should_allow_legal_rfc822_formats
|
@@ -44,7 +44,7 @@ class ValidatesAsEmailAddressByDefaultTest < Test::Unit::TestCase
|
|
44
44
|
].each do |address|
|
45
45
|
user = new_user(:email => address)
|
46
46
|
assert !user.valid?, "#{address} should be illegal."
|
47
|
-
|
47
|
+
assert user.errors.invalid?(:email)
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
@@ -101,7 +101,7 @@ class ValidatesAsEmailAddressTest < Test::Unit::TestCase
|
|
101
101
|
|
102
102
|
user = new_user(:email => 'a@')
|
103
103
|
assert !user.valid?
|
104
|
-
|
104
|
+
assert user.errors.invalid?(:email)
|
105
105
|
end
|
106
106
|
|
107
107
|
def test_should_allow_exact_length
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_as_email_address
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Pfeifer
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-12-14 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -23,20 +23,20 @@ extra_rdoc_files: []
|
|
23
23
|
|
24
24
|
files:
|
25
25
|
- lib/validates_as_email_address
|
26
|
-
- lib/validates_as_email_address/rfc_1035.rb
|
27
26
|
- lib/validates_as_email_address/rfc_822.rb
|
27
|
+
- lib/validates_as_email_address/rfc_1035.rb
|
28
28
|
- lib/validates_as_email_address.rb
|
29
|
+
- test/factory.rb
|
30
|
+
- test/test_helper.rb
|
31
|
+
- test/unit
|
32
|
+
- test/unit/validates_as_email_address_test.rb
|
29
33
|
- test/app_root
|
30
|
-
- test/app_root/app
|
31
|
-
- test/app_root/app/models
|
32
|
-
- test/app_root/app/models/user.rb
|
33
34
|
- test/app_root/db
|
34
35
|
- test/app_root/db/migrate
|
35
36
|
- test/app_root/db/migrate/001_create_users.rb
|
36
|
-
- test/
|
37
|
-
- test/
|
38
|
-
- test/
|
39
|
-
- test/unit/validates_as_email_address_test.rb
|
37
|
+
- test/app_root/app
|
38
|
+
- test/app_root/app/models
|
39
|
+
- test/app_root/app/models/user.rb
|
40
40
|
- CHANGELOG.rdoc
|
41
41
|
- init.rb
|
42
42
|
- LICENSE
|