Skip to content

Commit

Permalink
Rewrite valid? and fix specs
Browse files Browse the repository at this point in the history
  • Loading branch information
corny committed Jun 13, 2020
1 parent 6d7b91c commit 4954cc7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
17 changes: 8 additions & 9 deletions lib/valid_email2/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,24 @@ def initialize(address)
end

def valid?
@valid ||= begin
return false if @parse_error
return @valid if @valid != nil
return false if @parse_error

@valid = begin
if address.domain && address.address == @raw_address
domain = address.domain

domain !~ PROHIBITED_DOMAIN_CHARACTERS_REGEX &&
# Domain needs to have at least one dot
domain =~ /\./ &&
domain.include?('.') &&
# Domain may not have two consecutive dots
domain !~ /\.{2,}/ &&
!domain.include?('..') &&
# Domain may not start with a dot
domain !~ /^\./ &&
!domain.start_with?('.') &&
# Domain may not start with a dash
domain !~ /^-/ &&
!domain.start_with?('-') &&
# Domain name may not end with a dash
domain !~ /-\./ &&
# Address may not contain a dot directly before @
address.address !~ /\.@/
!domain.include?('-.')
else
false
end
Expand Down
7 changes: 1 addition & 6 deletions spec/valid_email2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,11 @@ class TestUserMessage < TestModel
expect(user.valid?).to be_falsey
end

it "is invalid if the domain contains emoticons" do
it "is invalid if the email contains emoticons" do
user = TestUser.new(email: "foo🙈@gmail.com")
expect(user.valid?).to be_falsy
end

it "is invalid if the domain contains .@ consecutively" do
user = TestUser.new(email: "foo.@gmail.com")
expect(user.valid?).to be_falsy
end

it "is invalid if the domain contains spaces" do
user = TestUser.new(email: "user@gmail .com")
expect(user.valid?).to be_falsy
Expand Down

0 comments on commit 4954cc7

Please sign in to comment.