Skip to content

Conversation

@Harsh517-tech415
Copy link

Added validateUrl utility

*/
export function validateUrl(value?: string | number | null): boolean {
try {
if (!value || typeof value !== "string") {
Copy link
Contributor

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
Contributor

@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