Skip to content

fix(validation): improve URL validation to handle edge cases #57

@usernane

Description

@usernane

Problem

Current isValidURL regex may not handle:

  • localhost
  • localhost:3000
  • IPv6 addresses
  • URLs with authentication (user:pass@host)
  • Data URLs

Current Implementation

var pattern = new RegExp('^(https?:\\/\\/)?' +
  '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' +
  '((\\d{1,3}\\.){3}\\d{1,3}))' +
  '(\\:\\d+)?(\\/[-a-z\\d%_.=~+!]*)*' +
  '(\\?[;&a-z\\d%_.~+=/-]*)?' +
  '(\\#[-a-z\\d_]*)?$', 'i');

Suggested Fix

Use the URL constructor for validation:

isValidURL: {
  value: function (url) {
    try {
      new URL(url);
      return true;
    } catch {
      return false;
    }
  }
}

Test Cases That Should Pass

  • http://localhost
  • http://localhost:3000/api
  • http://[::1]:8080
  • https://example.com

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpriority: mediumMedium priority issue

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions