Sha256: 55721ed09be45c699b9c4550c186275d651bf502988c7555aae6ffd0e16fdb53

Contents?: true

Size: 634 Bytes

Versions: 16

Compression:

Stored size: 634 Bytes

Contents

# frozen_string_literal: true

# A custom validator to check that the field value is a URL.
#
#   validates :my_url, url: true
#
class UrlValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    record.errors[attribute] << (options[:message] || "must be a valid URL") unless url_valid?(value)
  end

  # a URL may be technically well-formed but may
  # not actually be valid, so this checks for both.
  def url_valid?(url)
    return true if url.blank?

    url = URI.parse(url)
    (url.is_a?(URI::HTTP) || url.is_a?(URI::HTTPS)) && url.host.present?
  rescue URI::InvalidURIError
    false
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
decidim-core-0.26.7 app/forms/url_validator.rb
decidim-core-0.26.5 app/forms/url_validator.rb
decidim-core-0.26.4 app/forms/url_validator.rb
decidim-core-0.26.3 app/forms/url_validator.rb
decidim-core-0.26.2 app/forms/url_validator.rb
decidim-core-0.26.1 app/forms/url_validator.rb
decidim-core-0.26.0 app/forms/url_validator.rb
decidim-core-0.26.0.rc2 app/forms/url_validator.rb
decidim-core-0.26.0.rc1 app/forms/url_validator.rb
decidim-core-0.25.2 app/forms/url_validator.rb
decidim-core-0.25.1 app/forms/url_validator.rb
decidim-core-0.25.0 app/forms/url_validator.rb
decidim-core-0.25.0.rc4 app/forms/url_validator.rb
decidim-core-0.25.0.rc3 app/forms/url_validator.rb
decidim-core-0.25.0.rc2 app/forms/url_validator.rb
decidim-core-0.25.0.rc1 app/forms/url_validator.rb