owncast/web/utils/urls.ts

22 lines
545 B
TypeScript
Raw Normal View History

// to use with <input type="url"> fields, as the default pattern only checks for `:`,
export const DEFAULT_TEXTFIELD_URL_PATTERN = 'https?://.*';
2021-02-16 20:41:24 +01:00
export default function isValidUrl(url: string): boolean {
const validProtocols = ['http:', 'https:'];
try {
2022-05-12 08:31:31 +02:00
const validationObject = new URL(url);
if (
validationObject.protocol === '' ||
validationObject.hostname === '' ||
!validProtocols.includes(validationObject.protocol)
) {
return false;
}
} catch (e) {
return false;
}
2021-02-16 20:41:24 +01:00
return true;
}