isURL
Checks whether a value is a valid absolute URL string. A value is considered a URL when it is a string, contains a scheme followed by ://, and can be parsed by the WHATWG URL constructor without throwing.
This helper rejects malformed inputs that the parser would otherwise normalize (for example, "http:/broken.com"), but allows harmless normalization such as trailing slashes or percent‑encoding.
It never throws and never mutates input. Use it when you need to confirm that a value already represents a valid absolute URL before further processing or validation.
Signature
function isURL(value: unknown): value is string;
Parameters
| Name | Data type | Description |
|---|---|---|
| value | unknown |
The value to check as a URL. |
Returns
true: If the value is a valid absolute URL string, otherwisefalse.
Behavior
- Requires a scheme followed by
://(e.g.,https://,ftp://,custom-scheme://). - Rejects malformed inputs that the URL parser would normally "fix": (for example,
http:/broken.com). - Accepts valid absolute URLs even if the parser normalizes them (for example, adds a trailing slash).
- Accepts any valid scheme. Does not restrict to
HTTPorHTTPS. - Never throws or mutates input.
Examples
isURL("https://example.com")// true
isURL("http://localhost") // true
isURL("custom-scheme://resource") // true
isURL("http:/broken.com") // false
isURL("not a url") // false
isURL("example .com") // false
Notes
- This helper does not coerce values. Non-strings always return
false. - The minimal structural requirement (
<scheme>://) prevents acceptance of malformed inputs that the WHATWG parser would otherwise normalize. - Harmless normalization (trailing slashes, percent-encoding, canonical casing) does not invalidate the input.
- Suitable for validating configuration values, environment variables, request parameters, and any context requiring a guaranteed absolute URL string.
- The helper never throws and never mutates input.