1
0

url_validator.rb 421 B

12345678910111213141516
  1. # frozen_string_literal: true
  2. class URLValidator < ActiveModel::EachValidator
  3. def validate_each(record, attribute, value)
  4. record.errors.add(attribute, :invalid) unless compliant?(value)
  5. end
  6. private
  7. def compliant?(url)
  8. parsed_url = Addressable::URI.parse(url)
  9. parsed_url && %w(http https).include?(parsed_url.scheme) && parsed_url.host
  10. rescue Addressable::URI::InvalidURIError
  11. false
  12. end
  13. end