{"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":4432,"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":4414,"sourceCodeEnd":4450,"githubUrl":"https://github.com/colinhacks/zod/blob/912f0f51b0ced654d0069741e7160834dca742ee/packages/zod/src/v4/core/schemas.ts#L4414-L4450","documentation":"Thrown by `inst.implement(func)` on a `z.function()` schema when the argument is not a function. `.implement()` wraps a callback so its arguments and return value are validated against the function schema's input/output types; it requires an actual function to wrap.","triggerScenarios":"Calling `.implement(undefined)`, `.implement(null)`, `.implement('some string')`, or passing an object/array to a function schema's `.implement()`. Often a typo, an unset variable, or a wrong import.","commonSituations":"Calling `.implement()` before assigning the handler; passing a config object instead of a function; dynamic dispatch where the handler variable is conditionally defined.","solutions":["Pass an actual function to `.implement()`, e.g. `schema.implement((arg) => ...)`.","If the handler is optional/dynamic, guard with `if (typeof handler === 'function') schema.implement(handler)`.","Check imports/variable bindings — the value is likely undefined due to a missing or misnamed import."],"exampleFix":"// before\nconst fn = z.function(z.string(), z.boolean()).implement({ run: (s) => !!s });\n\n// after\nconst fn = z.function(z.string(), z.boolean()).implement((s) => !!s);","handlingStrategy":"type-guard","validationCode":"if (typeof handler !== 'function') throw new Error('handler must be a function');\nconst wrapped = fnSchema.implement(handler);","typeGuard":"function isFunction(v): boolean { return typeof v === 'function'; }","tryCatchPattern":"try {\n  const wrapped = fnSchema.implement(handler);\n} catch (e) {\n  if (e instanceof Error && e.message === 'implement() must be called with a function') {\n    // ensure handler is defined and is a function before retrying\n  }\n  throw e;\n}","preventionTips":["Type the handler parameter explicitly as a function so the compiler catches non-functions.","Guard dynamic handlers with typeof checks before calling .implement().","Verify imports/initialization order to avoid undefined handlers."],"tags":["function-schema","api-misuse","validation"],"analyzedSha":"912f0f51b0ced654d0069741e7160834dca742ee","analyzedAt":"2026-08-03T17:41:55.908Z","schemaVersion":2}