domain_validator.rb 558 B

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