email_validator 1.6.0 → 2.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 3a61b5f5394d7e3995cb72b8020ed8877539658f
4
- data.tar.gz: e1781250b91b5b62faf766b6120c0e91ae2cbd0e
2
+ SHA256:
3
+ metadata.gz: faae1443109b7d1c95aca6addd9d2be7c3cfaf58a771e003d80a1ea75dd0993f
4
+ data.tar.gz: c1094ac3f9d4887193eb2145c62f46cefe1d4ed0a734a6d7add1cf68222de1dd
5
5
  SHA512:
6
- metadata.gz: 7c0d25c20be026d62ef9bda3a775efe42e21f87be95323f3fbd5be16df2ed761d31066c0d54a3578ef1b7f22562e09df80152de910dd6a6517de936efce70279
7
- data.tar.gz: 2c6bd876e48aa9f27ac6d2ce53942961b52207cfe508d80dad93cd9df2ed0b0d1bc346ca3fc1ff8c218463b499b7d2fa5630af5374c3df013aec9cce183d93b7
6
+ metadata.gz: e99e6549ebfa01f6fc43b0a8f8bfba80ce0037700f2672be89bb0936b71f929bde127d667fc634a38bc6ea9e26d08a61393c5f261db3581362f8497001305790
7
+ data.tar.gz: 7e74593c1bb75c71d8864c1db31d9a415ec6fbc4d56a7f190363224f3906b884f0bd39af9208969112f12978b733ae09271e3a94b42ed1fc84b88553cd71df9d
@@ -0,0 +1,92 @@
1
+ # CHANGELOG
2
+
3
+ This file is used to list changes made in `email_validator`.
4
+
5
+ All notable changes to this project will be documented in this file.
6
+ This project adheres to [Semantic Versioning](http://semver.org/).
7
+
8
+ ## 2.2.1 (2020-12-10)
9
+
10
+ * [karlwilbur] - Modify regexp to:
11
+ - allow numeric-only hosts [#68]
12
+ - allow mailbox-only addresses in `:rfc` mode
13
+ - enforce the 255-char domain limit (not in `:loose` mode unless using `:domain`)
14
+
15
+ ## 2.2.0 (2020-12-09)
16
+
17
+ * [karlwilbur] - Rename `:strict` -> `:rfc`; `:moderate` -> `:strict`
18
+
19
+ ## 2.1.0 (2020-12-09)
20
+
21
+ * [karlwilbur] - Add linters and commit hooks to validate code prior to commits
22
+ * [karlwilbur] - Add `:mode` config option; values `:loose`, `:moderate`, `:strict`; default to `:loose`
23
+ * [karlwilbur] - Merge in changes from <https://github.com/karlwilbur/email_validator> fork
24
+
25
+ ## 1.9.0.pre (2020-10-14)
26
+
27
+ * [karlwilbur] - Add `require_fqdn` option, require FQDN by default
28
+ * [karlwilbur] - Add support for IPv4 and IPv6 address hosts
29
+ * [karlwilbur] - Add Rubocop, `.editorconfig`; code cleanup/linting
30
+
31
+ ## 1.8.0 (2019-06-14)
32
+
33
+ * [karlwilbur] - Refactor class methods for readability
34
+ * [karlwilbur] - `gemspec` meta updates
35
+ * [karlwilbur] - Use POSIX classes for better performance
36
+ * [karlwilbur] - Refactored tests to check specical characters one at a time
37
+ * [karlwilbur] - Refactored validation regex to be more techincally correct
38
+ * [karlwilbur] - Add this `CHANGELOG`
39
+
40
+ ## 1.7.0 (2019-04-20)
41
+
42
+ * [karlwilbur] - Added test coverage badge to README
43
+ * [karlwilbur] - Added I18n directive to remove warning message in testing
44
+ * [karlwilbur] - Added RFC-2822 reference
45
+ * [karlwilbur] - Ignore local rspec config file
46
+ * [karlwilbur] - Check for invalid double dots in strict mode
47
+ * [karlwilbur] - Updated spec_helper to remove Code Climate Test Reporter; it is to be run separately now
48
+ * [karlwilbur] - Allow leading/trailing whitespace in normal, not strict
49
+ * [karlwilbur] - Added `invalid?` as inverse of `valid?`
50
+ * [karlwilbur] - Add the ability to limit to a domain
51
+ * [karlwilbur] - Removed CodeShip badge
52
+ * [karlwilbur] - Make the dot in the domain part non-conditional
53
+ * [karlwilbur] - Fix domain label pattern to allow numbers per rfc5321
54
+
55
+ ## 1.6.0 (2015-06-14)
56
+
57
+ * [karlwilbur] - Fixed validation to be closer to RFC-5321
58
+ * [karlwilbur] - Updated specs to use Rspec 3 syntax
59
+ * [karlwilbur] - Added unicode suport to validation regexp
60
+ * [karlwilbur] - Added class access to regexp, and `valid?` calss method
61
+ * [karlwilbur] - Simplified code using new methods
62
+ * [karlwilbur] - Added CodeClimate and SimpleCov
63
+ * [karlwilbur] - Updated version and contact info
64
+
65
+ *** Forked from <https://github.com/balexand/email_validator>
66
+
67
+ ## 2.0.1 (2019-03-09)
68
+
69
+ * Add email value to error details [f1sherman #50]
70
+ * CI doesn't test Ruby versions that no longer receive updates [f1sherman #51]
71
+
72
+ ## 2.0.0 (2019-03-02)
73
+
74
+ * Looser validation [#49]
75
+
76
+ ## 1.6.0 (2015-05-12)
77
+
78
+ * Unicode characters support [i7an #24]
79
+
80
+ ## 1.5.0 (2014-12-08)
81
+
82
+ * Add a class method for simpler validation [TiteiKo and cluesque #19]
83
+ * RSpec 3.0 syntax [strivedi183 #17]
84
+ * Create Changes.md
85
+
86
+ ---
87
+
88
+ Check the [Markdown Syntax Guide](http://daringfireball.net/projects/markdown/syntax)
89
+ for help with Markdown.
90
+
91
+ The [Github Flavored Markdown page](http://github.github.com/github-flavored-markdown/)
92
+ describes the differences between markdown on github and standard markdown.
data/README.md CHANGED
@@ -1,6 +1,29 @@
1
- [![Build Status](https://secure.travis-ci.org/balexand/email_validator.png)](http://travis-ci.org/balexand/email_validator)
1
+ # EmailValidator
2
2
 
3
- ## Usage
3
+ [![Build Status](https://travis-ci.com/K-and-R/email_validator.svg?branch=master)](http://travis-ci.com/K-and-R/email_validator)
4
+ [![Code Climate](https://codeclimate.com/github/K-and-R/email_validator/badges/gpa.svg)](https://codeclimate.com/github/K-and-R/email_validator)
5
+ [![Test Coverage](https://codeclimate.com/github/K-and-R/email_validator/badges/coverage.svg)](https://codeclimate.com/github/K-and-R/email_validator/coverage)
6
+
7
+ An email validator for Rails 3+.
8
+
9
+ Supports RFC-2822-compliant and RFC-5321-compliant email validation using RFC-3696 validation.
10
+
11
+ Formerly found at: <https://github.com/balexand/email_validator>
12
+
13
+ ## Validation philosophy
14
+
15
+ The default validation provided by this gem (the `:loose` configuration option)
16
+ is extremely loose. It just checks that there's an `@` with something before and
17
+ after it without any whitespace. See [this article by David Gilbertson](https://hackernoon.com/the-100-correct-way-to-validate-email-addresses-7c4818f24643)
18
+ for an explanation of why.
19
+
20
+ We understand that many use cases require an increased level of validation. This
21
+ is supported by using the `:strict` validation mode. Additionally, the `:rfc`
22
+ RFC-compliant mode will consider technically valid emails address as valid which
23
+ may not be wanted, such as the valid `user` or `user@somehost` addresses. These
24
+ would be valid in `:rfc` mode but not valid in `:loose` or `:strict`.
25
+
26
+ ## Installation
4
27
 
5
28
  Add to your Gemfile:
6
29
 
@@ -10,54 +33,181 @@ gem 'email_validator'
10
33
 
11
34
  Run:
12
35
 
13
- ```
36
+ ```bash
14
37
  bundle install
15
38
  ```
16
39
 
17
- Then add the following to your model:
40
+ ## Usage
41
+
42
+ Add the following to your model:
18
43
 
19
44
  ```ruby
20
- validates :my_email_attribute, :email => true
45
+ validates :my_email_attribute, email: true
21
46
  ```
22
47
 
23
- ## Strict mode
48
+ You may wish to allow domains without a FQDN, like `user@somehost`. While this
49
+ is technically a valid address, it is uncommon to consider such address valid.
50
+ We will consider them valid by default with the `:loose` checking. Disallowed
51
+ by setting `require_fqdn: true` or by enabling `:strict` checking:
52
+
53
+ ```ruby
54
+ validates :my_email_attribute, email: {mode: :strict, require_fqdn: true}
55
+ ```
24
56
 
25
- In order to have stricter validation (according to http://www.remote.org/jochen/mail/info/chars.html) enable strict mode. You can do this globally by adding the following to your Gemfile:
57
+ You can also limit to a single domain (e.g: this might help if, for example, you
58
+ have separate `User` and `AdminUser` models and want to ensure that `AdminUser`
59
+ emails are on a specific domain):
26
60
 
27
61
  ```ruby
28
- gem 'email_validator', :require => 'email_validator/strict'
62
+ validates :my_email_attribute, email: {domain: 'example.com'}
29
63
  ```
30
64
 
31
- Or you can do this in a specific `validates` call:
65
+ ## Configuration
66
+
67
+ Default configuration can be overridden by setting options in `config/initializers/email_validator.rb`:
32
68
 
33
69
  ```ruby
34
- validates :my_email_attribute, :email => {:strict_mode => true}
70
+ if defined?(EmailValidator)
71
+ # To completly override the defaults
72
+ EmailValidator.default_options = {
73
+ allow_nil: false,
74
+ domain: nil,
75
+ require_fqdn: nil,
76
+ mode: :loose
77
+ }
78
+
79
+ # or just a few options
80
+ EmailValidator.default_options.merge!({ domain: 'mydomain.com' })
81
+ end
35
82
  ```
36
83
 
84
+ ### Loose mode
85
+
86
+ This it the default validation mode of this gem. It is intentionally extremely
87
+ loose (see the [Validation Philosophy section](#validation_philosophy) above. It
88
+ just checks that there's an `@` with something before and after it without any
89
+ whitespace.
90
+
91
+ ### Strict mode
92
+
93
+ Enabling `:strict` checking will check for a "normal" email format that would
94
+ be expected in most common everyday usage. Strict mode basically checks for a
95
+ properly sized and formatted mailbox label, a single "@" symbol, and a properly
96
+ sized and formatted FQDN. Enabling `:strict` mode will also enable `:require_fqdn`
97
+ configuration option.
98
+
99
+ Strict mode can be enabled globally by requiring `email_validator/strict` in
100
+ your `Gemfile`, by setting the option in `config/initializers/email_validator.rb`,
101
+ or by specifying the option in a specific `validates` call.
102
+
103
+ * `Gemfile`:
104
+
105
+ ```ruby
106
+ gem 'email_validator', require: 'email_validator/strict'
107
+ ```
108
+
109
+ * `config/initializers/email_validator.rb`:
110
+
111
+ ```ruby
112
+ if defined?(EmailValidator)
113
+ EmailValidator.default_options[:mode] = :strict
114
+ end
115
+ ```
116
+
117
+ * `validates` call:
118
+
119
+ ```ruby
120
+ validates :my_email_attribute, email: {mode: :strict}
121
+ ```
122
+
123
+ ### RFC mode
124
+
125
+ In order to have RFC-compliant validation (according to [http://www.remote.org/jochen/mail/info/chars.html](https://web.archive.org/web/20150508102948/http://www.remote.org/jochen/mail/info/chars.html)),
126
+ enable `:rfc` mode.
127
+
128
+ You can do this globally by requiring `email_validator/rfc` in your `Gemfile`,
129
+ by setting the options in `config/initializers/email_validator.rb`, or you can do
130
+ this in a specific `validates` call.
131
+
132
+ * `Gemfile`:
133
+
134
+ ```ruby
135
+ gem 'email_validator', require: 'email_validator/rfc'
136
+ ```
137
+
138
+ * `config/initializers/email_validator.rb`:
139
+
140
+ ```ruby
141
+ if defined?(EmailValidator)
142
+ EmailValidator.default_options[:mode] = :rfc
143
+ end
144
+ ```
145
+
146
+ * `validates` call:
147
+
148
+ ```ruby
149
+ validates :my_email_attribute, email: {mode: :rfc}
150
+ ```
151
+
37
152
  ## Validation outside a model
38
153
 
39
- If you need to validate an email outside a model, you can get the regexp :
154
+ If you need to validate an email outside a model, you can get the regexp:
40
155
 
41
- ### Normal mode
156
+ ### Loose/default mode
42
157
 
43
158
  ```ruby
44
- EmailValidator.regexp # returns the regex
45
159
  EmailValidator.valid?('narf@example.com') # boolean
46
160
  ```
47
161
 
162
+ ### Requiring a FQDN
163
+
164
+ ```ruby
165
+ EmailValidator.valid?('narf@somehost') # boolean false
166
+ EmailValidator.invalid?('narf@somehost', require_fqdn: false) # boolean true
167
+ ```
168
+
169
+ ### Requiring a specific domain
170
+
171
+ ```ruby
172
+ EmailValidator.valid?('narf@example.com', domain: 'foo.com') # boolean false
173
+ EmailValidator.invalid?('narf@example.com', domain: 'foo.com') # boolean true
174
+ ```
175
+
48
176
  ### Strict mode
49
177
 
50
178
  ```ruby
51
- EmailValidator.regexp(:strict_mode => true)
179
+ EmailValidator.regexp(mode: :strict) # returns the regex
180
+ EmailValidator.valid?('narf@example.com', mode: :strict) # boolean
181
+ ```
182
+
183
+ ### RFC mode
184
+
185
+ ```ruby
186
+ EmailValidator.regexp(mode: :rfc) # returns the regex
187
+ EmailValidator.valid?('narf@example.com', mode: :rfc) # boolean
52
188
  ```
53
189
 
54
190
  ## Thread safety
55
191
 
56
- This gem is thread safe, with one caveat: `EmailValidator.default_options` must be configured before use in a multi-threaded environment. If you configure `default_options` in a Rails initializer file, then you're good to go since initializers are run before worker threads are spawned.
192
+ This gem is thread safe, with one caveat: `EmailValidator.default_options` must
193
+ be configured before use in a multi-threaded environment. If you configure
194
+ `default_options` in a Rails initializer file, then you're good to go since
195
+ initializers are run before worker threads are spawned.
196
+
197
+ ## Alternative gems
198
+
199
+ Do you prefer a different email validation gem? If so, open an issue with a brief
200
+ explanation of how it differs from this gem. I'll add a link to it in this README.
201
+
202
+ * [`email_address`](https://github.com/afair/email_address) (<https://github.com/K-and-R/email_validator/issues/58>)
203
+ * [`email_verifier`](https://github.com/kamilc/email_verifier) (<https://github.com/K-and-R/email_validator/issues/65>)
57
204
 
58
- ## Credit
205
+ ## Maintainers
59
206
 
60
- Based on http://thelucid.com/2010/01/08/sexy-validation-in-edge-rails-rails-3
207
+ All thanks is given to [Brian Alexander (balexand)](https://github.com/balexand)
208
+ for is initial work on this gem.
61
209
 
62
- Regular Expression based on http://fightingforalostcause.net/misc/2006/compare-email-regex.php tests.
210
+ Currently maintained by:
63
211
 
212
+ * Karl Wilbur (<https://github.com/karlwilbur>)
213
+ * K&R Software (<https://github.com/K-and-R>)
@@ -1,29 +1,145 @@
1
- # encoding: UTF-8
2
1
  # Based on work from http://thelucid.com/2010/01/08/sexy-validation-in-edge-rails-rails-3/
3
2
  class EmailValidator < ActiveModel::EachValidator
4
- @@default_options = {}
3
+ # rubocop:disable Style/ClassVars
4
+ @@default_options = {
5
+ :allow_nil => false,
6
+ :domain => nil,
7
+ :require_fqdn => nil,
8
+ :mode => :loose
9
+ }
10
+ # rubocop:enable Style/ClassVars
5
11
 
6
- def self.regexp(options = {})
7
- options = default_options.merge(options)
12
+ class << self
13
+ def default_options
14
+ @@default_options
15
+ end
8
16
 
9
- name_validation = options[:strict_mode] ? "-\\p{L}\\d+._" : "^@\\s"
17
+ def valid?(value, options = {})
18
+ options = parse_options(options)
19
+ return true if value.nil? && options[:allow_nil] == true
20
+ return false if value.nil?
21
+ !!(value =~ regexp(options))
22
+ end
10
23
 
11
- /\A\s*([#{name_validation}]{1,64})@((?:[-\p{L}\d]+\.)+\p{L}{2,})\s*\z/i
12
- end
24
+ def invalid?(value, options = {})
25
+ !valid?(value, options)
26
+ end
13
27
 
14
- def self.valid?(value, options = {})
15
- !!(value =~ regexp(options))
16
- end
28
+ # Refs:
29
+ # https://tools.ietf.org/html/rfc2822 : 3.2. Lexical Tokens, 3.4.1. Addr-spec specification
30
+ # https://tools.ietf.org/html/rfc5321 : 4.1.2. Command Argument Syntax
31
+ def regexp(options = {})
32
+ options = parse_options(options)
33
+ case options[:mode]
34
+ when :loose
35
+ loose_regexp(options)
36
+ when :rfc
37
+ rfc_regexp(options)
38
+ else
39
+ strict_regexp(options)
40
+ end
41
+ end
42
+
43
+ protected
44
+
45
+ def loose_regexp(options = {})
46
+ return /\A[^\s]+@[^\s]+\z/ if options[:domain].nil?
47
+ /\A[^\s]+@#{domain_part_pattern(options)}\z/
48
+ end
49
+
50
+ def strict_regexp(options = {})
51
+ /\A(?>#{local_part_pattern})@#{domain_part_pattern(options)}\z/i
52
+ end
53
+
54
+ def rfc_regexp(options = {})
55
+ /\A(?>#{local_part_pattern})(?:@#{domain_part_pattern(options)})?\z/i
56
+ end
57
+
58
+ def alpha
59
+ '[[:alpha:]]'
60
+ end
61
+
62
+ def alnum
63
+ '[[:alnum:]]'
64
+ end
65
+
66
+ def alnumhy
67
+ "(?:#{alnum}|-)"
68
+ end
69
+
70
+ def ipv4
71
+ '\d{1,3}(?:\.\d{1,3}){3}'
72
+ end
17
73
 
18
- def self.default_options
19
- @@default_options
74
+ def ipv6
75
+ # only supporting full IPv6 addresses right now
76
+ 'IPv6:[[:xdigit:]]{1,4}(?::[[:xdigit:]]{1,4}){7}'
77
+ end
78
+
79
+ def address_literal
80
+ "\\[(?:#{ipv4}|#{ipv6})\\]"
81
+ end
82
+
83
+ def host_label_pattern
84
+ "#{alnum}(?:#{alnumhy}{,61}#{alnum})?"
85
+ end
86
+
87
+ # splitting this up into separate regex pattern for performance; let's not
88
+ # try the "contains" pattern unless we have to
89
+ def domain_label_pattern
90
+ '(?=[^.]{1,63}(?:\.|$))' \
91
+ '(?:' \
92
+ "#{alpha}" \
93
+ "|#{domain_label_starts_with_a_letter_pattern}" \
94
+ "|#{domain_label_ends_with_a_letter_pattern}" \
95
+ "|#{domain_label_contains_a_letter_pattern}" \
96
+ ')'
97
+ end
98
+
99
+ def domain_label_starts_with_a_letter_pattern
100
+ "#{alpha}#{alnumhy}{,61}#{alnum}"
101
+ end
102
+
103
+ def domain_label_ends_with_a_letter_pattern
104
+ "#{alnum}#{alnumhy}{,61}#{alpha}"
105
+ end
106
+
107
+ def domain_label_contains_a_letter_pattern
108
+ "(?:[[:digit:]])(?:[[:digit:]]|-)*#{alpha}#{alnumhy}*#{alnum}"
109
+ end
110
+
111
+ def atom_char
112
+ # The `atext` spec
113
+ # We are looking at this without whitespace; no whitespace support here
114
+ "[-#{alpha}#{alnum}+_!\"'#$%^&*{}/=?`|~]"
115
+ end
116
+
117
+ def local_part_pattern
118
+ # the `dot-atom-text` spec, but with a 64 character limit
119
+ "#{atom_char}(?:\\.?#{atom_char}){,63}"
120
+ end
121
+
122
+ def domain_part_pattern(options)
123
+ return options[:domain].sub(/\./, '\.') if options[:domain].present?
124
+ return fqdn_pattern if options[:require_fqdn]
125
+ "(?=.{1,255}$)(?:#{address_literal}|(?:#{host_label_pattern}\\.)*#{domain_label_pattern})"
126
+ end
127
+
128
+ def fqdn_pattern
129
+ "(?=.{1,255}$)(?:#{host_label_pattern}\\.)*#{domain_label_pattern}\\.#{domain_label_pattern}"
130
+ end
131
+
132
+ private
133
+
134
+ def parse_options(options)
135
+ # `:strict` mode enables `:require_fqdn`, unless it is already explicitly disabled
136
+ options[:require_fqdn] = true if options[:require_fqdn].nil? && options[:mode] == :strict
137
+ default_options.merge(options)
138
+ end
20
139
  end
21
140
 
22
141
  def validate_each(record, attribute, value)
23
142
  options = @@default_options.merge(self.options)
24
-
25
- unless self.class.valid?(value, self.options)
26
- record.errors.add(attribute, options[:message] || :invalid)
27
- end
143
+ record.errors.add(attribute, options[:message] || :invalid) unless self.class.valid?(value, options)
28
144
  end
29
145
  end