Sha256: c200224600259c7f17382998b2866b03f15e2522a9cd2c5f3cb2e1288db9b153

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

require "uri" unless defined?(URI)

class UrlValidator < BaseValidator

  SCHEMES = %w[
    http https
  ].freeze

  def validate_each(record, attribute, value)
    assign_attr_readers(record, attribute, URI.parse(value.to_s))
    valid_attr?
  rescue URI::BadURIError, URI::InvalidURIError
    record.errors.add(attribute, *error_message)
  end

  private

  def error_message_for(option)
    options[:message] || I18n.t("errors.messages.url.#{option}")
  end

  def valid_attr?
    raise URI::InvalidURIError if value.to_s.strip.empty?

    valid_uri? && valid_host? && valid_domain? && valid_scheme? && valid_root?
  end

  def valid_domain?
    return true unless options[:domain]

    value_downcased = value.host.to_s.downcase
    check = Array(options[:domain]).any? { |domain| value_downcased.end_with?(".#{domain.downcase}") }
    return true if check

    record.errors.add(attribute, error_message_for(:domain))
  end

  def valid_host?
    hosts = options[:include_host] || options[:exclude_host]
    return true unless hosts

    value_downcased = value.host.to_s.downcase
    check = options[:include_host] ? :any? : :none?
    check = Array(hosts).public_send(check) { |host| value_downcased.include?(host.to_s.downcase) }
    return true if check

    record.errors.add(attribute, error_message_for(:host))
  end

  def valid_root?
    return true unless options[:root_only]

    check = ["", "/"].include?(value.path) && value.query.blank? && value.fragment.blank?
    return true if check

    record.errors.add(attribute, error_message_for(:root))
  end

  def valid_scheme?
    return true unless options[:scheme]

    value_downcased = value.scheme.to_s.downcase
    schemes = options[:scheme] || SCHEMES
    check = Array(schemes).any? { |scheme| value_downcased == scheme.to_s.downcase }
    return true if check

    record.errors.add(attribute, error_message_for(:scheme))
  end

  def valid_uri?
    value.is_a?(URI::Generic)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lite-validators-1.8.0 lib/lite/validators/url_validator.rb