Skip to main content
Version: 5.x

isURL Enforce Rule

Description

The isURL enforce rule provides functionality to validate URL values. This documentation covers the isUrl rule, along with its options and configurations.

These rule exposes the validator.js isURL rule, and accepts the same options.

isURL Rule

The isURL rule checks whether a given value is a valid URL. It accepts various options to customize the validation behavior.

To be used, the rule first needs to be imported:

import 'vest/enforce/isURL';

enforce(value).isURL(options);

Options

The isURL rule accepts an optional options object to customize the validation behavior. The available options are as follows:

The isUrl rule accepts an optional options object to customize the validation behavior. The available options are as follows:

OptionDefault ValueDescription
require_protocolfalseRequires the URL to include a protocol (e.g., http:// or https://).
require_hosttrueRequires the URL to include a host (e.g., www.example.com).
require_valid_protocoltrueRequires the URL's protocol to be in the list of valid protocols (http, https, ftp).
allow_underscoresfalseAllows underscores in the host name.
allow_trailing_dotfalseAllows a trailing dot in the host name.
allow_protocol_relative_urlsfalseAllows protocol-relative URLs (e.g., //www.example.com).
allow_fragmentstrueAllows URL fragments (e.g., #section).
allow_query_componentstrueAllows query components in the URL (e.g., ?query=value).
validate_lengthtrueValidates that the URL length does not exceed the maximum allowed length (2083 characters).

Usage Example

// Usage with options
enforce(url).isURL({
protocols: ['http', 'https', 'ftp'],
require_tld: true,
require_protocol: false,
require_host: true,
require_port: false,
require_valid_protocol: true,
allow_underscores: false,
allow_trailing_dot: false,
allow_protocol_relative_urls: false,
allow_fragments: true,
allow_query_components: true,
validate_length: true,
});