-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d2603b
commit 186186d
Showing
9 changed files
with
104 additions
and
20 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./validateUrl"; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { validateUrl } from "./validateUrl"; | ||
|
||
describe("validateUrl", () => { | ||
it.each(["mailto", "http", "umami", "file"])( | ||
"returns 'Invalid URL' for %s protocol", | ||
protocol => { | ||
expect(validateUrl(`${protocol}://example.com`)).toBe("Invalid URL"); | ||
} | ||
); | ||
|
||
describe("empty URL", () => { | ||
it("returns 'Invalid URL'", () => { | ||
expect(validateUrl("")).toBe("Invalid URL"); | ||
expect(validateUrl(undefined)).toBe("Invalid URL"); | ||
}); | ||
|
||
it("returns true if allowEmpty option is set", () => { | ||
expect(validateUrl("", { allowEmpty: true })).toBe(true); | ||
expect(validateUrl(undefined, { allowEmpty: true })).toBe(true); | ||
}); | ||
}); | ||
|
||
it('returns "Invalid URL" for invalid URL', () => { | ||
expect(validateUrl("invalid url")).toBe("Invalid URL"); | ||
expect(validateUrl("http://invalid url")).toBe("Invalid URL"); | ||
expect(validateUrl("https://invalid url")).toBe("Invalid URL"); | ||
expect(validateUrl("https://")).toBe("Invalid URL"); | ||
expect(validateUrl("https:/example.com")).toBe("Invalid URL"); | ||
expect(validateUrl("https:/example")).toBe("Invalid URL"); | ||
}); | ||
|
||
it("returns true for a valid url", () => { | ||
expect(validateUrl("https://example.com")).toBe(true); | ||
expect(validateUrl("https://example.com:8080")).toBe(true); | ||
expect(validateUrl("https://example.com/path")).toBe(true); | ||
expect(validateUrl("https://example.com/path?query=1")).toBe(true); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const URL_REGEX = new RegExp( | ||
"^(https:\\/\\/)?" + // protocol | ||
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // domain name | ||
"((\\d{1,3}\\.){3}\\d{1,3}))" + // OR IP (v4) address | ||
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // port and path | ||
"(\\?[;&a-z\\d%_.~+=-]*)?" + // query string | ||
"(\\#[-a-z\\d_]*)?$", // fragment locator | ||
"i" | ||
); | ||
|
||
const ERROR_MESSAGE = "Invalid URL"; | ||
|
||
export const validateUrl = (url: string | undefined, { allowEmpty } = { allowEmpty: false }) => { | ||
try { | ||
if (!url) { | ||
return allowEmpty || ERROR_MESSAGE; | ||
} | ||
new URL(url); | ||
return URL_REGEX.test(url) || ERROR_MESSAGE; | ||
} catch { | ||
return ERROR_MESSAGE; | ||
} | ||
}; |