- A utility for parsing and validating URLs
- Provides options for strict and lax validation
- Example:
const urlResults = jetta.urlParser('example.com/about/', {addMissingProtocol: true})
urlResults.isLocalhost // false
urlResults.isValid // true
urlResults.parsedURL.protocol // 'https:'- jetta.urlParser(
candidateSTRING | OBJECT[,optionsOBJECT])-
Effectively never throws, even on ill-formatted/invalid URLs as it internally catches any errors and returns
results.isValid=false -
candidateSTRING | OBJECT- The URL to parse and validate
- If Object,
hostnameattribute is required (an instance ofurl.Urlwould be handy here)
-
optionsOBJECT optional-
Defaults can be found in
jetta.defaults.urlParser -
addMissingProtocolBOOLEAN- Determines if missing protocols should be added for parsing
- Example:
jetta.urlParser('example.com', {addMissingProtocol: true}).url // returns: 'https://example.com'
-
allowWhitespaceBeforeFormattingBOOLEAN- Determines if
candidateis a string, should the parser format it - Set this to
falsefor stricter validation - Example:
jetta.urlParser('https://example.com/here there', {allowWhitespaceBeforeFormatting: false}).isValid // returns: false
- Determines if
-
ipAddressesAllowedBOOLEAN- Determines if IP Addresses are allowed (both IPv4 & IPv6) for the hostname
-
localhostAllowedBOOLEAN- Determines if 'localhost' is allowed, including localhost IP Addresses (
127.*.*.*&::1) - Useful for security purposes
- Setting this to
trueandipAddressesAllowedtofalsemeans 'https://127.0.0.1' and 'https://[::1]' are invalid while 'localhost' is fine
- Determines if 'localhost' is allowed, including localhost IP Addresses (
-
protocolsAllowedOBJECT- A key-value object of protocols allowed, where value is
true - Set to
nullto allow all protocols - Example:
const protocolsAllowed = { 'https:': true, } jetta.urlParser('https://example.com/', {protocolsAllowed}).isValid // true jetta.urlParser('http://example.com/', {protocolsAllowed}).isValid // false
- A key-value object of protocols allowed, where value is
-
protocolReplacementSTRING- The protocol to be added when the parsed URL's protocol is missing and
addMissingProtocol === true - It should be with colons, but without slashes (to match the
url.protocol's format).- i.e. 'https:' not 'https' or 'https://'
- The protocol to be added when the parsed URL's protocol is missing and
-
localhostAllowedBOOLEAN- Determines if localhost URLs are considered valid
- Example:
jetta.urlParser('http://127.0.0.1', {localhostAllowed: true}).isValid // true jetta.urlParser('http://localhost', {localhostAllowed: false}).isValid // false jetta.urlParser('::1', {localhostAllowed: true}).isValid // true
-
-
return
resultsOBJECTisLocalhostBOOLEANisValidBOOLEANoptionsOBJECT-
The options used to parse, process, and validate
candidate -
This is never
null -
addMissingProtocolBOOLEAN -
allowWhitespaceBeforeFormattingBOOLEAN -
ipAddressesAllowedBOOLEAN -
localhostAllowedBOOLEAN -
protocolsAllowedOBJECT -
protocolReplacementSTRING
-
parsedURLOBJECT instanceofurl.Url|null- The parsed URL, an instance of node's url.Url
- If
candidatewas in
- If
- The parsed URL, an instance of node's url.Url
urlSTRING- The formatted URL
-