{"record":{"id":"f5357cfaf9470bd5","repo":"colinhacks/zod","slug":"invalid-arguments","errorCode":"invalid_arguments","errorMessage":"Invalid function arguments","messagePattern":"Invalid function arguments","errorType":"validation","errorClass":"ZodError","httpStatus":null,"severity":"error","filePath":"packages/zod/src/v3/types.ts","lineNumber":3892,"sourceCode":"        });\n        const result = await Reflect.apply(fn, this, parsedArgs as any);\n        const parsedReturns = await (me._def.returns as unknown as ZodPromise<ZodTypeAny>)._def.type\n          .parseAsync(result, params)\n          .catch((e) => {\n            error.addIssue(makeReturnsIssue(result, e));\n            throw error;\n          });\n        return parsedReturns;\n      });\n    } else {\n      // Would love a way to avoid disabling this rule, but we need\n      // an alias (using an arrow function was what caused 2651).\n      // eslint-disable-next-line @typescript-eslint/no-this-alias\n      const me = this;\n      return OK(function (this: any, ...args: any[]) {\n        const parsedArgs = me._def.args.safeParse(args, params);\n        if (!parsedArgs.success) {\n          throw new ZodError([makeArgsIssue(args, parsedArgs.error)]);\n        }\n        const result = Reflect.apply(fn, this, parsedArgs.data);\n        const parsedReturns = me._def.returns.safeParse(result, params);\n        if (!parsedReturns.success) {\n          throw new ZodError([makeReturnsIssue(result, parsedReturns.error)]);\n        }\n        return parsedReturns.data;\n      }) as any;\n    }\n  }\n\n  parameters() {\n    return this._def.args;\n  }\n\n  returnType() {\n    return this._def.returns;\n  }","sourceCodeStart":3874,"sourceCodeEnd":3910,"githubUrl":"https://github.com/colinhacks/zod/blob/2d90846af918af9602e088812d63a035d47cdbe4/packages/zod/src/v3/types.ts#L3874-L3910","documentation":"Thrown as a ZodError with code 'invalid_arguments' at packages/zod/src/v3/types.ts:3892 when a function wrapped by z.function() is invoked and its argument tuple fails the args schema validation. The wrapper parses the runtime args array through the configured ZodTuple, and on failure throws a ZodError (not a generic Error) so callers can read the structured issues.","triggerScenarios":"Defining `const fn = z.function().args(z.string(), z.number()).implement((s, n) => ...)` and then calling `fn(123, 'x')` — the first arg is not a string and the second is not a number, so the args tuple safeParse fails and the wrapper throws invalid_arguments.","commonSituations":"Calling an implemented ZodFunction from untyped caller code; widening the args schema (e.g. requiring a string) without updating call sites; passing the wrong number of arguments because the tuple/rest shape changed; interop with frameworks that pass extra/positional args.","solutions":["Inspect the thrown ZodError.issues — each issue's path points at the offending argument index.","Align the call site with the declared args schema (correct types and order).","If the args schema is too strict, relax it (e.g. add .optional(), accept a union) and re-implement.","Wrap the invocation in try/catch to translate the ZodError into a domain-specific error for upstream callers."],"exampleFix":"// before\nconst fn = z\n  .function()\n  .args(z.string(), z.number())\n  .implement((s, n) => `${s}:${n}`);\nfn(123, 'x'); // throws invalid_arguments\n\n// after\nfn('count', 42);","handlingStrategy":"try-catch","validationCode":"// Validate arguments before invoking the implemented function.\nimport { ZodFunction } from 'zod';\n\nfunction callSafely<Fn extends ZodFunction<any, any>>(fn: Fn, args: unknown[]) {\n  const argsSchema = fn.parameters();\n  const parsed = argsSchema.safeParse(args);\n  if (!parsed.success) {\n    return { ok: false as const, error: parsed.error };\n  }\n  return { ok: true as const, value: fn(...(parsed.data as unknown[])) };\n}","typeGuard":"null","tryCatchPattern":"import { ZodError } from 'zod';\n\ntry {\n  fn('count', 42);\n} catch (e) {\n  if (e instanceof ZodError && e.issues.some((i) => i.code === 'invalid_arguments')) {\n    // argument contract violation — e.issues[].path points at the bad arg index\n    reportArgErrors(e.issues);\n  } else {\n    throw e;\n  }\n}","preventionTips":["Keep the declared args schema aligned with what callers actually pass.","Use fn.parameters() to pre-validate argument arrays when integrating with untyped callers.","Treat argument-contract violations as bugs in the caller, not user input errors.","Unit-test the implemented function with representative valid and invalid argument shapes."],"tags":["z-function","arguments","validation","runtime"],"backgroundTag":null,"analyzedSha":"2d90846af918af9602e088812d63a035d47cdbe4","analyzedAt":"2026-08-11T01:21:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}