email_format_validator 0.0.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/.gitignore +5 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/email_format_validator.gemspec +18 -0
- data/lib/email_format_validator.rb +28 -0
- data/test/email_format_validator_test.rb +106 -0
- data/test/test_helper.rb +22 -0
- metadata +65 -0
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "email_format_validator"
|
6
|
+
s.version = "0.0.1"
|
7
|
+
s.authors = ["Axel Vergult"]
|
8
|
+
s.email = ["axel.vergult@gmail.com"]
|
9
|
+
s.homepage = "https://github.com/episko/email_format_validator"
|
10
|
+
s.summary = %q{A Rails 3 email format validator following the RFC 822}
|
11
|
+
s.description = %q{A Rails 3 email format validator following the RFC 822}
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.add_dependency("activemodel", ">= 0")
|
18
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require "uri"
|
4
|
+
|
5
|
+
class EmailFormatValidator < ActiveModel::EachValidator
|
6
|
+
|
7
|
+
module Patterns
|
8
|
+
def self.create_regexp(string)
|
9
|
+
Regexp.new string, nil, 'n'
|
10
|
+
end
|
11
|
+
ATOM = create_regexp "[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+"
|
12
|
+
QTEXT = create_regexp "[^\\x0d\\x22\\x5c\\x80-\\xff]"
|
13
|
+
QPAIR = create_regexp "\\x5c[\\x00-\\x7f]"
|
14
|
+
QSTRING = create_regexp "\\x22(?:#{QTEXT}|#{QPAIR})*\\x22"
|
15
|
+
WORD = create_regexp "(?:#{ATOM}|#{QSTRING})"
|
16
|
+
LOCAL_PT = create_regexp "#{WORD}(?:\\x2e#{WORD})*"
|
17
|
+
ADDRESS = create_regexp "#{LOCAL_PT}\\x40#{URI::REGEXP::PATTERN::HOSTNAME}"
|
18
|
+
|
19
|
+
EMAIL = /\A#{ADDRESS}\z/
|
20
|
+
end
|
21
|
+
|
22
|
+
def validate_each(record, attribute, value)
|
23
|
+
unless value =~ Patterns::EMAIL
|
24
|
+
record.errors[attribute] << (options[:message] || "is not formatted properly")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require_relative 'test_helper'
|
4
|
+
|
5
|
+
class TestUser < TestModel
|
6
|
+
validates :email, :email_format => true
|
7
|
+
end
|
8
|
+
|
9
|
+
class TestUserAllowsNilToTrue < TestModel
|
10
|
+
validates :email, :email_format => {:allow_nil => true}
|
11
|
+
end
|
12
|
+
|
13
|
+
class TestUserAllowsNilToFalse < TestModel
|
14
|
+
validates :email, :email_format => {:allow_nil => false}
|
15
|
+
end
|
16
|
+
|
17
|
+
class TestUserWithMessage < TestModel
|
18
|
+
validates :email, :email_format => {:message => 'is not a good looking email'}
|
19
|
+
end
|
20
|
+
|
21
|
+
class TestEmailFormatValidator < MiniTest::Unit::TestCase
|
22
|
+
def valid_emails
|
23
|
+
[
|
24
|
+
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@letters-in-local.org',
|
25
|
+
'01234567890@numbers-in-local.net',
|
26
|
+
'&\'*+-./=?^_{}~@other-valid-characters-in-local.net',
|
27
|
+
'mixed-1234-in-{+^}-local@sld.net',
|
28
|
+
'a@single-character-in-local.org',
|
29
|
+
'"quoted"@sld.com',
|
30
|
+
'"\e\s\c\a\p\e\d"@sld.com',
|
31
|
+
'"quoted-at-sign@sld.org"@sld.com',
|
32
|
+
'"escaped\"quote"@sld.com',
|
33
|
+
'"back\slash"@sld.com',
|
34
|
+
'one-character-third-level@a.example.com',
|
35
|
+
'single-character-in-sld@x.org',
|
36
|
+
'local@dash-in-sld.com',
|
37
|
+
'letters-in-sld@123.com',
|
38
|
+
'one-letter-sld@x.org',
|
39
|
+
'uncommon-tld@sld.museum',
|
40
|
+
'uncommon-tld@sld.travel',
|
41
|
+
'uncommon-tld@sld.mobi',
|
42
|
+
'country-code-tld@sld.uk',
|
43
|
+
'country-code-tld@sld.rw',
|
44
|
+
'local@sld.newTLD',
|
45
|
+
'punycode-numbers-in-tld@sld.xn--3e0b707e',
|
46
|
+
'the-total-length@of-an-entire-address-cannot-be-longer-than-two-hundred-and-fifty-four-characters' +
|
47
|
+
'-and-this-address-is-254-characters-exactly-so-it-should-be-valid-and-im-going-to-add-some-more' +
|
48
|
+
'-words-here-to-increase-the-lenght-blah-blah-blah-blah-bla.org',
|
49
|
+
'local@sub.domains.com'
|
50
|
+
]
|
51
|
+
end
|
52
|
+
|
53
|
+
def invalid_emails
|
54
|
+
[
|
55
|
+
'@missing-local.org',
|
56
|
+
'! #$%`|@invalid-characters-in-local.org',
|
57
|
+
'(),:;`|@more-invalid-characters-in-local.org',
|
58
|
+
'<>@[]\`|@even-more-invalid-characters-in-local.org',
|
59
|
+
'.local-starts-with-dot@sld.com',
|
60
|
+
'local-ends-with-dot.@sld.com',
|
61
|
+
'two..consecutive-dots@sld.com',
|
62
|
+
'missing-sld@.com',
|
63
|
+
'sld-starts-with-dashsh@-sld.com',
|
64
|
+
'sld-ends-with-dash@sld-.com',
|
65
|
+
'invalid-characters-in-sld@! "#$%(),/;<>_[]`|.org',
|
66
|
+
'missing-at-sign.net',
|
67
|
+
'unbracketed-IP@127.0.0.1',
|
68
|
+
'invalid-ip@127.0.0.1.26',
|
69
|
+
'another-invalid-ip@127.0.0.256',
|
70
|
+
'IP-and-port@127.0.0.1:25'
|
71
|
+
]
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_valid_emails
|
75
|
+
valid_emails.each { |email| assert TestUser.new(:email => email).valid? }
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_invalid_emails
|
79
|
+
invalid_emails.each { |email| refute TestUser.new(:email => email).valid? }
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_default_message_on_error
|
83
|
+
test_user = TestUser.new(:email => "invalid_email@")
|
84
|
+
refute test_user.valid?
|
85
|
+
assert test_user.errors[:email].include?("is not formatted properly")
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_custom_message_on_error
|
89
|
+
test_user = TestUserWithMessage.new(:email => "invalid_email@")
|
90
|
+
refute test_user.valid?
|
91
|
+
assert test_user.errors[:email].include?("is not a good looking email")
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_nil_email_when_allow_nil_option_is_not_set
|
95
|
+
refute TestUser.new(:email => nil).valid?
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_nil_email_when_allow_nil_option_is_set_to_true
|
99
|
+
assert TestUserAllowsNilToTrue.new(:email => nil).valid?
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_nil_email_when_allow_nil_option_is_set_to_false
|
103
|
+
refute TestUserAllowsNilToFalse.new(:email => nil).valid?
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'active_model'
|
5
|
+
require 'minitest/autorun'
|
6
|
+
|
7
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
8
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
9
|
+
|
10
|
+
require 'email_format_validator'
|
11
|
+
|
12
|
+
class TestModel
|
13
|
+
include ActiveModel::Validations
|
14
|
+
|
15
|
+
def initialize(attributes = {})
|
16
|
+
@attributes = attributes
|
17
|
+
end
|
18
|
+
|
19
|
+
def read_attribute_for_validation(key)
|
20
|
+
@attributes[key]
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: email_format_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Axel Vergult
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-09 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: &70110136755020 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70110136755020
|
25
|
+
description: A Rails 3 email format validator following the RFC 822
|
26
|
+
email:
|
27
|
+
- axel.vergult@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- Rakefile
|
35
|
+
- email_format_validator.gemspec
|
36
|
+
- lib/email_format_validator.rb
|
37
|
+
- test/email_format_validator_test.rb
|
38
|
+
- test/test_helper.rb
|
39
|
+
homepage: https://github.com/episko/email_format_validator
|
40
|
+
licenses: []
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.8.10
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: A Rails 3 email format validator following the RFC 822
|
63
|
+
test_files:
|
64
|
+
- test/email_format_validator_test.rb
|
65
|
+
- test/test_helper.rb
|