validate_as_email 1.0.1 → 3.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 28ec894f592a434dcab3f7a29822fc68184248ca
4
+ data.tar.gz: 652bca29a770d9b1e52236f103cfb7ca3ebf32c9
5
+ SHA512:
6
+ metadata.gz: 03243e0cbdf5f6d4782943f66dab6fd65d00db62f839cad14c2f4a3f04e76795351115ce7dc3ccded97bfa5b70fb9fb956ae6b94ef448d7077351608b9a2ad87
7
+ data.tar.gz: 1713e9ae94e98dc9f9516e76e53243339cef1ee618c517288ae0cb098ea246e485dd91583ae608cbd8c161e87e03f81b6530cb87466c185332d7c29dba2cded2
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Evently
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
- # ValidateAsEmail
1
+ # Validate as Email [![Circle CI](https://circleci.com/gh/jcf/validate_as_email.svg?style=svg)](https://circleci.com/gh/jcf/validate_as_email)
2
2
 
3
3
  Validation of email addresses via the excellent Mail gem that is
4
- available in all Rails 3 applications.
4
+ available in all Rails 3 and 4 applications.
5
5
 
6
6
  ## Installation
7
7
 
@@ -19,11 +19,65 @@ Or install it yourself as:
19
19
 
20
20
  ## Usage
21
21
 
22
- This gem required Ruby 1.9, and is tested against MRI, JRuby, and
23
- Rubinius using Travis CI.
22
+ This gem is tested against MRI, JRuby, and Rubinius using Travis CI.
24
23
 
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.
24
+ You will need to be using Rails 3 or greater to make use of this
25
+ validator, as it is built on top of ActiveModel, which was introduced in
26
+ Rails 3.
27
+
28
+ ### Usage with ActiveModel
29
+
30
+ ``` ruby
31
+ class Person
32
+ include ActiveModel::Validations
33
+ validates_as_email :email
34
+ attr_accessor :email
35
+ end
36
+
37
+ # OR
38
+
39
+ class Person
40
+ include ActiveModel::Validations
41
+ validates :email, email: true
42
+ attr_accessor :email
43
+ end
44
+ ```
45
+
46
+ ### Usage with ActiveRecord
47
+
48
+ ``` ruby
49
+ class Person < ActiveRecord::Base
50
+ validates_as_email :email
51
+ attr_accessor :email
52
+ end
53
+
54
+ # OR
55
+
56
+ class Person < ActiveRecord::Base
57
+ validates :email, email: true
58
+ attr_accessor :email
59
+ end
60
+ ```
61
+
62
+ ### Built-in RSpec Matcher
63
+
64
+ The custom matcher will be loaded automatically if RSpec is loaded
65
+ first. Otherwise, you'll need to require the RSpec matcher manually
66
+ using `require 'validate_as_email/rspec'`.
67
+
68
+ ``` ruby
69
+ require 'validate_as_email/rspec'
70
+
71
+ class Person < ActiveRecord::Base
72
+ validates_as_email :email
73
+ attr_accessor :email
74
+ end
75
+
76
+ describe Person do
77
+ it { should have_a_valid_email_address_for(:email) }
78
+ it { should_not have_a_valid_email_address_for(:email) }
79
+ end
80
+ ```
27
81
 
28
82
  ## Contributing
29
83
 
@@ -4,19 +4,26 @@ require 'mail'
4
4
  module ActiveModel
5
5
  module Validations
6
6
  class EmailValidator < EachValidator
7
- attr_reader :record, :attribute, :value, :email, :tree
7
+ attr_reader :record, :attribute, :value, :email
8
8
 
9
9
  def validate_each(record, attribute, value)
10
10
  @record, @attribute, @value = record, attribute, value
11
11
 
12
12
  @email = Mail::Address.new(value)
13
- @tree = email.__send__(:tree)
14
13
 
15
14
  add_error unless valid?
16
15
  rescue Mail::Field::ParseError
17
16
  add_error
18
17
  end
19
18
 
