{"id":"a4dcc641815932fe","repo":"colinhacks/zod","slug":"invalid-template-literal-part-part-zod-traits","errorCode":null,"errorMessage":"Invalid template literal part: ${part._zod.traits}","messagePattern":"Invalid template literal part: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/zod/src/v4/core/schemas.ts","lineNumber":4303,"sourceCode":"      : never\n    : never;\n\nexport const $ZodTemplateLiteral: core.$constructor<$ZodTemplateLiteral> = /*@__PURE__*/ core.$constructor(\n  \"$ZodTemplateLiteral\",\n  (inst, def) => {\n    $ZodType.init(inst, def);\n    const regexParts: string[] = [];\n    for (const part of def.parts) {\n      if (typeof part === \"object\" && part !== null) {\n        // is Zod schema\n        if (!part._zod.pattern) {\n          // if (!source)\n          throw new Error(`Invalid template literal part, no pattern found: ${[...(part as any)._zod.traits].shift()}`);\n        }\n\n        const source = part._zod.pattern instanceof RegExp ? part._zod.pattern.source : part._zod.pattern;\n\n        if (!source) throw new Error(`Invalid template literal part: ${part._zod.traits}`);\n\n        const start = source.startsWith(\"^\") ? 1 : 0;\n        const end = source.endsWith(\"$\") ? source.length - 1 : source.length;\n        regexParts.push(source.slice(start, end));\n      } else if (part === null || util.primitiveTypes.has(typeof part)) {\n        regexParts.push(util.escapeRegex(`${part}`));\n      } else {\n        throw new Error(`Invalid template literal part: ${part}`);\n      }\n    }\n    inst._zod.pattern = new RegExp(`^${regexParts.join(\"\")}$`);\n\n    inst._zod.parse = (payload, _ctx) => {\n      if (typeof payload.value !== \"string\") {\n        payload.issues.push({\n          input: payload.value,\n          inst,\n          expected: \"string\",","sourceCodeStart":4285,"sourceCodeEnd":4321,"githubUrl":"https://github.com/colinhacks/zod/blob/912f0f51b0ced654d0069741e7160834dca742ee/packages/zod/src/v4/core/schemas.ts#L4285-L4321","documentation":"Thrown in `$ZodTemplateLiteral` when a schema part has a `_zod.pattern` key but its resolved source string is empty/falsy. This is an edge case: the schema reported a pattern object/RegExp but it had no usable source text, so the combined regex cannot be built.","triggerScenarios":"A custom or transformed schema whose `pattern` is set to an empty string or a RegExp with empty source; a schema whose pattern getter returns undefined under certain config. Rare in normal usage — usually from custom schemas or unusual transforms.","commonSituations":"Custom Zod extensions that set `_zod.pattern` incorrectly; chaining transforms that erase the pattern; edge cases in third-party plugins.","solutions":["Inspect the offending schema's `_zod.pattern` and ensure it resolves to a non-empty RegExp source.","Replace the problematic part with a `z.string().regex(<non-empty>)` or `z.literal(...)` whose pattern is well-defined.","If using a custom schema, ensure its `pattern` property is a non-empty RegExp before passing it to a template literal."],"exampleFix":"// before\nconst part = z.string().regex(//); // empty pattern\nconst T = z.templateLiteral([z.literal('x'), part]);\n\n// after\nconst part = z.string().regex(/^[0-9]+/);\nconst T = z.templateLiteral([z.literal('x'), part]);","handlingStrategy":"validation","validationCode":"function hasNonEmptyPattern(s) {\n  const p = s?._zod?.pattern;\n  if (!p) return false;\n  const src = p instanceof RegExp ? p.source : p;\n  return typeof src === 'string' && src.length > 0;\n}","typeGuard":"function hasNonEmptyPattern(s): boolean {\n  const p = s?._zod?.pattern;\n  if (!p) return false;\n  const src = p instanceof RegExp ? p.source : p;\n  return typeof src === 'string' && src.length > 0;\n}","tryCatchPattern":"try {\n  const T = z.templateLiteral(parts);\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Invalid template literal part:')) {\n    // inspect each part's _zod.pattern source and replace empty ones\n  }\n  throw e;\n}","preventionTips":["Ensure any custom schema passed to a template literal exposes a non-empty _zod.pattern RegExp.","Avoid chaining transforms that erase the underlying string pattern.","Prefer z.string().regex(<non-empty>) for dynamic segments."],"tags":["template-literal","schema-definition","regex"],"analyzedSha":"912f0f51b0ced654d0069741e7160834dca742ee","analyzedAt":"2026-08-03T17:41:55.908Z","schemaVersion":2}