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

Conversation

Harsh517-tech415
Copy link

Added validateUrl utility

*/
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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants