Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  Bump version to `2.2.0`
  Rename `:strict` -> `:rfc`; `:moderate` -> `:strict`
  • Loading branch information
karlwilbur committed Dec 9, 2020
2 parents f3a9c4d + 1fd6240 commit 894ae01
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 239 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ This file is used to list changes made in `email_validator`.
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## 2.2.0 (2020-12-09)

* [karlwilbur] - Rename `:strict` -> `:rfc`; `:moderate` -> `:strict`

## 2.1.0 (2020-12-09)

* [karlwilbur] - Add linters and commit hooks to validate code prior to commits
Expand Down
56 changes: 28 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

An email validator for Rails 3+.

Supports RFC-2822-compliant and RFC-5321-compliant email validation.
Supports RFC-2822-compliant and RFC-5321-compliant email validation using RFC-3696 validation.

Formerly found at: <https://github.com/balexand/email_validator>

Expand All @@ -17,11 +17,11 @@ is extremely loose. It just checks that there's an `@` with something before and
after it without any whitespace. See [this article by David Gilbertson](https://hackernoon.com/the-100-correct-way-to-validate-email-addresses-7c4818f24643)
for an explanation of why.

We understand that maybe use cases require more stricy validation and this is
supported by using the `:moderate` validation mode. Additionally, the `:strict`
We understand that many use cases require an increased level of validation. This
is supported by using the `:strict` validation mode. Additionally, the `:rfc`
RFC-compliant mode will consider technically valid emails address as valid which
may not be wanted, such as the valid `user` or `user@somehost` addresses. These
would be valid in `:strict` mode but not valid in `:loose` or `:moderate`.
would be valid in `:rfc` mode but not valid in `:loose` or `:strict`.

## Installation

Expand All @@ -48,10 +48,10 @@ validates :my_email_attribute, email: true
You may wish to allow domains without a FDQN, like `user@somehost`. While this
is technically a valid address, it is uncommon to consider such address valid.
We will consider them valid by default with the `:loose` checking. Disallowed
by setting `require_fqdn: true` or by enabling `:moderate` checking:
by setting `require_fqdn: true` or by enabling `:strict` checking:

```ruby
validates :my_email_attribute, email: {mode: :moderate, require_fqdn: true}
validates :my_email_attribute, email: {mode: :strict, require_fqdn: true}
```

You can also limit to a single domain (e.g: this might help if, for example, you
Expand Down Expand Up @@ -86,67 +86,67 @@ end
This it the default validation mode of this gem. It is intentionally extremely
loose (see the [Validation Philosophy section](#validation_philosophy) above. It
just checks that there's an `@` with something before and after it without any
whitespace. The `:domain` and `:require_fqdn` option are ignored in `:loose` mode.
whitespace.

### Moderate mode
### Strict mode

Enabling `:moderate` checking will check for a "normal" email format that would
be expected in most common everyday usage. Moderate mode basically checks for a
Enabling `:strict` checking will check for a "normal" email format that would
be expected in most common everyday usage. Strict mode basically checks for a
properly sized and formatted mailbox label, a single "@" symbol, and a properly
sized and formatted FQDN. Enabling `:moderate` mode will also enable `:require_fqdn`
sized and formatted FQDN. Enabling `:strict` mode will also enable `:require_fqdn`
configuration option.

Moderate mode can be enabled globally by requiring `email_validator/moderate` in
Strict mode can be enabled globally by requiring `email_validator/strict` in
your `Gemfile`, by setting the option in `config/initializers/email_validator.rb`,
or by specifying the option in a specific `validates` call.

* `Gemfile`:

```ruby
gem 'email_validator', github: 'karlwilbur/email_validator', require: 'email_validator/moderate'
gem 'email_validator', require: 'email_validator/strict'
```

* `config/initializers/email_validator.rb`:

```ruby
if defined?(EmailValidator)
EmailValidator.default_options[:mode] = :moderate
EmailValidator.default_options[:mode] = :strict
end
```

* `validates` call:

```ruby
validates :my_email_attribute, email: {mode: :moderate}
validates :my_email_attribute, email: {mode: :strict}
```

### Strict mode
### RFC mode

In order to have stricter 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))
enable `:strict` mode.
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)),
enable `:rfc` mode.

You can do this globally by requiring `email_validator/strict` in your `Gemfile`,
You can do this globally by requiring `email_validator/rfc` in your `Gemfile`,
by setting the options in `config/initializers/email_validator.rb`, or you can do
this in a specific `validates` call.

* `Gemfile`:

```ruby
gem 'email_validator', github: 'karlwilbur/email_validator', require: 'email_validator/strict'
gem 'email_validator', require: 'email_validator/rfc'
```

* `config/initializers/email_validator.rb`:

```ruby
if defined?(EmailValidator)
EmailValidator.default_options[:mode] = :strict
EmailValidator.default_options[:mode] = :rfc
end
```

* `validates` call:

```ruby
validates :my_email_attribute, email: {mode: :strict}
validates :my_email_attribute, email: {mode: :rfc}
```

## Validation outside a model
Expand All @@ -173,18 +173,18 @@ EmailValidator.valid?('[email protected]', domain: 'foo.com') # boolean false
EmailValidator.invalid?('[email protected]', domain: 'foo.com') # boolean true
```

### Moderate mode
### Strict mode

```ruby
EmailValidator.regexp(mode: :moderate) # returns the regex
EmailValidator.valid?('[email protected]', mode: :moderate) # boolean
EmailValidator.regexp(mode: :strict) # returns the regex
EmailValidator.valid?('[email protected]', mode: :strict) # boolean
```

### Strict mode
### RFC mode

```ruby
EmailValidator.regexp(mode: :strict) # returns the regex
EmailValidator.valid?('[email protected]', mode: :strict) # boolean
EmailValidator.regexp(mode: :rfc) # returns the regex
EmailValidator.valid?('[email protected]', mode: :rfc) # boolean
```

## Thread safety
Expand Down
2 changes: 1 addition & 1 deletion email_validator.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'email_validator'
s.version = '2.1.0'
s.version = '2.2.0'
s.authors = ['Brian Alexander', 'Karl Wilbur']
s.summary = 'An email validator for Rails 3+.'
s.description = 'An email validator for Rails 3+. See homepage for details: http://github.com/K-and-R/email_validator'
Expand Down
4 changes: 2 additions & 2 deletions lib/email_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def domain_pattern(options)
private

def parse_options(options)
# `:moderate` mode enables `:require_fqdn`, unless it is already explicitly disabled
options[:require_fqdn] = true if options[:require_fqdn].nil? && options[:mode] == :moderate
# `:strict` mode enables `:require_fqdn`, unless it is already explicitly disabled
options[:require_fqdn] = true if options[:require_fqdn].nil? && options[:mode] == :strict
default_options.merge(options)
end
end
Expand Down
4 changes: 0 additions & 4 deletions lib/email_validator/moderate.rb

This file was deleted.

4 changes: 4 additions & 0 deletions lib/email_validator/rfc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# require this file to enable `:rfc` mode by default

require 'email_validator'
EmailValidator.default_options[:mode] = :rfc
Loading

0 comments on commit 894ae01

Please sign in to comment.