validators 2.4.2 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ module Validators
2
+ class DisposableHostnames
3
+ FILE_PATH = File.expand_path("../../../data/disposable.json", __FILE__)
4
+
5
+ def self.all
6
+ @tld ||= JSON.load(File.read(FILE_PATH))
7
+ end
8
+ end
9
+ end
@@ -1,9 +1,9 @@
1
1
  module Validators
2
2
  class TLD
3
- TLD_FILE_PATH = File.expand_path('../../../data/tld.txt', __FILE__)
3
+ FILE_PATH = File.expand_path("../../../data/tld.json", __FILE__)
4
4
 
5
5
  def self.all
6
- @tld ||= File.read(TLD_FILE_PATH).lines.map(&:chomp)
6
+ @tld ||= JSON.load(File.read(FILE_PATH))
7
7
  end
8
8
 
9
9
  def self.host_with_valid_tld?(host)
@@ -2,9 +2,16 @@ module ActiveModel
2
2
  module Validations
3
3
  class EmailValidator < EachValidator
4
4
  def validate_each(record, attribute, value)
5
+ allow_disposable = options.fetch(:disposable, false)
6
+
5
7
  return if value.blank? && options[:allow_blank]
6
8
  return if value.nil? && options[:allow_nil]
7
9
 
10
+ validate_email_format(record, attribute, value, options)
11
+ validate_disposable_email(record, attribute, value, options) unless allow_disposable
12
+ end
13
+
14
+ def validate_email_format(record, attribute, value, options)
8
15
  if value.to_s !~ Validators::EMAIL_FORMAT
9
16
  record.errors.add(
10
17
  attribute, :invalid_email,
@@ -12,6 +19,15 @@ module ActiveModel
12
19
  )
13
20
  end
14
21
  end
22
+
23
+ def validate_disposable_email(record, attribute, value, options)
24
+ hostname = value.to_s.split("@").last
25
+
26
+ record.errors.add(
27
+ attribute, :disposable_email,
28
+ :value => value
29
+ ) if Validators::DisposableHostnames.all.include?(hostname)
30
+ end
15
31
  end
16
32
 
17
33
  module ClassMethods
@@ -1,8 +1,8 @@
1
1
  module Validators
2
2
  module Version
3
3
  MAJOR = 2
4
- MINOR = 4
5
- PATCH = 2
4
+ MINOR = 5
5
+ PATCH = 0
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
data/lib/validators.rb CHANGED
@@ -1,7 +1,10 @@
1
+ require "json"
2
+
1
3
  module Validators
2
4
  require "validators/constants"
3
5
  require "validators/ip"
4
6
  require "validators/tld"
7
+ require "validators/disposable_hostnames"
5
8
  require "validators/validates_datetime"
6
9
  require "validators/validates_ip_address"
7
10
  require "validators/validates_email_format_of"
data/spec/spec_helper.rb CHANGED
@@ -7,7 +7,8 @@ require "validators"
7
7
  require "active_support/all"
8
8
 
9
9
  Time.zone = "America/Sao_Paulo"
10
- TLDs = Validators::TLD.all.sample(100)
10
+ TLDs = Validators::TLD.all.sample(50)
11
+ DISPOSABLE_EMAILS = Validators::DisposableHostnames.all.sample(50)
11
12
 
12
13
  Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
13
14
 
@@ -14,6 +14,7 @@ en:
14
14
  invalid_owner: "is not associated with your user"
15
15
  invalid_cpf: "is not a valid CPF"
16
16
  invalid_cnpj: "is not a valid CNPJ"
17
+ disposable_email: "is not allowed (high-bounce domain)"
17
18
 
18
19
  activemodel:
19
20
  <<: *activerecord
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ describe ".validates_email_format_of" do
4
+ context "when using disposable e-mail" do
5
+ before do
6
+ User.validates_email_format_of :email
7
+ end
8
+
9
+ DISPOSABLE_EMAILS.each do |domain|
10
+ it "rejects disposable e-mail (#{domain})" do
11
+ user = User.new(email: "user@#{domain}")
12
+ user.valid?
13
+
14
+ expect(user.errors[:email]).to include(I18n.t('activerecord.errors.messages.disposable_email'))
15
+ end
16
+ end
17
+ end
18
+
19
+ context "disable disposable e-mail" do
20
+ before do
21
+ User.validates_email_format_of :email, disposable: true
22
+ end
23
+
24
+ it "accepts e-mail" do
25
+ user = User.new(email: "user@mailinator.com")
26
+ user.valid?
27
+
28
+ expect(user.errors[:email]).to be_empty
29
+ end
30
+ end
31
+ end
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  require "spec_helper"
3
2
 
4
3
  describe ".validates_email_format_of" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.2
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-01 00:00:00.000000000 Z
11
+ date: 2015-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -133,12 +133,13 @@ files:
133
133
  - ".rspec"
