validate_as_email 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# ValidateAsEmail
|
2
|
+
|
3
|
+
Validation of email addresses via the excellent Mail gem that is
|
4
|
+
available in all Rails 3 applications.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'validate_as_email'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install validate_as_email
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
This gem required Ruby 1.9, and is tested against MRI, JRuby, and
|
23
|
+
Rubinius using Travis CI.
|
24
|
+
|
25
|
+
You will need to be using Rails 3 to make use of this validator, as it
|
26
|
+
is built on top of ActiveModel, which was introduced in Rails 3.
|
27
|
+
|
28
|
+
## Contributing
|
29
|
+
|
30
|
+
1. Fork it
|
31
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
32
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
33
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
34
|
+
5. Create new Pull Request
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require 'mail'
|
3
|
+
|
4
|
+
module ActiveModel
|
5
|
+
module Validations
|
6
|
+
class EmailValidator < EachValidator
|
7
|
+
attr_reader :record, :attribute, :value, :email, :tree
|
8
|
+
|
9
|
+
def validate_each(record, attribute, value)
|
10
|
+
@record, @attribute, @value = record, attribute, value
|
11
|
+
|
12
|
+
@email = Mail::Address.new(value)
|
13
|
+
@tree = email.__send__(:tree)
|
14
|
+
|
15
|
+
add_error unless valid?
|
16
|
+
rescue Mail::Field::ParseError
|
17
|
+
add_error
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def valid?
|
23
|
+
!!(domain_and_address_present? && domain_has_more_than_one_atom?)
|
24
|
+
end
|
25
|
+
|
26
|
+
def domain_and_address_present?
|
27
|
+
email.domain && email.address == value
|
28
|
+
end
|
29
|
+
|
30
|
+
def domain_has_more_than_one_atom?
|
31
|
+
tree.domain.dot_atom_text.elements.length > 1
|
32
|
+
end
|
33
|
+
|
34
|
+
def add_error
|
35
|
+
if message = options[:message]
|
36
|
+
record.errors[attribute] << message
|
37
|
+
else
|
38
|
+
record.errors.add(attribute, :invalid)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
module HelperMethods
|
44
|
+
def validates_as_email(*attr_names)
|
45
|
+
validates_with EmailValidator, _merge_attributes(attr_names)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveModel::Validations::EmailValidator do
|
4
|
+
let(:person) { Person.new(email: 'invalid') }
|
5
|
+
|
6
|
+
let(:options) { {attributes: [:email]} }
|
7
|
+
|
8
|
+
def build_validator(options = {})
|
9
|
+
ActiveModel::Validations::EmailValidator.new(
|
10
|
+
options.merge(attributes: [:email])
|
11
|
+
)
|
12
|
+
end
|
13
|
+
|
14
|
+
subject(:validator) { build_validator }
|
15
|
+
|
16
|
+
describe '#validate' do
|
17
|
+
context 'with a valid email address' do
|
18
|
+
before do
|
19
|
+
person.email = 'james@logi.cl'
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'does not add errors' do
|
23
|
+
validator.validate(person)
|
24
|
+
person.errors.to_a.should == []
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'with nil allowed' do
|
29
|
+
subject(:validator) do
|
30
|
+
build_validator(allow_nil: true)
|
31
|
+
end
|
32
|
+
|
33
|
+
before do
|
34
|
+
person.email = nil
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'skips adding errors is email is nil' do
|
38
|
+
validator.validate(person)
|
39
|
+
person.errors.to_a.should == []
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'with blank is allowed' do
|
44
|
+
subject(:validator) do
|
45
|
+
build_validator(allow_blank: true)
|
46
|
+
end
|
47
|
+
|
48
|
+
before do
|
49
|
+
person.email = ' '
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'skips adding errors is email is nil' do
|
53
|
+
validator.validate(person)
|
54
|
+
person.errors.to_a.should == []
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'with no message provided' do
|
59
|
+
it 'adds a symbol to errors for I18n lookup' do
|
60
|
+
validator.validate(person)
|
61
|
+
person.errors.to_a.should == ['Email is invalid']
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'with a specific error message provided' do
|
66
|
+
subject(:validator) do
|
67
|
+
build_validator(message: 'is kinda odd looking')
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'uses the message you specify' do
|
71
|
+
validator.validate(person)
|
72
|
+
person.errors.to_a.should == ['Email is kinda odd looking']
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validate_as_email
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- James Conroy-Finn
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: mail
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec-rails
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '2.11'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.11'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: cucumber
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '1.2'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '1.2'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: aruba
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0.4'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0.4'
|
94
|
+
description: The ultimate Rails 3 email validator
|
95
|
+
email:
|
96
|
+
- james@logi.cl
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- lib/active_model/validations/email_validator.rb
|
102
|
+
- lib/validate_as_email/version.rb
|
103
|
+
- lib/validate_as_email.rb
|
104
|
+
- README.md
|
105
|
+
- spec/active_model/validations/email_validator_spec.rb
|
106
|
+
homepage: https://github.com/evently/mail_validator
|
107
|
+
licenses: []
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ! '>='
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 1.8.23
|
127
|
+
signing_key:
|
128
|
+
specification_version: 3
|
129
|
+
summary: The ultimate Rails 3 email validator. Powered by the Mail gem.
|
130
|
+
test_files:
|
131
|
+
- spec/active_model/validations/email_validator_spec.rb
|
132
|
+
has_rdoc:
|