Class: EmailAddressValidation

Inherits:
Validation show all
Defined in:
lib/yodel/models/core/validations/email_address_validation.rb

Instance Attribute Summary

Attributes inherited from Validation

#field, #params

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Validation

#initialize, #to_json

Constructor Details

This class inherits a constructor from Validation

Class Method Details

.validate(params, field, name, value, record, errors) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/yodel/models/core/validations/email_address_validation.rb', line 2

def self.validate(params, field, name, value, record, errors)
  begin
    # modified: http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/
    address = Mail::Address.new(value)
  
    # ensure there is a domain and the full parsed address is equivalent to the value
    if address.domain.present? && address.address == value
      # ensure the domain component is made up of more than just a TLD
      domain_tree = address.send(:tree).domain
      if domain_tree.dot_atom_text.elements.size > 1
        return true
      end
    end
  rescue
  end
  
  errors[field.name] << new(name)
end

Instance Method Details

#describeObject



21
22
23
# File 'lib/yodel/models/core/validations/email_address_validation.rb', line 21

def describe
  "must be a valid email address"
end