{"record":{"id":"4b9e7476c83ee7cc","repo":"microsoft/playwright","slug":"url-parameter-should-be-string-regexp-urlpattern","errorCode":null,"errorMessage":"url parameter should be string, RegExp, URLPattern or function","messagePattern":"url parameter should be string, RegExp, URLPattern or function","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/isomorphic/urlMatch.ts","lineNumber":193,"sourceCode":"  return match1 === match2;\n}\n\nexport function urlMatches(baseURL: string | undefined, urlString: string, match: URLMatch | undefined, webSocketUrl?: boolean): boolean {\n  if (match === undefined || match === '')\n    return true;\n  if (isString(match))\n    match = new RegExp(resolveGlobToRegexPattern(baseURL, match, webSocketUrl));\n  if (isRegExp(match)) {\n    match.lastIndex = 0;\n    return match.test(urlString);\n  }\n  const url = parseURL(urlString);\n  if (!url)\n    return false;\n  if (isURLPattern(match))\n    return match.test(url.href);\n  if (typeof match !== 'function')\n    throw new Error('url parameter should be string, RegExp, URLPattern or function');\n  return match(url);\n}\n\nexport function resolveGlobToRegexPattern(baseURL: string | undefined, glob: string, webSocketUrl?: boolean): string {\n  if (webSocketUrl)\n    baseURL = toWebSocketBaseUrl(baseURL);\n  glob = resolveGlobBase(baseURL, glob);\n  return globToRegexPattern(glob);\n}\n\nfunction toWebSocketBaseUrl(baseURL: string | undefined) {\n  // Allow http(s) baseURL to match ws(s) urls. Schemes are case-insensitive,\n  // same as elsewhere in this file, so 'HTTP://...' should be rewritten too.\n  if (baseURL && /^https?:\\/\\//i.test(baseURL))\n    baseURL = baseURL.replace(/^https?/i, scheme => scheme.toLowerCase() === 'https' ? 'wss' : 'ws');\n  return baseURL;\n}\n","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/isomorphic/urlMatch.ts#L175-L211","documentation":"Thrown by urlMatches when the `match` argument is not a string, RegExp, URLPattern, or function. After all type checks (isString, isRegExp, isURLPattern, typeof function) fail, the value is of an unsupported type and cannot be used for URL matching.","triggerScenarios":"Passing an object, number, null (non-undefined), array, or any non-supported type as a URL match: `page.route(123, ...)`, `page.waitForRequest({ host: 'x' }, ...)` (plain object that is not a URLPattern), or `context.route(['a','b'], ...)`. Note `undefined`/'' short-circuit to match-all and do not throw.","commonSituations":"Passing a plain object literal expecting it to behave like URLPattern; using an array of matchers where a single matcher is required; type coercion bug feeding a number into a route matcher; URLPattern unavailable in the runtime so a value fails isURLPattern.","solutions":["Pass a glob string, RegExp, URLPattern instance, or a `(url: URL) => boolean` function.","If using an object, construct a real `new URLPattern({...})` first.","Wrap multi-match logic in a function that ORs individual checks."],"exampleFix":"// before\npage.route({ pathname: '/api' }, handler);  // plain object, not URLPattern\n\n// after\npage.route(new URLPattern({ pathname: '/api' }), handler);\n// or simply\npage.route('**/api', handler);","handlingStrategy":"type-guard","validationCode":"function assertUrlMatch(m: unknown) {\n  if (m === undefined || m === '') return;\n  if (typeof m === 'string' || m instanceof RegExp || typeof m === 'function') return;\n  if (typeof globalThis.URLPattern === 'function' && m instanceof globalThis.URLPattern) return;\n  throw new Error('url match must be string | RegExp | URLPattern | function');\n}","typeGuard":"import { isURLPattern } from 'playwright-core/lib/utils';\nfunction isUrlMatch(m: unknown): m is string | RegExp | URLPattern | ((u: URL) => boolean) {\n  return typeof m === 'string'\n    || m instanceof RegExp\n    || typeof m === 'function'\n    || isURLPattern(m);\n}","tryCatchPattern":"try {\n  page.route(matcher as any, handler);\n} catch (e) {\n  if (/url parameter should be string/.test(e.message)) {\n    throw new TypeError(`Invalid route matcher ${typeof matcher}; wrap object in new URLPattern()`);\n  }\n  throw e;\n}","preventionTips":["Type route/waitForRequest matchers as the union the library accepts.","Construct URLPattern explicitly rather than passing plain objects.","Add an ESLint rule or runtime assert on matcher types at boundaries."],"tags":["url-matching","types","validation"],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}