Class: ActiveModel::Validations::EmailValidator
- Inherits:
-
EachValidator
- Object
- EachValidator
- ActiveModel::Validations::EmailValidator
- Defined in:
- lib/active_validators/active_model/validations/email_validator.rb
Instance Method Summary collapse
- #basic_check(mail) ⇒ Object
- #check_validity! ⇒ Object
- #validate_each(record, attribute, value) ⇒ Object
Instance Method Details
#basic_check(mail) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/active_validators/active_model/validations/email_validator.rb', line 27 def basic_check(mail) # We must check that value contains a domain and that value is an email address valid = !!mail.domain if [: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 <[email protected]> # 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 valid end |
#check_validity! ⇒ Object
5 6 7 |
# File 'lib/active_validators/active_model/validations/email_validator.rb', line 5 def check_validity! raise ArgumentError, "Not a callable object #{[:with].inspect}" unless [:with] == nil || [:with].respond_to?(:call) end |
#validate_each(record, attribute, value) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/active_validators/active_model/validations/email_validator.rb', line 9 def validate_each(record, attribute, value) valid = begin mail = Mail::Address.new(value) basic_check(mail) && value.include?(mail.address) rescue Exception => _ false end if [:with] # technically the test suite will pass without the boolean coercion # but we know the code is safer with it in place valid &&= !![:with].call(mail) end record.errors.add attribute, (.fetch(:message, :invalid)) unless valid end |