email_validator.rb 493 B

123456789101112131415161718
  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