Sha256: af78f7d8a1bbe1846d1539eeb3f280288a5d47c9aab6c13e5ec920abbe4a1c74

Contents?: true

Size: 613 Bytes

Versions: 22

Compression:

Stored size: 613 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.add attribute, :url_format, **options 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

22 entries across 22 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/validators/url_validator.rb
decidim-core-0.28.4 app/validators/url_validator.rb
decidim-core-0.27.9 app/forms/url_validator.rb
decidim-core-0.29.0 app/validators/url_validator.rb
decidim-core-0.28.3 app/validators/url_validator.rb
decidim-core-0.27.8 app/forms/url_validator.rb
decidim-core-0.29.0.rc4 app/validators/url_validator.rb
decidim-core-0.29.0.rc3 app/validators/url_validator.rb
decidim-core-0.29.0.rc2 app/validators/url_validator.rb
decidim-core-0.29.0.rc1 app/validators/url_validator.rb
decidim-core-0.28.2 app/validators/url_validator.rb
decidim-core-0.27.7 app/forms/url_validator.rb
decidim-core-0.28.1 app/validators/url_validator.rb
decidim-core-0.27.6 app/forms/url_validator.rb
decidim-core-0.26.10 app/forms/url_validator.rb
decidim-core-0.26.9 app/forms/url_validator.rb
decidim-core-0.28.0 app/validators/url_validator.rb
decidim-core-0.27.5 app/forms/url_validator.rb
decidim-core-0.28.0.rc5 app/validators/url_validator.rb
decidim-core-0.28.0.rc4 app/validators/url_validator.rb