Skip to content

Enhancment/url validator harsh #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- added `validateUrl` utility function

## [1.1.0] - 2024-10-17

- export `boolean` utilities
Expand Down
19 changes: 19 additions & 0 deletions src/lib/url.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { validateUrl } from "./url";

describe("url tests", () => {
test.each([
["google.com", true],
["https://www.example.com", true],
["http://localhost:3000", true],
["invalid_url", false],
["javascript:alert('XSS')", false],
["ftp://example.com", false],
["mailto:[email protected]", false],
["", false],
[123, false],
[null, false],
[undefined, false],
])('validateUrl("%s")', (value, expected) => {
expect(validateUrl(value)).toBe(expected);
});
});
21 changes: 21 additions & 0 deletions src/lib/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Validate the url
* @param value The Url String
* @returns The parsed boolean
*/
export function validateUrl(value?: string | number | null): boolean {
try {
if (!value || typeof value !== "string") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check can be replaced with the isNullOrEmpty or isNullOrWhitespace utility method.

return false;
}

if (!value.startsWith("http://") && !value.startsWith("https://")) {
Copy link
Collaborator

@drebrez drebrez Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Limit to only consider "http/https" protocols might not be good for a validateUrl method, maybe renaming the method to isValidHttpUrl (this naming also matches the other validation methods like isObject, isValidGuid,...) might be better.

And at this point it might be good to have both, validateUrl and validateHttpUrl, and then one can reuse the other and do additional checks, something like:

export function isValidUrl(value?: string): boolean {
  try {
    new URL(value);
    return true;
  } catch {
    return false;
  }
}

export function isValidHttpUrl(value?: string): boolean {
  if (!isValidUrl(value)) {
    return false;
  }

  return value.startsWith("http://") || value.startsWith("https://");
}

what you think?

value = "http://".concat(value);
}

const urlPattern = /^(http|https):\/\/[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/i;
return urlPattern.test(value);
} catch (err) {
return false;
}
}