用途:本技能提供 Zod 4 的 schema 校验模式参考,触发场景为使用 Zod 做校验、且涉及从 v3 升级的破坏性变更。
主要能力:涵盖 Zod 4 的主要写法。顶层校验器由 z.string().email() 变为 z.email(),同理有 z.uuid()、z.url();z.string().nonempty() 改为 z.string().min(1);错误信息统一用 error 参数,如 z.object({...},{error:"Required"})、z.string({error:"Name must be a string"})。还包含基础类型、约束(min/max/int/positive/multipleOf)、对象与 z.infer 类型推断、parse 与 safeParse 两种解析方式、数组/record/tuple、union 与 discriminatedUnion、transform 与 z.coerce 类型转换、preprocess、refine 与 superRefine 多条错误、optional/nullable/nullish/default,以及自定义 error map。
使用方式:从 "zod" 引入 z,定义 schema 后调用 parse(出错抛异常)或 safeParse(返回 {success,data/error})。React 表单可配合 react-hook-form 与 @hookform/resolvers/zod 的 zodResolver 使用。
限制:资料仅覆盖上述模式,未提供 Zod 4 的安装命令、版本要求、性能数据、平台支持或收费信息。