Sha256: c9e13602afa1a6da7d9d4b24abd21a822e9d89970e84f530252c7f3c1da88053

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

require 'mail'
module ActiveModel
  module Validations
    class EmailValidator < EachValidator
      def validate_each(record, attribute, value)
        # takes from: https://github.com/hallelujah/valid_email
        begin
          mail = Mail::Address.new(value)
          # We must check that value contains a domain and that value is an email address
          valid = mail.domain && value.include?(mail.address)

          if options[:only_address]
            # 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
            tree = mail.__send__(:tree)
            valid &&= (tree.domain.dot_atom_text.elements.size > 1)
          else
            valid &&= (mail.domain.split('.').length > 1)
          end
        rescue Exception => e
          valid = false
        end
        record.errors.add attribute, (options[:message]) unless valid
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activevalidators-2.0.1 lib/active_model/validations/email_validator.rb
activevalidators-2.0.0 lib/active_model/validations/email_validator.rb