activemodel-email_address_validator 1.0.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +8 -2
- data/CHANGELOG.md +17 -0
- data/README.md +10 -1
- data/activemodel-email_address_validator.gemspec +3 -3
- data/lib/activemodel_email_address_validator/version.rb +1 -1
- data/lib/validator/email_address_validator.rb +35 -3
- data/test/activemodel-email_address_validator/test_email_address.rb +4 -0
- data/test/minitest_helper.rb +19 -0
- data/test/test_active_model_integration.rb +42 -31
- metadata +9 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 880e994c63a284ed895dfab2da408360b9cd70f8466176e99c1118d76e830f64
|
4
|
+
data.tar.gz: ef9a1389211791aff43ed9874421515e3417b7e2b00e85ce70ee478c8f8a837b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0dd406cdb1537d8d8099e71531e8797b9d4ebe3f619842ef65eaf9d43fd1a31062739bbca7ed7fa3f46f63ba2da30a8949f1e0f5795b7af3f26e2ea3cc8c030c
|
7
|
+
data.tar.gz: c8ebc7e19f10ac906503606c8308c7ade2018a08840465743819d38f86598320955323020fbd73d8b0304794c2cc11104195a9a19639c9740bae246295735c73
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,23 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
6
6
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
7
7
|
|
8
|
+
## [2.0.0]
|
9
|
+
|
10
|
+
### Added
|
11
|
+
|
12
|
+
- Ability to specify custom rules to use when validating the email address
|
13
|
+
using the `:with` option.
|
14
|
+
|
15
|
+
- Support for Ruby 2.5, 2.5 and 2.6. We've probably always had the support,
|
16
|
+
but now we're actually testing it.
|
17
|
+
|
18
|
+
- Support for ActiveModel 6.
|
19
|
+
|
20
|
+
### Removed
|
21
|
+
|
22
|
+
- Support for Ruby 2.2 and 2.3 that are both EOL. We probably still support
|
23
|
+
and work on those versions, but we won't verify and test them any more.
|
24
|
+
|
8
25
|
## [1.0.1]
|
9
26
|
|
10
27
|
### Added
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Email Address Validator
|
2
2
|
## ActiveModel-style email address format validator
|
3
3
|
|
4
|
-
[![Build Status](https://travis-ci.org/substancelab/activemodel-email_address_validator.svg?branch=master)](https://travis-ci.org/substancelab/activemodel-email_address_validator) [![Code Climate](https://codeclimate.com/github/substancelab/activemodel-email_address_validator/badges/gpa.svg)](https://codeclimate.com/github/substancelab/activemodel-email_address_validator) [![Coverage Status](https://coveralls.io/repos/substancelab/activemodel-email_address_validator/badge.svg?branch=more-badges&service=github)](https://coveralls.io/github/substancelab/activemodel-email_address_validator?branch=more-badges)
|
4
|
+
[![Build Status](https://travis-ci.org/substancelab/activemodel-email_address_validator.svg?branch=master)](https://travis-ci.org/substancelab/activemodel-email_address_validator) [![Code Climate](https://codeclimate.com/github/substancelab/activemodel-email_address_validator/badges/gpa.svg)](https://codeclimate.com/github/substancelab/activemodel-email_address_validator) [![Coverage Status](https://coveralls.io/repos/substancelab/activemodel-email_address_validator/badge.svg?branch=more-badges&service=github)](https://coveralls.io/github/substancelab/activemodel-email_address_validator?branch=more-badges)
|
5
5
|
|
6
6
|
Whenever I have wanted to validate an email address it has been because I wanted to be somewhat certain I can send an email to someone. Usually this happens as part of a signup procedure.
|
7
7
|
|
@@ -28,6 +28,14 @@ If you want to use a specific regular expression:
|
|
28
28
|
|
29
29
|
validates :email, :email_address => {:format => /.+@.+\..+/}
|
30
30
|
|
31
|
+
### Bring your own logic
|
32
|
+
|
33
|
+
If a regular expression isn't enough for you, you can include your own rules for email addresses. For example, you could validate that all email adresses belong to the same company:
|
34
|
+
|
35
|
+
validates :email, :email_address => {
|
36
|
+
:with => proc { |address| address.end_with?("@substancelab.com") }
|
37
|
+
}
|
38
|
+
|
31
39
|
### Verify domain (still to be done - pull request, anybody?)
|
32
40
|
|
33
41
|
This also checks that the domain actually has an MX record. Note this might take a while because of DNS lookups.
|
@@ -64,6 +72,7 @@ Or install it yourself as:
|
|
64
72
|
* https://github.com/codyrobbins/active-model-email-validator
|
65
73
|
* https://github.com/franckverrot/activevalidators
|
66
74
|
* https://github.com/hallelujah/valid_email
|
75
|
+
* https://github.com/micke/valid_email2
|
67
76
|
* https://github.com/validates-email-format-of/validates_email_format_of
|
68
77
|
|
69
78
|
### Clientside
|
@@ -17,9 +17,9 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.add_dependency "activemodel", ">= 4.0", "<
|
20
|
+
spec.add_dependency "activemodel", ">= 4.0", "< 7.0"
|
21
21
|
|
22
|
-
spec.add_development_dependency "bundler", "
|
23
|
-
spec.add_development_dependency "rake", "
|
22
|
+
spec.add_development_dependency "bundler", ">= 1.7"
|
23
|
+
spec.add_development_dependency "rake", ">= 10.0"
|
24
24
|
spec.add_development_dependency "minitest"
|
25
25
|
end
|
@@ -3,13 +3,45 @@ require "activemodel_email_address_validator/email_address"
|
|
3
3
|
|
4
4
|
class EmailAddressValidator < ActiveModel::EachValidator
|
5
5
|
def validate_each(record, attribute, value)
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
rules = Array(options[:with] || [])
|
7
|
+
if rules.empty?
|
8
|
+
# We are using the old syntax, assume regular expressions
|
9
|
+
rules = [build_rule_from_format(options[:format])]
|
10
|
+
end
|
11
|
+
|
12
|
+
invalidate(record, attribute) unless all_rules_pass?(rules, value)
|
9
13
|
end
|
10
14
|
|
11
15
|
private
|
12
16
|
|
17
|
+
def all_rules_pass?(rules, address)
|
18
|
+
evaluators = rules.map { |rule| build_evaluator(rule) }
|
19
|
+
results = evaluators.map { |evaluator| evaluator.call(address) }
|
20
|
+
results.all?
|
21
|
+
end
|
22
|
+
|
23
|
+
# Returns an evaluator for the given rule. Evaluators are objects that respond
|
24
|
+
# to `#call` with an arity of 1; the raw attribute value in question.
|
25
|
+
#
|
26
|
+
# The return-value of `#call` should be `true` if the address passes the given
|
27
|
+
# rule, false otherwise.
|
28
|
+
def build_evaluator(rule)
|
29
|
+
case rule
|
30
|
+
when Regexp
|
31
|
+
proc { |a| a =~ rule }
|
32
|
+
else
|
33
|
+
rule
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def build_rule_from_format(format)
|
38
|
+
proc { |attribute_value|
|
39
|
+
address =
|
40
|
+
ActiveModelEmailAddressValidator::EmailAddress.new(attribute_value)
|
41
|
+
address.valid?(format)
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
13
45
|
def invalidate(record, attribute)
|
14
46
|
record.errors.add(attribute, :invalid)
|
15
47
|
end
|
data/test/minitest_helper.rb
CHANGED
@@ -6,3 +6,22 @@ require "activemodel-email_address_validator"
|
|
6
6
|
|
7
7
|
require "minitest/autorun"
|
8
8
|
require "minitest/spec"
|
9
|
+
|
10
|
+
SIMPLEST_VALIDATION = {:email => {:email_address => true}}
|
11
|
+
|
12
|
+
# rubocop:disable Metrics/MethodLength
|
13
|
+
def build_model_with_validations(validations = SIMPLEST_VALIDATION)
|
14
|
+
klass = Class.new do
|
15
|
+
include ActiveModel::Validations
|
16
|
+
def self.model_name
|
17
|
+
ActiveModel::Name.new(self, nil, "ValidatorModel")
|
18
|
+
end
|
19
|
+
|
20
|
+
validations.each do |attribute, options|
|
21
|
+
attr_accessor attribute
|
22
|
+
validates attribute, options
|
23
|
+
end
|
24
|
+
end
|
25
|
+
klass.new
|
26
|
+
end
|
27
|
+
# rubocop:enable Metrics/MethodLength
|
@@ -1,24 +1,6 @@
|
|
1
1
|
require "minitest_helper"
|
2
2
|
require "active_model"
|
3
3
|
|
4
|
-
# rubocop:disable Metrics/MethodLength
|
5
|
-
SIMPLEST_VALIDATION = {:email => {:email_address => true}}
|
6
|
-
def build_model_with_validations(validations = SIMPLEST_VALIDATION)
|
7
|
-
klass = Class.new do
|
8
|
-
include ActiveModel::Validations
|
9
|
-
def self.model_name
|
10
|
-
ActiveModel::Name.new(self, nil, "ValidatorModel")
|
11
|
-
end
|
12
|
-
|
13
|
-
validations.each do |attribute, options|
|
14
|
-
attr_accessor attribute
|
15
|
-
validates attribute, options
|
16
|
-
end
|
17
|
-
end
|
18
|
-
klass.new
|
19
|
-
end
|
20
|
-
# rubocop:enable Metrics/MethodLength
|
21
|
-
|
22
4
|
class EmailAddressValidatorTest < MiniTest::Test
|
23
5
|
def setup
|
24
6
|
@subject = build_model_with_validations
|
@@ -28,15 +10,15 @@ class EmailAddressValidatorTest < MiniTest::Test
|
|
28
10
|
@subject = build_model_with_validations(
|
29
11
|
:email => {:email_address => true, :allow_nil => true}
|
30
12
|
)
|
31
|
-
accept(nil)
|
13
|
+
accept(nil, @subject)
|
32
14
|
end
|
33
15
|
|
34
16
|
def test_accepts_valid_email_address
|
35
|
-
accept("bob@example.com")
|
17
|
+
accept("bob@example.com", @subject)
|
36
18
|
end
|
37
19
|
|
38
20
|
def test_rejects_invalid_email_address
|
39
|
-
reject("bobexample.com")
|
21
|
+
reject("bobexample.com", @subject)
|
40
22
|
end
|
41
23
|
|
42
24
|
def test_adds_errors_to_validated_attribute
|
@@ -53,21 +35,50 @@ class EmailAddressValidatorTest < MiniTest::Test
|
|
53
35
|
subject = build_model_with_validations(
|
54
36
|
:email => {:email_address => {:format => /.+@enterprise\..+/}}
|
55
37
|
)
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
38
|
+
accept("whatever@enterprise.museum", subject)
|
39
|
+
reject("totally@valid.com", subject)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_validates_with_custom_regular_as_a_rule
|
43
|
+
subject = build_model_with_validations(
|
44
|
+
:email => {:email_address => {:with => /.+@enterprise\..+/}}
|
45
|
+
)
|
46
|
+
accept("whatever@enterprise.museum", subject)
|
47
|
+
reject("totally@valid.com", subject)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_validates_with_proc
|
51
|
+
subject = build_model_with_validations(
|
52
|
+
:email => {:email_address => {
|
53
|
+
:with => proc { |address| address == "foo" }}
|
54
|
+
}
|
55
|
+
)
|
56
|
+
accept("foo", subject)
|
57
|
+
reject("foo@bar.com", subject)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_validates_with_multiple_procs
|
61
|
+
subject = build_model_with_validations(
|
62
|
+
:email => {:email_address => {
|
63
|
+
:with => [
|
64
|
+
proc { |address| address == "ada" },
|
65
|
+
proc { |address| address.reverse == address }
|
66
|
+
]}
|
67
|
+
}
|
68
|
+
)
|
69
|
+
accept("ada", subject)
|
70
|
+
reject("bob", subject)
|
60
71
|
end
|
61
72
|
|
62
73
|
private
|
63
74
|
|
64
|
-
def accept(email_address)
|
65
|
-
|
66
|
-
assert
|
75
|
+
def accept(email_address, subject)
|
76
|
+
subject.email = email_address
|
77
|
+
assert subject.valid?, "Expected #{email_address.inspect} to be valid"
|
67
78
|
end
|
68
79
|
|
69
|
-
def reject(email_address)
|
70
|
-
|
71
|
-
assert
|
80
|
+
def reject(email_address, subject)
|
81
|
+
subject.email = email_address
|
82
|
+
assert !subject.valid?, "Expected #{email_address.inspect} to be invalid"
|
72
83
|
end
|
73
84
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activemodel-email_address_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jakob Skjerning
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '4.0'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '7.0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,33 +29,33 @@ dependencies:
|
|
29
29
|
version: '4.0'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '7.0'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: bundler
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- - "
|
37
|
+
- - ">="
|
38
38
|
- !ruby/object:Gem::Version
|
39
39
|
version: '1.7'
|
40
40
|
type: :development
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
|
-
- - "
|
44
|
+
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '1.7'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rake
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
|
-
- - "
|
51
|
+
- - ">="
|
52
52
|
- !ruby/object:Gem::Version
|
53
53
|
version: '10.0'
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
|
-
- - "
|
58
|
+
- - ">="
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: '10.0'
|
61
61
|
- !ruby/object:Gem::Dependency
|
@@ -113,8 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
113
|
- !ruby/object:Gem::Version
|
114
114
|
version: '0'
|
115
115
|
requirements: []
|
116
|
-
|
117
|
-
rubygems_version: 2.5.2
|
116
|
+
rubygems_version: 3.0.6
|
118
117
|
signing_key:
|
119
118
|
specification_version: 4
|
120
119
|
summary: ActiveModel-style email address format validator
|