134
134
  - ".travis.yml"
135
135
  - Gemfile
136
- - Gemfile.lock
137
136
  - README.md
138
137
  - Rakefile
139
- - data/tld.txt
138
+ - data/disposable.json
139
+ - data/tld.json
140
140
  - lib/validators.rb
141
141
  - lib/validators/constants.rb
142
+ - lib/validators/disposable_hostnames.rb
142
143
  - lib/validators/ip.rb
143
144
  - lib/validators/tld.rb
144
145
  - lib/validators/validates_cnpj_format_of.rb
@@ -161,6 +162,7 @@ files:
161
162
  - spec/support/models.rb
162
163
  - spec/support/translations.yml
163
164
  - spec/support/urls.rb
165
+ - spec/validators/disposable_email_spec.rb
164
166
  - spec/validators/ip_spec.rb
165
167
  - spec/validators/validates_cnpj_format_of_spec.rb
166
168
  - spec/validators/validates_cpf_format_of_spec.rb
@@ -206,6 +208,7 @@ test_files:
206
208
  - spec/support/models.rb
207
209
  - spec/support/translations.yml
208
210
  - spec/support/urls.rb
211
+ - spec/validators/disposable_email_spec.rb
209
212
  - spec/validators/ip_spec.rb
210
213
  - spec/validators/validates_cnpj_format_of_spec.rb
211
214
  - spec/validators/validates_cpf_format_of_spec.rb
data/Gemfile.lock DELETED
@@ -1,94 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- validators (2.4.2)
5
-
6
- GEM
7
- remote: http://rubygems.org/
8
- specs:
9
- activemodel (4.2.1)
10
- activesupport (= 4.2.1)
11
- builder (~> 3.1)
12
- activerecord (4.2.1)
13
- activemodel (= 4.2.1)
14
- activesupport (= 4.2.1)
15
- arel (~> 6.0)
16
- activesupport (4.2.1)
17
- i18n (~> 0.7)
18
- json (~> 1.7, >= 1.7.7)
19
- minitest (~> 5.1)
20
- thread_safe (~> 0.3, >= 0.3.4)
21
- tzinfo (~> 1.1)
22
- arel (6.0.0)
23
- awesome_print (1.6.1)
24
- builder (3.2.2)
25
- byebug (4.0.4)
26
- columnize (= 0.9.0)
27
- codeclimate-test-reporter (0.4.7)
28
- simplecov (>= 0.7.1, < 1.0.0)
29
- coderay (1.1.0)
30
- columnize (0.9.0)
31
- cpf_cnpj (0.2.1)
32
- diff-lcs (1.2.5)
33
- docile (1.1.5)
34
- i18n (0.7.0)
35
- json (1.8.2)
36
- method_source (0.8.2)
37
- minitest (5.5.1)
38
- multi_json (1.11.0)
39
- pry (0.10.1)
40
- coderay (~> 1.1.0)
41
- method_source (~> 0.8.1)
42
- slop (~> 3.4)
43
- pry-byebug (3.1.0)
44
- byebug (~> 4.0)
45
- pry (~> 0.10)
46
- pry-meta (0.0.10)
47
- awesome_print
48
- pry
49
- pry-byebug
50
- pry-remote
51
- pry-remote (0.1.8)
52
- pry (~> 0.9)
53
- slop (~> 3.0)
54
- rake (10.4.2)
55
- rspec (3.2.0)
56
- rspec-core (~> 3.2.0)
57
- rspec-expectations (~> 3.2.0)
58
- rspec-mocks (~> 3.2.0)
59
- rspec-core (3.2.2)
60
- rspec-support (~> 3.2.0)
61
- rspec-expectations (3.2.0)
62
- diff-lcs (>= 1.2.0, < 2.0)
63
- rspec-support (~> 3.2.0)
64
- rspec-mocks (3.2.1)
65
- diff-lcs (>= 1.2.0, < 2.0)
66
- rspec-support (~> 3.2.0)
67
- rspec-support (3.2.2)
68
- simplecov (0.9.2)
69
- docile (~> 1.1.0)
70
- multi_json (~> 1.0)
71
- simplecov-html (~> 0.9.0)
72
- simplecov-html (0.9.0)
73
- slop (3.6.0)
74
- sqlite3 (1.3.10)
75
- sqlite3-ruby (1.3.3)
76
- sqlite3 (>= 1.3.3)
77
- sshkey (1.6.1)
78
- thread_safe (0.3.5)
79
- tzinfo (1.2.2)
80
- thread_safe (~> 0.1)
81
-
82
- PLATFORMS
83
- ruby
84
-
85
- DEPENDENCIES
86
- activerecord (>= 3.0)
87
- codeclimate-test-reporter
88
- cpf_cnpj
89
- pry-meta
90
- rake
91
- rspec
92
- sqlite3-ruby
93
- sshkey
94
- validators!