You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

18 lines
493 B

  1. # frozen_string_literal: true
  2. class EmailValidator < ActiveModel::EachValidator
  3. def validate_each(record, attribute, value)
  4. return if Rails.configuration.x.email_domains_blacklist.empty?
  5. record.errors.add(attribute, I18n.t('users.invalid_email')) if blocked_email?(value)
  6. end
  7. private
  8. def blocked_email?(value)
  9. domains = Rails.configuration.x.email_domains_blacklist.gsub('.', '\.')
  10. regexp = Regexp.new("@(.+\\.)?(#{domains})", true)
  11. value =~ regexp
  12. end
  13. end