Sha256: f52918a73538d7229fe3731e28e6a67a87ed898a9c9866399f2f8274e372f2be

Contents?: true

Size: 786 Bytes

Versions: 29

Compression:

Stored size: 786 Bytes

Contents

require 'mail'
class EmailValidator < ActiveModel::EachValidator
  def validate_each(record,attribute,value)
    begin
      m = Mail::Address.new(value)
      # We must check that value contains a domain and that value is an email address
      r = m.domain && m.address == value
      t = m.__send__(:tree)
      # We need to dig into treetop
      # A valid domain must have dot_atom_text elements size > 1
      # user@localhost is excluded
      # treetop must respond to domain
      # We exclude valid email values like <user@localhost.com>
      # Hence we use m.__send__(tree).domain
      r &&= (t.domain.dot_atom_text.elements.size > 1)
    rescue Exception => e
      r = false
    end
    record.errors[attribute] << (options[:message] || "is invalid") unless r
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
wheels-0.1.30 app/validators/email_validator.rb
wheels-0.1.28 app/validators/email_validator.rb
wheels-0.1.27 app/validators/email_validator.rb
wheels-0.1.25 app/validators/email_validator.rb
wheels-0.1.24 app/validators/email_validator.rb
wheels-0.1.23 app/validators/email_validator.rb
wheels-0.1.22 app/validators/email_validator.rb
wheels-0.1.21 app/validators/email_validator.rb
wheels-0.1.20 app/validators/email_validator.rb
wheels-0.1.19 app/validators/email_validator.rb
wheels-0.1.18 app/validators/email_validator.rb
wheels-0.1.17 app/validators/email_validator.rb
wheels-0.1.16 app/validators/email_validator.rb
wheels-0.1.15 app/validators/email_validator.rb
wheels-0.1.14 app/validators/email_validator.rb
wheels-0.1.13 app/validators/email_validator.rb
wheels-0.1.12 app/validators/email_validator.rb
wheels-0.1.10 app/validators/email_validator.rb
wheels-0.1.9 app/validators/email_validator.rb
wheels-0.1.8 app/validators/email_validator.rb