How to use the isUri function from valid-url

Find comprehensive JavaScript valid-url.isUri code examples handpicked from public code repositorys.

valid-url.isUri is a function provided by the valid-url library that checks whether a given string is a valid URI.

26
27
28
29
30
31
32
33
34
35
    name: 'keen:ua_parser',
    input: { ua_string: 'user_agent' },
    output: 'parsed_user_agent',
  });
}
if (config.urlAddon && properties.url && validUrl.isUri(properties.url)) {
  addons.push({
    name: 'keen:url_parser',
    input: { url: 'url' },
    output: 'parsed_page_url',
fork icon77
star icon54
watch icon20

+ 65 other calls in file

3
4
5
6
7
8
9
10
11
12
13
module.exports = function(url, pathPrefix) {
  // work with undefined
  url = url || "";


  if (
    validUrl.isUri(url) ||
    url.indexOf("http://") === 0 ||
    url.indexOf("https://") === 0
  ) {
    return url;
fork icon0
star icon0
watch icon1

+ 26 other calls in file

How does valid-url.isUri work?

valid-url.isUri works by examining a given string and determining whether it is a valid Uniform Resource Identifier (URI).

In order to check whether a given string is a valid URI, valid-url.isUri performs a series of tests to ensure that the string conforms to the syntax specified by the URI specification. This includes checking that the string includes a scheme (e.g. "http", "ftp", "mailto"), that the scheme is followed by a colon and two slashes (if applicable), and that the path portion of the URI is properly encoded.

If the string passes all of the tests, then valid-url.isUri returns true. Otherwise, it returns false.

By using valid-url.isUri, JavaScript developers can programmatically check whether a given string is a valid URI, allowing them to validate user input, ensure that URLs are properly formatted, and perform other URI-related operations.

16
17
18
19
20
21
22
23
24
25
26


routes.get("/getDetails", async (req, res, next) => {
  try {
    const URL = util.shortentURL(req.query.url);
    if (
      validUrl.isUri(URL) &&
      validUrl.isWebUri(URL) &&
      validUrl.isHttpsUri(URL)
    ) {
      try {
fork icon0
star icon0
watch icon0

+ 3 other calls in file

Ai Example

1
2
3
4
5
const validUrl = require("valid-url");

// check whether a string is a valid URI using valid-url.isUri
const uri = "http://www.example.com";
console.log(`Is ${uri} a valid URI? ${validUrl.isUri(uri)}`);

In this example, we're using the valid-url library to check whether a given string (uri) is a valid URI. We call the validUrl.isUri function and pass the string as an argument. validUrl.isUri then performs a series of tests to determine whether the string is a valid URI, returning true if it is and false otherwise. In this case, the uri string is a valid URI, so validUrl.isUri returns true. We then log the result to the console using a template string. When we run this code, it will output the following message to the console: bash Copy code