{"record":{"id":"fd19a44cf3783f59","repo":"colinhacks/zod","slug":"implement-must-be-called-with-a-function","errorCode":null,"errorMessage":"implement() must be called with a function","messagePattern":"implement\\(\\) must be called with a function","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/zod/src/v4/core/schemas.ts","lineNumber":4449,"sourceCode":"\n  output<NewReturns extends $ZodType>(output: NewReturns): $ZodFunction<Args, NewReturns>;\n}\n\nexport interface $ZodFunctionParams<I extends $ZodFunctionIn, O extends $ZodType> {\n  input?: I;\n  output?: O;\n}\n\nexport const $ZodFunction: core.$constructor<$ZodFunction> = /*@__PURE__*/ core.$constructor(\n  \"$ZodFunction\",\n  (inst, def) => {\n    $ZodType.init(inst, def);\n    inst._def = def;\n    inst._zod.def = def;\n\n    inst.implement = (func) => {\n      if (typeof func !== \"function\") {\n        throw new Error(\"implement() must be called with a function\");\n      }\n      return function (this: any, ...args: never[]) {\n        const parsedArgs = inst._def.input ? parse(inst._def.input, args) : args;\n        const result = Reflect.apply(func, this, parsedArgs as never[]);\n        if (inst._def.output) {\n          return parse(inst._def.output, result);\n        }\n        return result as any;\n      };\n    };\n\n    inst.implementAsync = (func) => {\n      if (typeof func !== \"function\") {\n        throw new Error(\"implementAsync() must be called with a function\");\n      }\n      return async function (this: any, ...args: never[]) {\n        const parsedArgs = inst._def.input ? await parseAsync(inst._def.input, args) : args;\n        const result = await Reflect.apply(func, this, parsedArgs as never[]);","sourceCodeStart":4431,"sourceCodeEnd":4467,"githubUrl":"https://github.com/colinhacks/zod/blob/2d90846af918af9602e088812d63a035d47cdbe4/packages/zod/src/v4/core/schemas.ts#L4431-L4467","documentation":"Thrown by inst.implement() on a $ZodFunction when the argument is not of type \"function\". implement() wraps a user function so that its arguments and return value are parsed against the function schema's input/output schemas; passing anything other than a callable function (a number, object, undefined, null, string) is rejected immediately at the wrap site. The check is `typeof func !== \"function\"`.","triggerScenarios":"Calling z.function(...).implement(null), .implement({}), .implement(42), .implement(undefined), or .implement(\"foo\"). Also triggered by passing a value that was supposed to be a function but was overwritten or never assigned (undefined from a failed import).","commonSituations":"Forgetting to pass the handler; passing a class instance or bound object instead of the function; conditional logic that yields a non-function in a branch; refactoring that renames the function but leaves the old reference (now undefined); misuse of the API by treating implement() like a setter.","solutions":["Pass a callable function to implement().","If the handler is optional, guard with a conditional before calling implement().","Verify the imported symbol is actually a function (check for circular imports returning undefined).","Add a unit test asserting typeof handler === \"function\" before wiring."],"exampleFix":"// before\nconst fn = z.function(z.string(), z.string()).implement(undefined);\n// after\nconst fn = z.function(z.string(), z.string()).implement((s) => s.toUpperCase());","handlingStrategy":"type-guard","validationCode":"function implementOrThrow<F>(fnSchema: z.ZodFunction, fn: F) {\n  if (typeof fn !== \"function\") throw new Error(\"implement() requires a function\");\n  return fnSchema.implement(fn as any);\n}","typeGuard":"function isFunction(v: unknown): v is Function {\n  return typeof v === \"function\";\n}","tryCatchPattern":null,"preventionTips":["Verify the handler reference is defined and callable before passing to implement().","Watch for circular imports that yield undefined.","Type the handler parameter explicitly so tsc flags non-functions."],"tags":["function","implement","schema-construction","type-guard"],"backgroundTag":null,"analyzedSha":"2d90846af918af9602e088812d63a035d47cdbe4","analyzedAt":"2026-08-11T01:21:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-22T11:17:16.035Z"}