Files
copilotkit__copilotkit/packages/react-ui/oxlint-rules/no-single-arg-zod-record.mjs
Mark Fogle 70f54a8403 fix: use two-argument z.record for Zod 4 compatibility, add lint guard
Zod 4 made the key schema mandatory for z.record, so the single-argument
z.record(valueType) form is a compile-time error (TS2554) when built against
Zod 4. @copilotkit/react-core declares zod ">=3.0.0", so downstream apps on
Zod 4 are affected; runtime parsing is unaffected under both majors.

- react-core + vue MCPAppsActivityContentSchema: toolInput now uses the
  two-argument z.record(z.string(), z.unknown()) form
- react-core defineToolCallRenderer test: same fix for a metadata schema
- add a toolInput field-contract test (round-trips mixed value types)
- add copilotkit/no-single-arg-zod-record oxlint rule (autofix), enabled as
  error for packages/**; the incompatibility is type-level, so no runtime
  test can guard it while the workspace lockfile pins Zod 3

Closes #4295

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 21:33:55 +00:00

74 lines
2.3 KiB
JavaScript

/**
* Oxlint rule: no-single-arg-zod-record
*
* Bans the single-argument `z.record(valueType)` form in favor of the
* two-argument `z.record(z.string(), valueType)` form.
*
* Zod 4 made the key schema mandatory for z.record, so the single-argument
* `z.record(valueType)` form is a compile-time error (TS2554) when built
* against Zod 4 — even though it still parses fine at runtime under both Zod
* majors. Packages that allow a Zod 4 peer (e.g. `@copilotkit/react-core`
* declares `zod: ">=3.0.0"`) are affected. The two-argument
* `z.record(z.string(), valueType)` form is valid under both Zod 3 and Zod 4.
*
* See GitHub issue #4295.
*
* Matches `z.record(...)` specifically (the conventional zod import alias in
* this repo) to avoid flagging unrelated single-argument `.record()` calls.
*/
const rule = {
meta: {
type: "problem",
docs: {
description:
"Disallow the single-argument z.record() form, which is removed in Zod 4",
},
fixable: "code",
schema: [],
messages: {
singleArgRecord:
"Use the two-argument form `z.record(z.string(), …)`. Zod 4 requires an explicit key schema, so the single-argument `z.record(value)` form is a compile-time error (TS2554) when built against Zod 4 (GitHub #4295).",
},
},
create(context) {
return {
CallExpression(node) {
const callee = node.callee;
if (
!callee ||
callee.type !== "MemberExpression" ||
callee.computed ||
!callee.object ||
callee.object.type !== "Identifier" ||
callee.object.name !== "z" ||
!callee.property ||
callee.property.type !== "Identifier" ||
callee.property.name !== "record"
) {
return;
}
if (node.arguments.length !== 1) return;
const arg = node.arguments[0];
// A spread argument cannot be safely rewritten — report without a fix.
if (arg.type === "SpreadElement") {
context.report({ node, messageId: "singleArgRecord" });
return;
}
context.report({
node,
messageId: "singleArgRecord",
fix(fixer) {
return fixer.insertTextBefore(arg, "z.string(), ");
},
});
},
};
},
};
export default rule;