validate.io-uri

Validates if a value is a URI.

Usage no npm install needed!

<script type="module">
  import validateIoUri from 'https://cdn.skypack.dev/validate.io-uri';
</script>

README

URI

NPM version Build Status Coverage Status Dependencies

Validates if a value is a URI.

Installation

$ npm install validate.io-uri

For use in the browser, use browserify.

Usage

var isURI = require( 'validate.io-uri' );

isURI( value )

Validates if a value is a URI.

var value = 'http://google.com';

var bool = isURI( value );
// returns true

Note: for non-string input values, the function returns false.

Examples

var isURI = require( 'validate.io-uri' );

// VALID //

var bool = isURI( 'http://google.com' );
console.log( bool );
// returns true

bool = isURI( 'http://localhost/' );
console.log( bool );
// returns true

bool = isURI( 'http://example.w3.org/path%20with%20spaces.html' );
console.log( bool );
// returns true

bool = isURI( 'http://example.w3.org/%20' );
console.log( bool );
// returns true

bool = isURI( 'ftp://ftp.is.co.za/rfc/rfc1808.txt' );
console.log( bool );
// returns true

bool = isURI( 'ftp://ftp.is.co.za/../../../rfc/rfc1808.txt' );
console.log( bool );
// returns true

bool = isURI( 'http://www.ietf.org/rfc/rfc2396.txt' );
console.log( bool );
// returns true

bool = isURI( 'ldap://[2001:db8::7]/c=GB?objectClass?one' );
console.log( bool );
// returns true

bool = isURI( 'mailto:John.Doe@example.com' );
console.log( bool );
// returns true

bool = isURI( 'news:comp.infosystems.www.servers.unix' );
console.log( bool );
// returns true

bool = isURI( 'tel:+1-816-555-1212' );
console.log( bool );
// returns true

bool = isURI( 'telnet://192.0.2.16:80/' );
console.log( bool );
// returns true

bool = isURI( 'urn:oasis:names:specification:docbook:dtd:xml:4.1.2' );
console.log( bool );
// returns true


// INVALID //

// No scheme:
bool = isURI( '' );
console.log( bool );
// returns false

// No scheme:
bool = isURI( 'foo' );
console.log( bool );
// returns false

// No scheme:
bool = isURI( 'foo@bar' );
console.log( bool );
// returns false

// No scheme:
bool = isURI( '://foo/' );
console.log( bool );
// returns false

// Illegal characters:
bool = isURI( 'http://<foo>' );
console.log( bool );
// returns false

// Invalid path:
bool = isURI( 'http:////foo.html' );
console.log( bool );
// returns false

// Incomplete hex escapes...
bool = isURI( 'http://example.w3.org/%a' );
console.log( bool );
// returns false

bool = isURI( 'http://example.w3.org/%a/foo' );
console.log( bool );
// returns false

bool = isURI( 'http://example.w3.org/%at' );
console.log( bool );
// returns false

To run the example code from the top-level application directory,

$ node ./examples/index.js

Notes

  • See RFC 3986 and Wikipedia for information regarding the URI scheme.
  • This module uses the same test URIs as valid-url (a clone of a corresponding Perl package), which are based on examples from RFC 3986.

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

License

MIT license.

Copyright

Copyright © 2015. Athan Reines.