Class: UrlValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/url_validation.rb

Overview

Validates URLs. Uses the following I18n error message keys:

invalid_url URL is improperly formatted.
url_not_accessible Couldn't connect to the URL.
url_invalid_response Got a bad HTTP response (not of an acceptable type, e.g., 2xx).

Options

Basic options

:allow_nil If true, nil values are allowed.
:allow_blank If true, nil or empty values are allowed.

Error messages

:invalid_url_message A custom message to use in place of :invalid_url.
:incorrect_url_type_message A custom message to use in place of :incorrect_url_type.
:url_not_accessible_message A custom message to use in place of :url_not_accessible.
:url_invalid_response_message A custom message to use in place of :url_invalid_response.

Networkless URL validation

:scheme A string or array of strings, such as "http" or "ftp", indicating which URL schemes are valid. By default only HTTP(S) URLs are accepted.
:default_scheme A default URL scheme to try for improper URLs. If this is set to, e.g., "http", then when a URL like "whoops.com" is given (which would otherwise fail due to an improper format), "http://whoops.com" will be tried instead.

Over-the-network URL validation

The HTTPI gem is used to provide a generic interface to whatever HTTP client you wish to use. This allows you to drop in, e.g., a Curl client if you want. You can set the HTTPI adapter with the :httpi_adapter option.

By default, HEAD requests are used for accessibility checks. If a server does not support HEAD requests, you can set :http_method to :get (or any other verb supported by HTTPI).

:check_host If true, the validator will perform a network test to verify that it can connect to the server and access the host (at the "/" path). This check will only be performed for HTTP(S) URLs.
:check_path An integer or symbol (or array of integers or symbols), such as 301 or :moved_permanently, indicating what response codes are unacceptable. You can also use ranges, and include them in an array, such as [:moved_permanently, 400..404, 500..599]. By default, this is nil, and therefore only host accessibility is checked. If true is given, uses a default set of invalid error codes (4xx and 5xx). Implies :check_host is also true.
:httpi_adapter The HTTPI adapter to use for checking HTTP and HTTPS URLs (default set by the HTTPI gem).
:http_method The HTTP verb (as a Symbol) to use for the accessibility check. Defaults to :head. Set to :get for servers that do not support HEAD.

Other options

:request_callback A proc that receives the request object (for HTTP(S) requests, the HTTPI::Request object) before it is executed. You can use this proc to set, e.g., custom headers or timeouts on the request.

Examples:

Checks the syntax only

validates :link, url: true

Ensures the host is available but does not check the path

validates :link, url: {check_host: true}

Ensures that the host is available and that a request for the path does not return a 4xx or 5xx response

validates :link, url: {check_path: true}

Ensures that the host is available and that a request for the path does not return a 3xx, 4xx, or 5xx response

validates :link, url: {check_path: [300..399, 400..499, 500..599]}

Checks for host accessibility with a custom timeout

validates :link, url: {
  check_host: true,
  request_callback: ->(request) { request.timeout = 30 }
}

Uses a GET request instead of the default HEAD request

validates :link, url: {check_host: true, http_method: :get}

Constant Summary collapse

VERSION =
UrlValidation::VERSION