Skip to content

Instantly share code, notes, and snippets.

@cbwar
Created August 30, 2022 10:45
Show Gist options
  • Save cbwar/082cea01e08ff5d0cb1bbe16184a70d9 to your computer and use it in GitHub Desktop.
Save cbwar/082cea01e08ff5d0cb1bbe16184a70d9 to your computer and use it in GitHub Desktop.
Validate url
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