Skip to content

feat: 迁移到 zod 4(dependabot #34) - #71

Open
Last-emo-boy wants to merge 4 commits into
betafrom
feat/zod-4-migration
Open

feat: 迁移到 zod 4(dependabot #34)#71
Last-emo-boy wants to merge 4 commits into
betafrom
feat/zod-4-migration

Conversation

@Last-emo-boy

Copy link
Copy Markdown
Owner

概述

将 zod 从 3.25.76 升级到 4.4.3,完成全量 API 迁移。对应 dependabot PR #34(运行时核心依赖、大版本迁移)。

迁移规模

  • 起始 649 个 tsc 错误 / 152 文件0 错误
  • 161 文件改动;全量 jest 套件与 beta(zod3) 基线逐项一致,零回归

主要变更

1. z.record 签名(540 + 7 处,codemod)
zod4 要求显式 key schema:z.record(value)z.record(z.string(), value)(zod3 单参默认 key 即 z.string(),语义不变)。

2. 内部 introspection 重写zod-schema-converter.tstool-help.ts

  • _def.* → 公开 .unwrap() / .element / .options / .values / .valueType + _zod.def
  • 删除已移除的 ZodEffects / ZodBranded 分支(transform 改用 ZodPipe
  • ZodDefault.defaultValue 由函数改为值
  • check 提取改读 zod4 _zod.def.checksmin_length/max_length/string_format/number_format/greater_than/less_than/multiple_of),恢复 JSON schema 的 minLength/format/minimum/integer 等约束
  • getSchemaGuidancecustom check 识别 refinement,恢复 x-guidance

3. .default({}).prefault({})(config / plugin-sdk / runtime-node / external-re-bridge)
zod4 .default() 值需匹配 output;依赖嵌套默认填充的对象改用 .prefault()(= zod3 旧 default 行为)。

4. API 重命名 / 枚举推导

  • ZodError.errors.issuesSafeParseSuccessZodSafeParseSuccessZodIssueCode.invalid_enum_value'invalid_value'
  • shared runtime-control-plane:两处 z.enumas const,修复 zod4 重载误匹配导致枚举长度泄漏进 z.infer(如 RuntimeTaskStatus 出现 5

5. 依赖
所有包 zod ^4.4.3@rikune/plugin-sdk peer 放宽 ^3.0.0 || ^4.0.0

6. 测试期望
3 处更新到 zod4 默认错误消息(Invalid input: expected record... / Too small: expected number to be >=1 / Invalid option: expected one of);自定义错误消息保持不变

验证

  • tsc --noEmit:0 错误(主项目 + 4 个 workspace 包)
  • npm run build:通过;npm run lint:0 错误
  • 全量 jest(373 suites / 2529 tests):与 beta 基线完全一致(20 个失败均为 ghidra 未安装 / Windows 专属工具的环境类用例,beta 同样失败)

🤖 Generated with Claude Code

Last-emo-boy and others added 4 commits June 29, 2026 18:04
- 所有包 zod ^3.25.76 -> ^4.4.3;plugin-sdk peer 放宽 ^3.0.0 || ^4.0.0
- codemod: z.record(value) -> z.record(z.string(), value)(zod4 要求显式 key
  schema;zod3 单参默认 key 即 z.string(),语义不变),144 文件 540 处
- 剩余 77 个 zod4 API 杂项错误将在后续提交逐项修复

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- tool-help + zod-schema-converter: _def.* -> 公开 .unwrap()/.element/.options/
  .values/.valueType + _zod.def;删除已移除的 ZodEffects/ZodBranded 分支(改 ZodPipe);
  ZodDefault.defaultValue 由函数改为值
- config/plugin-sdk/runtime-node: .default({}) -> .prefault({})(zod4 default 语义变更)
- tool-executor + report-summarize: ZodError.errors -> .issues;
  SafeParseSuccess -> ZodSafeParseSuccess;invalid_enum_value -> 'invalid_value'
- shared runtime-control-plane: 两处 z.enum 加 as const(修 zod4 重载误匹配导致
  枚举长度泄漏进推导类型,如 RuntimeTaskStatus 出现 5)
- packages 内 z.record codemod(7 处)

tsc 0 错误、npm run build(4 包 + emit)通过。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- zod-schema-converter: applyString/Number/ArrayChecks 改读 zod4 的 _zod.def.checks
  结构(min_length/max_length/string_format/number_format/greater_than/less_than/
  multiple_of),恢复 JSON schema 的 minLength/maxLength/format/minimum/integer 等约束;
  getSchemaGuidance 按 custom check 识别 refinement(ZodEffects 已移除)恢复 x-guidance
- 测试期望更新到 zod4 默认消息:ZodError.errors->.issues;
  'Expected object, received array'->'Invalid input: expected record...';
  数字 'Number must be >='->'Too small: expected number to be >=1';
  'Invalid enum value'->'Invalid option: expected one of'(自定义消息保持不变)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant