Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ source "http://rubygems.org"
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
gem "bundler", "~> 1.0.0"
gem "jeweler", "~> 1.5.2"
gem "bundler"
gem "jeweler"
gem 'simplecov', '>= 0.3.8', :require => false
gem "idn", :git => "git://github.com/mihu/idn.git"
gem "rspec", "~> 2.3.0"
gem "yard", "~> 0.6.0"
gem "rspec"
gem "yard"
gem "idn-ruby"
end
21 changes: 21 additions & 0 deletions lib/rails_email_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ def has_mx?(domain)
not mx.nil? and mx.size > 0
end


# validate if an a exists on domain
def has_a?(domain)
require 'resolv'
a = []
Resolv::DNS.open do |dns|
a = dns.getresources(domain, Resolv::DNS::Resource::IN::A)
end
not a.nil? and a.size > 0
end

# convert an unicode domain_part to asccii for validation
def convert_idn(domain_part)
if allow_idn?
Expand Down Expand Up @@ -64,8 +75,18 @@ def validate_each(record, attribute, value)
# check mx
if valid and validate_mx?
valid = false unless has_mx? domain_part

# check a
if !valid and validate_mx?
if has_a? domain_part
valid = true
end
end

end



# email valid
record.errors.add(attribute, :invalid) unless valid
end
Expand Down
3 changes: 2 additions & 1 deletion spec/rails_email_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class ValidateDefaultEmail < ValidateEmail
it "should validate without errors" do
[
'[email protected]',
'[email protected]'
'[email protected]',
'[email protected]',
].each do |email|

@object.email = email
Expand Down