dancroak-validates_email_format_of 1.2.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ = CHANGELOG
2
+
3
+ == Version 1.0
4
+ * initial version
5
+
6
+ == Version 1.1 (the Francis Hwang edition)
7
+ * moved Regexp out of class methods into the ValidatesEmailFormatOf module
8
+
9
+ == Version 1.2 (the Ismael Santos Kafeltz and Michael MacDonald edition)
10
+ * added support for un-escaped and escaped special characters in the local part, per RFC 3696
11
+ * added :allow_nil option
12
+
13
+ == Version 1.2.1 (the RTFM edition)
14
+ * added support for quoted local parts
15
+ * added length checks for domain and local parts
16
+ * corrected escaped character support for RFC 3696 Errata
17
+ * added :allow_blank option
18
+ * added :unless option
19
+
20
+ == Unreleased
21
+ * Now available as a gem on GitHub
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006 Alex Dunae
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest ADDED
@@ -0,0 +1,15 @@
1
+ CHANGELOG
2
+ init.rb
3
+ lib/validates_email_format_of.rb
4
+ MIT-LICENSE
5
+ rails/init.rb
6
+ rakefile
7
+ README.markdown
8
+ test/database.yml
9
+ test/fixtures/people.yml
10
+ test/fixtures/person.rb
11
+ test/schema.rb
12
+ test/test_helper.rb
13
+ test/validates_email_format_of_test.rb
14
+ Rakefile
15
+ Manifest
data/README.markdown ADDED
@@ -0,0 +1,54 @@
1
+ Validates email format
2
+ ======================
3
+
4
+ Validate various formats of email address against RFC 2822.
5
+
6
+ Usage
7
+ -----
8
+
9
+ class Person < ActiveRecord::Base
10
+ validates_email_format_of :email
11
+ end
12
+
13
+ Options
14
+ -------
15
+
16
+ :message =>
17
+ String. A custom error message (default is: " does not appear to be a valid e-mail address")
18
+
19
+ :on =>
20
+ Symbol. Specifies when this validation is active (default is :save, other options :create, :update)
21
+
22
+ :allow_nil =>
23
+ Boolean. Allow nil values (default is false)
24
+
25
+ :allow_blank =>
26
+ Boolean. Allow blank values (default is false)
27
+
28
+ :if =>
29
+ Specifies a method, proc or string to call to determine if the validation should occur
30
+ (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The method,
31
+ proc or string should return or evaluate to a true or false value.
32
+
33
+ :unless =>
34
+ See :if option.
35
+
36
+ Testing
37
+ -------
38
+
39
+ To execute the unit tests run <tt>rake test</tt>.
40
+
41
+ The unit tests for this plugin use an in-memory sqlite3 database.
42
+
43
+ Installing the gem
44
+ ------------------
45
+
46
+ * gem sources -a http://gems.github.com (only needed once)
47
+ * sudo gem install dancroak-validates\_email\_format\_of
48
+
49
+ Credits
50
+ -------
51
+
52
+ Written by Alex Dunae (dunae.ca), 2006-07.
53
+
54
+ Thanks to Francis Hwang (http://fhwang.net/) at Diversion Media for creating the 1.1 update.
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'rails', 'init')
@@ -0,0 +1,54 @@
1
+ module ValidatesEmailFormatOf
2
+ LocalPartSpecialChars = Regexp.escape('!#$%&\'*-/=?+-^_`{|}~')
3
+ LocalPartUnquoted = '(([[:alnum:]' + LocalPartSpecialChars + ']+[\.\+]+))*[[:alnum:]' + LocalPartSpecialChars + '+]+'
4
+ LocalPartQuoted = '\"(([[:alnum:]' + LocalPartSpecialChars + '\.\+]*|(\\\\[\x00-\xFF]))*)\"'
5
+ Regex = Regexp.new('^((' + LocalPartUnquoted + ')|(' + LocalPartQuoted + ')+)@(((\w+\-+)|(\w+\.))*\w{1,63}\.[a-z]{2,6}$)', Regexp::EXTENDED | Regexp::IGNORECASE)
6
+ end
7
+
8
+ module ActiveRecord
9
+ module Validations
10
+ module ClassMethods
11
+ # Validates whether the value of the specified attribute is a valid email address
12
+ #
13
+ # class User < ActiveRecord::Base
14
+ # validates_email_format_of :email, :on => :create
15
+ # end
16
+ #
17
+ # Configuration options:
18
+ # * <tt>message</tt> - A custom error message (default is: " does not appear to be a valid e-mail address")
19
+ # * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update)
20
+ # * <tt>allow_nil</tt> - Allow nil values (default is false)
21
+ # * <tt>allow_blank</tt> - Allow blank values (default is false)
22
+ # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should
23
+ # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
24
+ # method, proc or string should return or evaluate to a true or false value.
25
+ # * <tt>unless</tt> - See <tt>:if</tt>
26
+ def validates_email_format_of(*attr_names)
27
+ options = { :message => ' does not appear to be a valid e-mail address',
28
+ :on => :save,
29
+ :allow_nil => false,
30
+ :allow_blank => false,
31
+ :with => ValidatesEmailFormatOf::Regex }
32
+
33
+ options.update(attr_names.pop) if attr_names.last.is_a?(Hash)
34
+
35
+ validates_each(attr_names, options) do |record, attr_name, value|
36
+ v = value.to_s
37
+
38
+ # local part max is 64 chars, domain part max is 255 chars
39
+ # TODO: should this decode escaped entities before counting?
40
+ begin
41
+ domain, local = v.reverse.split('@', 2)
42
+ rescue
43
+ record.errors.add(attr_name, options[:message])
44
+ next
45
+ end
46
+
47
+ unless v =~ options[:with] and not v =~ /\.\./ and domain.length <= 255 and local.length <= 64
48
+ record.errors.add(attr_name, options[:message])
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'validates_email_format_of'
data/rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rake'
2
+ require 'echoe'
3
+
4
+ Echoe.new("validates_email_format_of") do |p|
5
+ p.author = "Alex Dunae"
6
+ p.summary = "Validate e-mail addreses against RFC 2822 and RFC 3696"
7
+ p.url = "http://code.dunae.ca/validates_email_format_of.html"
8
+ p.version = '1.2.1'
9
+ p.email = 'code@code.dunae.ca'
10
+ end
@@ -0,0 +1,61 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require 'rubygems'
4
+ require 'active_record'
5
+
6
+ require 'validates_email_format_of'
7
+
8
+ ActiveRecord::Base.establish_connection(
9
+ :adapter => 'sqlite3',
10
+ :database => ':memory:')
11
+
12
+ ActiveRecord::Schema.define(:version => 0) do
13
+ create_table :people, :force => true do |t|
14
+ t.column 'email', :string
15
+ end
16
+ end
17
+
18
+ class Person < ActiveRecord::Base
19
+ validates_email_format_of :email, :on => :create, :message => 'fails with custom message', :allow_nil => true
20
+ end
21
+
22
+ # Set up Feedback testing framework, a la carte
23
+
24
+ require 'test/unit'
25
+ require 'shoulda'
26
+ require "#{File.dirname(__FILE__)}/../init"
27
+
28
+ class Test::Unit::TestCase #:nodoc:
29
+
30
+ def self.should_allow_values(*good_values)
31
+ get_options!(good_values)
32
+ good_values.each do |v|
33
+ should "allow email to be set to #{v.inspect}" do
34
+ user = User.new(:email => v)
35
+ user.save
36
+ assert_nil user.errors.on(:email)
37
+ end
38
+ end
39
+ end
40
+
41
+ def self.should_not_allow_values(*bad_values)
42
+ message = get_options!(bad_values, :message)
43
+ message ||= /invalid/
44
+ bad_values.each do |v|
45
+ should "not allow email to be set to #{v.inspect}" do
46
+ user = User.new(:email => v)
47
+ assert !user.save, "Saved user with email set to \"#{v}\""
48
+ assert user.errors.on(:email), "There are no errors set on email after being set to \"#{v}\""
49
+ end
50
+ end
51
+ end
52
+
53
+ def self.get_options!(args, *wanted)
54
+ ret = []
55
+ opts = (args.last.is_a?(Hash) ? args.pop : {})
56
+ wanted.each {|w| ret << opts.delete(w)}
57
+ raise ArgumentError, "Unsuported options given: #{opts.keys.join(', ')}" unless opts.keys.empty?
58
+ return *ret
59
+ end
60
+
61
+ end
@@ -0,0 +1,137 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ValidatesEmailFormatOfTest < Test::Unit::TestCase
4
+ @@valid_email = 'valid@example.com'
5
+ @@invalid_email = 'invalid@example.'
6
+
7
+ @@valid_emails = ['valid@example.com',
8
+ 'Valid@test.example.com',
9
+ 'valid+valid123@test.example.com',
10
+ 'valid_valid123@test.example.com',
11
+ 'valid-valid+123@test.example.co.uk',
12
+ 'valid-valid+1.23@test.example.com.au',
13
+ 'valid@example.co.uk',
14
+ 'v@example.com',
15
+ 'valid@example.ca',
16
+ 'valid_@example.com',
17
+ 'valid123.456@example.org',
18
+ 'valid123.456@example.travel',
19
+ 'valid123.456@example.museum',
20
+ 'valid@example.mobi',
21
+ 'valid@example.info',
22
+ 'valid-@example.com',
23
+ # from RFC 3696, page 6
24
+ 'customer/department=shipping@example.com',
25
+ '$A12345@example.com',
26
+ '!def!xyz%abc@example.com',
27
+ '_somename@example.com',
28
+ # apostrophes
29
+ "test'test@example.com",
30
+ ]
31
+
32
+ def test_should_allow_valid_email_addresses
33
+ @@valid_emails.each do |email|
34
+ p = create_person(:email => email)
35
+ save_passes(p, email)
36
+ end
37
+ end
38
+
39
+ @@invalid_emails = ['invalid@example-com',
40
+ # period can not start local part
41
+ '.invalid@example.com',
42
+ # period can not end local part
43
+ 'invalid.@example.com',
44
+ # period can not appear twice consecutively in local part
45
+ 'invali..d@example.com',
46
+ 'invalid@example.com.',
47
+ 'invalid@example.com_',
48
+ 'invalid@example.com-',
49
+ 'invalid-example.com',
50
+ 'invalid@example.b#r.com',
51
+ 'invalid@example.c',
52
+ 'invali d@example.com',
53
+ 'invalidexample.com',
54
+ 'invalid@example.']
55
+
56
+ def test_should_not_allow_invalid_email_addresses
57
+ @@invalid_emails.each do |email|
58
+ p = create_person(:email => email)
59
+ save_fails(p, email)
60
+ end
61
+ end
62
+
63
+ @@quoted_emails = ['"Abc\@def"@example.com',
64
+ '"Fred\ Bloggs"@example.com',
65
+ '"Joe.\\Blow"@example.com',
66
+ ]
67
+
68
+ # from http://www.rfc-editor.org/errata_search.php?rfc=3696
69
+ def test_should_allow_quoted_characters
70
+ @@quoted_emails.each do |email|
71
+ p = create_person(:email => email)
72
+ save_passes(p, email)
73
+ end
74
+ end
75
+
76
+ @@unescaped_emails = ['Fred\ Bloggs_@example.com',
77
+ 'Abc\@def+@example.com',
78
+ 'Joe.\\Blow@example.com'
79
+ ]
80
+
81
+ # from http://tools.ietf.org/html/rfc3696, page 5
82
+ # corrected in http://www.rfc-editor.org/errata_search.php?rfc=3696
83
+ def test_should_not_allow_escaped_characters_without_quotes
84
+ @@unescaped_emails.each do |email|
85
+ p = create_person(:email => email)
86
+ save_fails(p, email)
87
+ end
88
+ end
89
+
90
+ @@long_emails = ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@example.com',
91
+ 'test@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com'
92
+ ]
93
+
94
+ def test_should_check_length_limits
95
+ @@long_emails.each do |email|
96
+ p = create_person(:email => email)
97
+ save_fails(p, email)
98
+ end
99
+ end
100
+
101
+ def test_should_respect_validate_on_option
102
+ p = create_person(:email => @@valid_email)
103
+ save_passes(p)
104
+
105
+ # we only asked to validate on :create so this should fail
106
+ assert p.update_attributes(:email => @@invalid_email)
107
+ assert_equal @@invalid_email, p.email
108
+ end
109
+
110
+ def test_should_allow_custom_error_message
111
+ p = create_person(:email => @@invalid_email)
112
+ save_fails(p)
113
+ assert_equal 'fails with custom message', p.errors.on(:email)
114
+ end
115
+
116
+ def test_should_allow_nil
117
+ p = create_person(:email => nil)
118
+ save_passes(p)
119
+ end
120
+
121
+ protected
122
+ def create_person(params)
123
+ Person.new(params)
124
+ end
125
+
126
+ def save_passes(p, email = '')
127
+ assert p.valid?, " validating #{email}"
128
+ assert p.save
129
+ assert_nil p.errors.on(:email)
130
+ end
131
+
132
+ def save_fails(p, email = '')
133
+ assert !p.valid?, " validating #{email}"
134
+ assert !p.save
135
+ assert p.errors.on(:email)
136
+ end
137
+ end
@@ -0,0 +1,33 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{validates_email_format_of}
3
+ s.version = "1.2.1"
4
+
5
+ s.required_rubygems_version = Gem::Requirement.new("= 1.2") if s.respond_to? :required_rubygems_version=
6
+ s.authors = ["Alex Dunae"]
7
+ s.date = %q{2008-07-02}
8
+ s.description = %q{Validate e-mail addreses against RFC 2822 and RFC 3696}
9
+ s.email = %q{code@code.dunae.ca}
10
+ s.extra_rdoc_files = ["CHANGELOG", "lib/validates_email_format_of.rb", "README.markdown"]
11
+ s.files = ["CHANGELOG", "init.rb", "lib/validates_email_format_of.rb", "MIT-LICENSE", "rails/init.rb", "rakefile", "README", "test/database.yml", "test/fixtures/people.yml", "test/fixtures/person.rb", "test/schema.rb", "test/test_helper.rb", "test/validates_email_format_of_test.rb", "Rakefile", "Manifest", "validates_email_format_of.gemspec"]
12
+ s.has_rdoc = true
13
+ s.homepage = %q{http://code.dunae.ca/validates_email_format_of.html}
14
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Validates_email_format_of"]
15
+ s.require_paths = ["lib"]
16
+ s.rubyforge_project = %q{validates_email_format_of}
17
+ s.rubygems_version = %q{1.2.0}
18
+ s.summary = %q{Validate e-mail addreses against RFC 2822 and RFC 3696}
19
+ s.test_files = ["test/test_helper.rb", "test/validates_email_format_of_test.rb"]
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 2
24
+
25
+ if current_version >= 3 then
26
+ s.add_development_dependency(%q<echoe>, [">= 0"])
27
+ else
28
+ s.add_dependency(%q<echoe>, [">= 0"])
29
+ end
30
+ else
31
+ s.add_dependency(%q<echoe>, [">= 0"])
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dancroak-validates_email_format_of
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Dunae
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-02 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: echoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description: Validate e-mail addreses against RFC 2822 and RFC 3696
25
+ email: code@code.dunae.ca
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - CHANGELOG
32
+ - lib/validates_email_format_of.rb
33
+ - README.markdown
34
+ files:
35
+ - CHANGELOG
36
+ - init.rb
37
+ - lib/validates_email_format_of.rb
38
+ - MIT-LICENSE
39
+ - rails/init.rb
40
+ - rakefile
41
+ - README
42
+ - test/database.yml
43
+ - test/fixtures/people.yml
44
+ - test/fixtures/person.rb
45
+ - test/schema.rb
46
+ - test/test_helper.rb
47
+ - test/validates_email_format_of_test.rb
48
+ - Rakefile
49
+ - Manifest
50
+ - validates_email_format_of.gemspec
51
+ - README.markdown
52
+ has_rdoc: true
53
+ homepage: http://code.dunae.ca/validates_email_format_of.html
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --line-numbers
57
+ - --inline-source
58
+ - --title
59
+ - Validates_email_format_of
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - "="
71
+ - !ruby/object:Gem::Version
72
+ version: "1.2"
73
+ version:
74
+ requirements: []
75
+
76
+ rubyforge_project: validates_email_format_of
77
+ rubygems_version: 1.2.0
78
+ signing_key:
79
+ specification_version: 2
80
+ summary: Validate e-mail addreses against RFC 2822 and RFC 3696
81
+ test_files:
82
+ - test/test_helper.rb
83
+ - test/validates_email_format_of_test.rb