domain_validator.rb 418 B

1234567891011121314151617
  1. # frozen_string_literal: true
  2. class DomainValidator < ActiveModel::EachValidator
  3. def validate_each(record, attribute, value)
  4. return if value.blank?
  5. record.errors.add(attribute, I18n.t('domain_validator.invalid_domain')) unless compliant?(value)
  6. end
  7. private
  8. def compliant?(value)
  9. Addressable::URI.new.tap { |uri| uri.host = value }
  10. rescue Addressable::URI::InvalidURIError
  11. false
  12. end
  13. end