NPM valid-url Package


valid-url

Usage

const isValidUrl = require('valid-url').isUri;

const result = isValidUrl('https://example.com');

// Result:
// true

Implementation Guide

The valid-url package provides a simple and reliable way to validate URLs in JavaScript. It supports a wide range of URL schemes, including http, https, ftp, ftps, and mailto. You can also pass in custom schemes if needed.

To use the package, simply install it from npm:

npm install valid-url

Once installed, you can use the isUri function to validate a URL:

const isValid = isUri(url, options);

The isUri function takes two parameters:

  • url: The URL to validate.
  • options: An optional object of configuration options.

The following configuration options are available:

  • allowAbsolute: Allow absolute URLs (default: true).
  • allowRelative: Allow relative URLs (default: false).
  • allowDomain: Allow domains without a TLD (default: false).
  • allowUnicode: Allow Unicode in URLs (default: false).
  • allowLocal: Allow local URLs (default: false).
  • schemes: An array of custom schemes to allow (default: []).

Best Practices

When validating URLs, it is important to consider the following best practices:

  • Always check if a URL is valid before using it.
  • Use the most strict validation options possible.
  • Be aware of the limitations of URL validation.

Limitations

The valid-url package is not able to validate all possible URLs. For example, it cannot validate URLs that contain non-ASCII characters or URLs that point to a file or resource that does not exist.

Changelog

See the CHANGELOG for a history of changes.

Contributing

Contributions are welcome! Please read the contributing guidelines before submitting a pull request.

License

The valid-url package is licensed under the MIT License. See the LICENSE file for details.