Created
August 30, 2022 10:45
-
-
Save cbwar/082cea01e08ff5d0cb1bbe16184a70d9 to your computer and use it in GitHub Desktop.
Validate url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function validateUrl(url: string, throws: boolean = false): boolean { | |
try { | |
new URL(url) | |
} catch (err) { | |
if (throws) { | |
throw new Error(`invalid url '${url}': ${err}`) | |
} | |
return false | |
} | |
if (!url.startsWith('https://') && !url.startsWith('http://')) { | |
if (throws) { | |
throw new Error(`invalid url '${url}': url must start with http or https`) | |
} | |
return false | |
} | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment