{"record":{"id":"489de54e9ccf86a3","repo":"colinhacks/zod","slug":"invalid-template-literal-part-part","errorCode":null,"errorMessage":"Invalid template literal part: ${part}","messagePattern":"Invalid template literal part: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/zod/src/v4/core/schemas.ts","lineNumber":4328,"sourceCode":"    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\",\n          code: \"invalid_type\",\n        });\n        return payload;\n      }\n\n      inst._zod.pattern.lastIndex = 0;\n\n      if (!inst._zod.pattern.test(payload.value)) {","sourceCodeStart":4310,"sourceCodeEnd":4346,"githubUrl":"https://github.com/colinhacks/zod/blob/2d90846af918af9602e088812d63a035d47cdbe4/packages/zod/src/v4/core/schemas.ts#L4310-L4346","documentation":"Thrown by $ZodTemplateLiteral when a part is neither a Zod-schema object (no _zod.pattern branch) nor a primitive/null that the constructor knows how to inline. This is the catch-all for unrecognized part types — functions, class constructors, plain objects without _zod, Symbols that aren't in util.primitiveTypes, etc. The message prints the part itself.","triggerScenarios":"Calling z.template`${() => \"x\"}`, z.template`${SomeClass}`, z.template`${{ foo: 1 }}`, z.template`${Symbol(\"x\")}`, or passing any non-schema, non-primitive value as a template part. Often the result of forgetting to call a schema factory (z.string vs z.string()) or passing a schema definition object instead of an instance.","commonSituations":"Forgetting parentheses on a schema factory (passing the function reference instead of its result); passing a config object where a schema is expected; passing a class constructor; Symbol parts; copy-paste from code that used a different templating library.","solutions":["Pass only Zod schema instances or primitive values (string/number/boolean/null/undefined/bigint) as template parts.","If you wrote z.string instead of z.string(), add the parentheses.","Convert plain config objects to schemas with z.object() before embedding.","For dynamic fragments, stringify the value first and embed it as a plain string part."],"exampleFix":"// before\nconst T = z.template`id-${z.string}`; // forgot to call\n// after\nconst T = z.template`id-${z.string()}`;","handlingStrategy":"type-guard","validationCode":"function isTemplatePart(part: unknown): boolean {\n  if (part === null || part === undefined) return true;\n  const t = typeof part;\n  return t === \"string\" || t === \"number\" || t === \"boolean\" || t === \"bigint\" ||\n         (t === \"object\" && !!(part as any)?._zod?.pattern);\n}","typeGuard":"function isTemplatePart(part: unknown): boolean {\n  if (part === null || part === undefined) return true;\n  const t = typeof part;\n  return t === \"string\" || t === \"number\" || t === \"boolean\" || t === \"bigint\" ||\n         (t === \"object\" && !!(part as any)?._zod?.pattern);\n}","tryCatchPattern":null,"preventionTips":["Pass only Zod schema instances or primitives into z.template`...`.","Always invoke schema factories (z.string() not z.string).","Convert plain config objects to z.object() before embedding."],"tags":["template-literal","schema-construction","type-error"],"backgroundTag":null,"analyzedSha":"2d90846af918af9602e088812d63a035d47cdbe4","analyzedAt":"2026-08-11T01:21:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-22T16:17:23.217Z"}