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.

23 lines
566 B

  1. # frozen_string_literal: true
  2. class DomainValidator < ActiveModel::EachValidator
  3. def validate_each(record, attribute, value)
  4. return if value.blank?
  5. domain = if options[:acct]
  6. value.split('@').last
  7. else
  8. value
  9. end
  10. record.errors.add(attribute, I18n.t('domain_validator.invalid_domain')) unless compliant?(domain)
  11. end
  12. private
  13. def compliant?(value)
  14. Addressable::URI.new.tap { |uri| uri.host = value }
  15. rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
  16. false
  17. end
  18. end