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.

25 lines
558 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 = 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