isValidUrl
Validates whether a value is a well-formed URL string. Delegates parsing to the WHATWG URL API — the same parser used by browsers and Node — so any scheme accepted by new URL(input) is considered valid (including http, https, ftp, file, and custom schemes).
Try it
Section titled “Try it”Import
Section titled “Import”import { isValidUrl } from "1o1-utils";import { isValidUrl } from "1o1-utils/is-valid-url";Signature
Section titled “Signature”function isValidUrl({ url, protocols }: IsValidUrlParams): booleanParameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
url | unknown | Yes | The value to validate |
protocols | string | readonly string[] | No | Allowlist of accepted protocols (e.g., "https" or ["http", "https"]). Matched case-insensitively; trailing : is optional, so "https" and "https:" are equivalent |
Returns
Section titled “Returns”boolean — true if the value is a parseable URL whose protocol is accepted, false otherwise.
Examples
Section titled “Examples”isValidUrl({ url: "https://example.com" }); // => trueisValidUrl({ url: "http://localhost:3000" }); // => trueisValidUrl({ url: "ftp://files.example.com" }); // => trueisValidUrl({ url: "mailto:user@example.com" }); // => true
isValidUrl({ url: "not-a-url" }); // => falseisValidUrl({ url: "" }); // => falseisValidUrl({ url: "//example.com" }); // => false — no schemeisValidUrl({ url: null }); // => false// Restrict to web protocolsisValidUrl({ url: "https://example.com", protocols: ["http", "https"] }); // => trueisValidUrl({ url: "ftp://files.example.com", protocols: ["http", "https"] }); // => false
// Accept a single protocol as a stringisValidUrl({ url: "https://example.com", protocols: "https" }); // => trueisValidUrl({ url: "http://example.com", protocols: "https" }); // => false// Validate user input before calling fetchfunction loadFeed(input: string) { if (!isValidUrl({ url: input, protocols: ["http", "https"] })) { throw new Error("Invalid URL"); } return fetch(input);}Edge Cases
Section titled “Edge Cases”- Non-string inputs (
null,undefined, numbers, objects, evenURLinstances) returnfalse. - Empty strings return
falsewithout invoking the parser. - A scheme is required —
"//example.com"and"example.com"returnfalse. - Uses
URL.canParsewhenprotocolsis omitted and available; falls back totry/catcharoundnew URL(input)otherwise. - Custom schemes (e.g.,
app://,chrome-extension://) are accepted by default. Passprotocolsto restrict. protocols: []rejects every input — no scheme can match an empty allowlist.- Protocol names are lowercased and any trailing
:is stripped, so["HTTPS"],["https"], and["https:"](orurl.protocolfrom aURLinstance) all matchhttps://....
Also known as
Section titled “Also known as”url, validate url, is url, valid url, url check, parse url
Prompt suggestion
Section titled “Prompt suggestion”I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use isValidUrl to validate a user-supplied URL before passing it to fetch.