domain_validator.rb 566 B

1234567891011121314151617181920212223
  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