19
+ def domain_parts
20
+ @domain_parts ||= if email.domain
21
+ email.domain.split('.')
22
+ else
23
+ []
24
+ end
25
+ end
26
+
20
27
  private
21
28
 
22
29
  def valid?
@@ -28,7 +35,7 @@ module ActiveModel
28
35
  end
29
36
 
30
37
  def domain_has_more_than_one_atom?
31
- tree.domain.dot_atom_text.elements.length > 1
38
+ domain_parts.length > 1
32
39
  end
33
40
 
34
41
  def add_error
@@ -0,0 +1,48 @@
1
+ require 'rspec/rails/extensions/active_record/base'
2
+ # Adds a custom matcher to RSpec to make it easier to make sure your email
3
+ # column is valid.
4
+ #
5
+ # class Person
6
+ # include ActiveModel::Validations
7
+ #
8
+ # validates_as_email :email
9
+ #
10
+ # def initialize(attributes = {})
11
+ # @attributes = attributes
12
+ # end
13
+ #
14
+ # def email
15
+ # @attributes[:email]
16
+ # end
17
+ #
18
+ # def email=(address)
19
+ # @attributes[:email] = address
20
+ # end
21
+ # end
22
+ #
23
+ # describe Person
24
+ # it { should have_a_valid_email_address_for(:email) }
25
+ # end
26
+ RSpec::Matchers.define(:have_a_valid_email_address_for) do |column_name|
27
+ match do |klass|
28
+ %w(
29
+ matz@example.com
30
+ a@b.tld
31
+ 1@8.8.8.8
32
+ ).each do |valid|
33
+ klass.send("#{column_name}=", valid)
34
+ klass.should have(0).errors_on(column_name)
35
+ end
36
+
37
+ %w(
38
+ word
39
+ a@
40
+ @b
41
+ example.com
42
+ 8.8.8.8
43
+ ).each do |invalid|
44
+ klass.send("#{column_name}=", invalid)
45
+ klass.should have(1).error_on(column_name)
46
+ end
47
+ end
48
+ end
@@ -1,3 +1,3 @@
1
1
  module ValidateAsEmail
2
- VERSION = '1.0.1'
2
+ VERSION = '3.0.0'
3
3
  end
@@ -2,5 +2,6 @@ require 'validate_as_email/version'
2
2
  require 'active_model'
3
3
  require 'active_model/validations/email_validator'
4
4
 
5
- module ValidateAsEmail
5
+ if defined?(RSpec::Matchers)
6
+ require 'validate_as_email/rspec'
6
7
  end
@@ -1,75 +1,57 @@
1
1
  require 'spec_helper'
2
2
 
3
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
4
  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']
5
+ [
6
+ {
7
+ desc: 'with a valid email address',
8
+ validator: {},
9
+ people: [
10
+ {email: 'a@b.c'},
11
+ {email: 'a+x@b.c'},
12
+ {email: 'a@192.168.0.1'},
13
+ {email: '1@1.com'}
14
+ ],
15
+ errors: []
16
+ },
17
+ {
18
+ desc: 'with nil allowed',
19
+ validator: {allow_nil: true},
20
+ people: [ {email: nil} ],
21
+ errors: []
22
+ },
23
+ {
24
+ desc: 'with blank allowed',
25
+ validator: {allow_blank: true},
26
+ people: [ {email: ' '}, {email: "\t\n"} ],
27
+ errors: []
28
+ },
29
+ {
30
+ desc: 'with no message provided',
31
+ validator: {},
32
+ people: [ {email: 'invalid'}, {email: ' '} ],
33
+ errors: ['Email is invalid']
34
+ },
35
+ {
36
+ desc: 'with a custom error message',
37
+ validator: {message: 'is kinda odd looking'},
38
+ people: [ {email: 'invalid'}, {email: ' '} ],
39
+ errors: ['Email is kinda odd looking']
40
+ }
41
+ ].each do |data|
42
+ data[:people].each do |person_attributes|
43
+ context data do
44
+ let(:person) { Person.new(person_attributes) }
45
+
46
+ let(:validator) do
47
+ ActiveModel::Validations::EmailValidator.new(
48
+ data[:validator].merge(attributes: [:email])
49
+ )
50
+ end
51
+
52
+ before { validator.validate(person) }
53
+ it { expect(person.errors.to_a).to eq(data[:errors]) }
54
+ end
73
55
  end
