From d7354fdf493cc9696be162fbebc5dfe4037ee4d6 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Fri, 16 Jan 2026 22:55:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20Zod=20Options=20=E4=B8=8E?= =?UTF-8?q?=20Description=20=E9=A1=BA=E5=BA=8F=E9=97=AE=E9=A2=98=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 readme.md diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..4740205 --- /dev/null +++ b/readme.md @@ -0,0 +1,37 @@ +# Zod Options 与 Description 顺序问题说明 + +## 问题描述 + +在使用 Zod 定义 Schema 时,如果 `.options()` 方法调用位于 `.describe()` 方法之后,会导致设置的 description 信息丢失。 + +## 错误示例 + +```typescript +import { z } from "zod"; + +// 错误:options 在 description 后面 +const schema = z.object({ + name: z.string().describe("用户名").options({ message: "请输入用户名" }), +}); +``` + +在上面的示例中,`describe("用户名")` 设置的描述信息会被后续的 `.options()` 调用覆盖。 + +## 正确示例 + +```typescript +import { z } from "zod"; + +// 正确:options 在 description 前面 +const schema = z.object({ + name: z.string().options({ message: "请输入用户名" }).describe("用户名"), +}); +``` + +## 原因分析 + +Zod 的方法链式调用中,`.options()` 会创建新的 schema 实例,导致之前通过 `.describe()` 设置的元数据丢失。 + +## 建议 + +在 Zod Schema 定义时,应确保 `.options()` 在 `.describe()` 之前调用,以保留描述信息。