{"id":"0d764a64f8943f7d","repo":"sindresorhus/is","slug":"expected-value-which-is-string-with-a-url-recei","errorCode":null,"errorMessage":"Expected value which is `string with a URL`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `string with a URL`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1969,"sourceCode":"\t}\n}\n\nexport function assertUrlInstance(value: unknown, message?: string): asserts value is URL {\n\tif (!isUrlInstance(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('URL', value));\n\t}\n}\n\n// eslint-disable-next-line unicorn/prevent-abbreviations\nexport function assertUrlSearchParams(value: unknown, message?: string): asserts value is URLSearchParams {\n\tif (!isUrlSearchParams(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('URLSearchParams', value));\n\t}\n}\n\nexport function assertUrlString(value: unknown, message?: string): asserts value is UrlString {\n\tif (!isUrlString(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('string with a URL', value));\n\t}\n}\n\nexport function assertValidDate(value: unknown, message?: string): asserts value is Date {\n\tif (!isValidDate(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('valid Date', value));\n\t}\n}\n\nexport function assertValidLength(value: unknown, message?: string): asserts value is number {\n\tif (!isValidLength(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('valid length', value));\n\t}\n}\n\n// eslint-disable-next-line @typescript-eslint/no-restricted-types\nexport function assertWeakMap<Key extends object = object, Value = unknown>(value: unknown, message?: string): asserts value is WeakMap<Key, Value> {\n\tif (!isWeakMap(value)) {","sourceCodeStart":1951,"sourceCodeEnd":1987,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1951-L1987","documentation":"Thrown by `assertUrlString()` in @sindresorhus/is when the value is not a string that parses with `new URL(value)`. The underlying `isUrlString` returns false both for non-strings and for strings the WHATWG URL parser rejects — notably strings without a scheme, since `new URL()` requires an absolute URL.","triggerScenarios":"`assert.urlString(value)` with a relative or scheme-less string ('example.com', '/api/users'), an empty string, undefined, or a URL object (objects fail — only strings pass).","commonSituations":"Missing env var yielding undefined; users entering 'example.com' without 'https://'; passing path-only routes where an absolute URL is required; passing a URL instance where the string form was expected.","solutions":["Ensure the string is an absolute URL with a scheme — prepend 'https://' or resolve against a base: `new URL(path, base).href`.","Check the value isn't undefined/empty — validate config/env vars at startup.","If you have a URL object, pass `url.href`, or use `assert.urlInstance` instead."],"exampleFix":"// before\nassert.urlString('example.com/api');\n// after\nassert.urlString('https://example.com/api');","handlingStrategy":"validation","validationCode":"function isUrlString(value) {\n  if (typeof value !== 'string') return false;\n  try { new URL(value); return true; } catch { return false; }\n}","typeGuard":"if (is.urlString(value)) {\n  // value is a string containing a valid absolute URL\n}","tryCatchPattern":"try {\n  assert.urlString(value);\n} catch (error) {\n  if (error instanceof TypeError) { /* not a string or not parseable as a URL */ }\n  throw error;\n}","preventionTips":["The check requires a parseable ABSOLUTE URL — relative paths like '/foo' fail; supply a base or full URL","A URL instance fails this check (it wants a string); use is.urlInstance for objects","Trim and validate user-supplied URLs with new URL() before passing them into APIs that assert urlString"],"tags":["typescript","type-assertion","url","validation"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}