74
56
  end
75
57
  end
@@ -0,0 +1,19 @@
1
+ $: << 'lib'
2
+
3
+ begin
4
+ require 'simplecov'
5
+ rescue LoadError
6
+ end
7
+
8
+ require 'validate_as_email'
9
+ require 'rspec/rails/extensions/active_record/base'
10
+
11
+ Dir.glob(File.expand_path('../support/**/*.rb', __FILE__)) { |file| require file }
12
+
13
+ RSpec.configure do |config|
14
+ config.treat_symbols_as_metadata_keys_with_true_values = true
15
+ config.run_all_when_everything_filtered = true
16
+ config.filter_run :focus
17
+
18
+ config.order = 'random'
19
+ end
@@ -0,0 +1,25 @@
1
+ class Model
2
+ include ActiveModel::Validations
3
+
4
+ attr_accessor :attributes
5
+
6
+ def initialize(attributes = {})
7
+ @attributes = attributes
8
+ end
9
+
10
+ def read_attribute_for_validation(key)
11
+ @attributes[key]
12
+ end
13
+
14
+ def email
15
+ @attributes[:email]
16
+ end
17
+
18
+ def email=(email)
19
+ @attributes[:email] = email
20
+ end
21
+ end
22
+
23
+ class Person < Model
24
+ validates :email, email: true
25
+ end
metadata CHANGED
@@ -1,132 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validate_as_email
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
5
- prerelease:
4
+ version: 3.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - James Conroy-Finn
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-07-19 00:00:00.000000000 Z
11
+ date: 2016-02-22 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activemodel
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
- version: '3'
19
+ version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
- version: '3'
26
+ version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: mail
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
- version: '2'
33
+ version: '0'
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - ">="
44
39
  - !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
40
+ version: '0'
41
+ description: The ultimate email validator
95
42
  email:
96
43
  - james@logi.cl
97
44
  executables: []
98
45
  extensions: []
99
46
  extra_rdoc_files: []
100
47
  files:
48
+ - LICENSE
49
+ - README.md
101
50
  - lib/active_model/validations/email_validator.rb
102
- - lib/validate_as_email/version.rb
103
51
  - lib/validate_as_email.rb
104
- - README.md
52
+ - lib/validate_as_email/rspec.rb
53
+ - lib/validate_as_email/version.rb
105
54
  - spec/active_model/validations/email_validator_spec.rb
106
- homepage: https://github.com/evently/mail_validator
55
+ - spec/spec_helper.rb
56
+ - spec/support/model.rb
57
+ homepage: https://github.com/jcf/validate_as_email
107
58
  licenses: []
59
+ metadata: {}
108
60
  post_install_message:
109
61
  rdoc_options: []
110
62
  require_paths:
111
63
  - lib
112
64
  required_ruby_version: !ruby/object:Gem::Requirement
113
- none: false
114
65
  requirements:
115
- - - ! '>='
66
+ - - ">="
116
67
  - !ruby/object:Gem::Version
117
68
  version: '0'
118
69
  required_rubygems_version: !ruby/object:Gem::Requirement
119
- none: false
120
70
  requirements:
121
- - - ! '>='
71
+ - - ">="
122
72
  - !ruby/object:Gem::Version
123
73
  version: '0'
124
74
  requirements: []
125
75
  rubyforge_project:
126
- rubygems_version: 1.8.23
76
+ rubygems_version: 2.4.5.1
127
77
  signing_key:
128
- specification_version: 3
129
- summary: The ultimate Rails 3 email validator. Powered by the Mail gem.
78
+ specification_version: 4
79
+ summary: The ultimate email validator. Powered by the Mail gem.
130
80
  test_files:
131
81
  - spec/active_model/validations/email_validator_spec.rb
132
- has_rdoc:
82
+ - spec/spec_helper.rb
83
+ - spec/support/model.rb