class UrlValidator

Constants

SCHEMES

Public Instance Methods

validate_each(record, attribute, value) click to toggle source
# File lib/lite/validators/url_validator.rb, line 11
def validate_each(record, attribute, value)
  assign_attr_readers(record, attribute, URI.parse(value.to_s))
  valid_attr?
rescue URI::InvalidURIError
  record.errors.add(attribute, *error_message)
end

Private Instance Methods

error_message_for(option) click to toggle source
# File lib/lite/validators/url_validator.rb, line 24
def error_message_for(option)
  options[:message] || I18n.t("errors.messages.url.#{option}")
end
scheme() click to toggle source
# File lib/lite/validators/url_validator.rb, line 20
def scheme
  options[:scheme] || UrlValidator::SCHEMES
end
valid_attr?() click to toggle source
# File lib/lite/validators/url_validator.rb, line 28
def valid_attr?
  raise URI::InvalidURIError if value.to_s.strip.empty?

  valid_uri? && valid_domain? && valid_scheme? && valid_root?
end
valid_domain?() click to toggle source

rubocop:disable Layout/LineLength, Metrics/AbcSize

# File lib/lite/validators/url_validator.rb, line 35
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}") }
  record.errors.add(attribute, error_message_for(:domain)) unless check
end
valid_root?() click to toggle source

rubocop:enable Layout/LineLength, Metrics/AbcSize

# File lib/lite/validators/url_validator.rb, line 44
def valid_root?
  return true unless options[:root_only]

  check = ['', '/'].include?(value.path) && value.query.blank? && value.fragment.blank?
  record.errors.add(attribute, error_message_for(:root)) unless check
end
valid_scheme?() click to toggle source
# File lib/lite/validators/url_validator.rb, line 51
def valid_scheme?
  return true unless options[:scheme]

  value_downcased = value.scheme.to_s.downcase
  check = Array(scheme).any? { |sch| value_downcased == sch.to_s.downcase }
  record.errors.add(attribute, error_message_for(:scheme)) unless check
end
valid_uri?() click to toggle source
# File lib/lite/validators/url_validator.rb, line 59
def valid_uri?
  value.is_a?(URI::Generic)
end