refactor(url validation): replace regex-based URL validation by the JavaScript URL constructor (#1650)
Replaced regex-based URL validation with the native JavaScript URL constructor to improve reliability. This approach should be more robust and should help prevent bugs like the one we previously encountered with malformed regex. fix #1539
This commit is contained in:
15
src/utils/urlValidationHelper.ts
Normal file
15
src/utils/urlValidationHelper.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export function isValidURL(value: unknown) {
|
||||
try {
|
||||
let url: URL;
|
||||
if (typeof value === 'string') {
|
||||
url = new URL(value);
|
||||
} else if (value instanceof URL) {
|
||||
url = value;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return url.protocol === 'http:' || url.protocol === 'https:';
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user