Skip to content

validateURL

Validates that a value is a valid absolute URL string.

This helper ensures that a string value represents a fully-qualified URL with a scheme and host. It never mutates input and never throws.

Internally, it uses the isURL type guard for runtime-safe validation.

Signature

function validateURL(value: unknown): string | null

Parameters

Name Data type Description
value unknown The value to validate.

Returns

  • The original string if it is a valid absolute URL.
  • null: If invalid.

Behavior

  • Accepts strings only.
  • Requires a scheme followed by ://.
  • Rejects malformed URLs that URL would otherwise normalize incorrectly.
  • Never throws and never mutates input.

Examples

validateURL("http://example.com")            // "http://example.com"
validateURL("https://example.com/path")     // "https://example.com/path"
validateURL("ftp://ftp.example.com")        // "ftp://ftp.example.com"
validateURL("http:/broken.com")             // null
validateURL("justastring")                  // null
validateURL(123)                             // null

Notes

  • Use when you need a predictable absolute URL before further processing or network requests
  • This helper does not accept relative URLs, missing schemes, or malformed strings