From a309180b2b8647551cffeb7b90a30101b885a6ff Mon Sep 17 00:00:00 2001 From: abearxiong Date: Tue, 27 Jan 2026 20:45:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BE=9D=E8=B5=96=E9=A1=B9?= =?UTF-8?q?=E7=89=88=E6=9C=AC=EF=BC=8C=E6=B7=BB=E5=8A=A0=20Zod=20schema=20?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 5 +++-- pnpm-lock.yaml | 22 +++++++++++++++++----- src/index.ts | 7 +++++-- src/schema.ts | 26 ++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 src/schema.ts diff --git a/package.json b/package.json index 393bd40..7fe1d87 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,10 @@ "keywords": [], "author": "abearxiong (https://www.xiongxiao.me)", "license": "MIT", - "packageManager": "pnpm@10.14.0", + "packageManager": "pnpm@10.28.2", "type": "module", "dependencies": { - "zod": "^4.3.5" + "zod": "^4.3.6", + "zod-to-json-schema": "^3.25.1" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 007e842..90e543f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,14 +9,26 @@ importers: .: dependencies: zod: - specifier: ^4.3.5 - version: 4.3.5 + specifier: ^4.3.6 + version: 4.3.6 + zod-to-json-schema: + specifier: ^3.25.1 + version: 3.25.1(zod@4.3.6) packages: - zod@4.3.5: - resolution: {integrity: sha512-k7Nwx6vuWx1IJ9Bjuf4Zt1PEllcwe7cls3VNzm4CQ1/hgtFUK2bRNG3rvnpPUhFjmqJKAKtjV576KnUkHocg/g==} + zod-to-json-schema@3.25.1: + resolution: {integrity: sha512-pM/SU9d3YAggzi6MtR4h7ruuQlqKtad8e9S0fmxcMi+ueAK5Korys/aWcV9LIIHTVbj01NdzxcnXSN+O74ZIVA==} + peerDependencies: + zod: ^3.25 || ^4 + + zod@4.3.6: + resolution: {integrity: sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg==} snapshots: - zod@4.3.5: {} + zod-to-json-schema@3.25.1(zod@4.3.6): + dependencies: + zod: 4.3.6 + + zod@4.3.6: {} diff --git a/src/index.ts b/src/index.ts index d3f615c..37ef4fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,10 @@ -import { z } from 'zod' +import { z, parse } from 'zod' const name = z.string().optional().describe('The name of the user') // name.optional() // console.log(name.description) // Output: The name of the user -console.log(JSON.stringify(name), name.description) // Output: The name of the user \ No newline at end of file +const str = JSON.stringify(name) + +console.log(JSON.stringify(name), name.description) // Output: The name of the user +// const nameClone = \ No newline at end of file diff --git a/src/schema.ts b/src/schema.ts new file mode 100644 index 0000000..3f91b1f --- /dev/null +++ b/src/schema.ts @@ -0,0 +1,26 @@ +import { z, toJSONSchema, fromJSONSchema } from 'zod' + +// 定义一个复杂的 Zod schema +const UserSchema = z.object({ + name: z.string().optional().describe('用户名'), + age: z.number().min(0).max(150).describe('年龄'), + email: z.email().describe('邮箱'), + tags: z.array(z.string()).describe('标签列表'), + isActive: z.boolean().default(false).describe('是否激活') +}).describe('用户信息') + +console.log('原始 Zod schema:', UserSchema.description) + + +// 示例 +const serialized = toJSONSchema(UserSchema) +console.log('序列化后的 JSON Schema:') +console.log(serialized) + +// 网络传输模拟 +const transmitted = serialized + +// 接收端反序列化 +const restoredSchema = fromJSONSchema(transmitted) +console.log('\n反序列化后的 schema 类型:', typeof restoredSchema) +console.log(restoredSchema.description) // trueF \ No newline at end of file