@chnak/zod-to-markdown 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +246 -246
- package/examples/complex-schema-table.ts +60 -60
- package/examples/complex-types.md +152 -152
- package/examples/nested-object-example.ts +42 -42
- package/examples/openai-chat-completion.md +191 -191
- package/examples/openai-chat-schema.ts +64 -64
- package/jest.config.js +4 -4
- package/lib/index.js +246 -11
- package/lib/index.test.js +174 -70
- package/package.json +41 -41
package/README.md
CHANGED
|
@@ -1,246 +1,246 @@
|
|
|
1
|
-
# zod-to-markdown
|
|
2
|
-
|
|
3
|
-
将 Zod schema 转换为 Markdown 文档的工具函数,支持两种输出格式:树状列表和表格。
|
|
4
|
-
|
|
5
|
-
## 安装
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install @chnak/zod-to-markdown
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## 使用方法
|
|
12
|
-
|
|
13
|
-
### ESM / TypeScript
|
|
14
|
-
|
|
15
|
-
```typescript
|
|
16
|
-
import { zodSchemaToMarkdown, zodSchemaToTable } from '@chnak/zod-to-markdown';
|
|
17
|
-
import { z } from 'zod';
|
|
18
|
-
|
|
19
|
-
const schema = z.object({
|
|
20
|
-
name: z.string(),
|
|
21
|
-
age: z.number(),
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
// 树状格式
|
|
25
|
-
const markdown = zodSchemaToMarkdown(schema);
|
|
26
|
-
console.log(markdown);
|
|
27
|
-
|
|
28
|
-
// 表格格式
|
|
29
|
-
const table = zodSchemaToTable(schema);
|
|
30
|
-
console.log(table);
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
### CommonJS / Node.js
|
|
34
|
-
|
|
35
|
-
```javascript
|
|
36
|
-
const { zodSchemaToMarkdown, zodSchemaToTable } = require('@chnak/zod-to-markdown');
|
|
37
|
-
const { z } = require('zod');
|
|
38
|
-
|
|
39
|
-
const schema = z.object({
|
|
40
|
-
name: z.string(),
|
|
41
|
-
age: z.number(),
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
const markdown = zodSchemaToMarkdown(schema);
|
|
45
|
-
const table = zodSchemaToTable(schema);
|
|
46
|
-
console.log(markdown);
|
|
47
|
-
console.log(table);
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
### 输出结果
|
|
51
|
-
|
|
52
|
-
**树状格式 (`zodSchemaToMarkdown`)**:
|
|
53
|
-
|
|
54
|
-
```markdown
|
|
55
|
-
- name
|
|
56
|
-
- String
|
|
57
|
-
- age
|
|
58
|
-
- Number
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
**表格格式 (`zodSchemaToTable`)**:
|
|
62
|
-
|
|
63
|
-
```markdown
|
|
64
|
-
| 字段 | 类型 | 描述 |
|
|
65
|
-
|------|------|------|
|
|
66
|
-
| name | String | - |
|
|
67
|
-
| age | Number | - |
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
## 支持的 Zod 类型
|
|
71
|
-
|
|
72
|
-
### 基础类型
|
|
73
|
-
- `ZodObject` - 对象 schema,包含嵌套属性
|
|
74
|
-
- `ZodArray` - 数组 schema,包含元素类型
|
|
75
|
-
- `ZodString` - 字符串,可选 minLength/maxLength
|
|
76
|
-
- `ZodNumber` - 数字,可选 minValue/maxValue
|
|
77
|
-
- `ZodBoolean` - 布尔类型
|
|
78
|
-
- `ZodEnum` - 枚举值
|
|
79
|
-
- `ZodBigInt` - BigInt 类型
|
|
80
|
-
- `ZodDate` - Date 类型
|
|
81
|
-
|
|
82
|
-
### 工具类型
|
|
83
|
-
- `ZodOptional` - 可选属性
|
|
84
|
-
- `ZodNullable` - 可为空属性
|
|
85
|
-
- `ZodDefault` - 默认值
|
|
86
|
-
- `ZodEffects` - transform/coerce 效果
|
|
87
|
-
|
|
88
|
-
### 高级类型
|
|
89
|
-
- `ZodUnion` - 联合类型
|
|
90
|
-
- `ZodDiscriminatedUnion` - 带鉴别键的联合类型
|
|
91
|
-
- `ZodIntersection` - 交叉类型
|
|
92
|
-
- `ZodRecord` - Record(字典)类型
|
|
93
|
-
- `ZodTuple` - 元组类型
|
|
94
|
-
|
|
95
|
-
### 特殊类型
|
|
96
|
-
- `ZodLiteral` - 字面量值(如 `"hello"`, `1`, `true`)
|
|
97
|
-
- `ZodNaN` - NaN 类型
|
|
98
|
-
- `ZodNever` - Never 类型
|
|
99
|
-
- `ZodUnknown` - Unknown 类型
|
|
100
|
-
- `ZodVoid` - Void 类型
|
|
101
|
-
|
|
102
|
-
## 示例
|
|
103
|
-
|
|
104
|
-
### 复杂 Schema
|
|
105
|
-
|
|
106
|
-
```typescript
|
|
107
|
-
import { z } from 'zod';
|
|
108
|
-
|
|
109
|
-
const userSchema = z.object({
|
|
110
|
-
id: z.string().uuid(),
|
|
111
|
-
name: z.string(),
|
|
112
|
-
email: z.string().email(),
|
|
113
|
-
age: z.number().optional(),
|
|
114
|
-
role: z.enum(['admin', 'user', 'guest']),
|
|
115
|
-
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
console.log(zodSchemaToMarkdown(userSchema));
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
输出:
|
|
122
|
-
```markdown
|
|
123
|
-
- id
|
|
124
|
-
- String
|
|
125
|
-
- name
|
|
126
|
-
- String
|
|
127
|
-
- email
|
|
128
|
-
- String
|
|
129
|
-
- age
|
|
130
|
-
- Optional
|
|
131
|
-
- Number
|
|
132
|
-
- role
|
|
133
|
-
- Enum: admin, user, guest
|
|
134
|
-
- metadata
|
|
135
|
-
- Optional
|
|
136
|
-
- Record
|
|
137
|
-
Key:
|
|
138
|
-
- String
|
|
139
|
-
Value:
|
|
140
|
-
- Unknown
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
### 鉴别联合类型 (Discriminated Union)
|
|
144
|
-
|
|
145
|
-
```typescript
|
|
146
|
-
const messageSchema = z.discriminatedUnion('role', [
|
|
147
|
-
z.object({ role: z.literal('user'), content: z.string() }),
|
|
148
|
-
z.object({ role: z.literal('assistant'), content: z.string() }),
|
|
149
|
-
]);
|
|
150
|
-
|
|
151
|
-
console.log(zodSchemaToMarkdown(messageSchema));
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
输出:
|
|
155
|
-
```markdown
|
|
156
|
-
- DiscriminatedUnion (key: role)
|
|
157
|
-
- role
|
|
158
|
-
- Literal: "user"
|
|
159
|
-
- content
|
|
160
|
-
- String
|
|
161
|
-
|
|
|
162
|
-
- role
|
|
163
|
-
- Literal: "assistant"
|
|
164
|
-
- content
|
|
165
|
-
- String
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
### Transform 效果
|
|
169
|
-
|
|
170
|
-
```typescript
|
|
171
|
-
const transformedSchema = z.string().transform(val => val.length);
|
|
172
|
-
|
|
173
|
-
console.log(zodSchemaToMarkdown(transformedSchema));
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
输出:
|
|
177
|
-
```markdown
|
|
178
|
-
- Effects (transform)
|
|
179
|
-
- String
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
### 表格格式示例
|
|
183
|
-
|
|
184
|
-
```typescript
|
|
185
|
-
import { z } from 'zod';
|
|
186
|
-
|
|
187
|
-
const userSchema = z.object({
|
|
188
|
-
id: z.string().uuid().describe('用户ID'),
|
|
189
|
-
name: z.string().describe('姓名'),
|
|
190
|
-
profile: z.object({
|
|
191
|
-
bio: z.string().describe('个人简介'),
|
|
192
|
-
avatar: z.string().describe('头像URL'),
|
|
193
|
-
}).describe('用户资料'),
|
|
194
|
-
tags: z.array(z.string()).describe('标签'),
|
|
195
|
-
skills: z.record(z.string(), z.number()).describe('技能评分'),
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
console.log(zodSchemaToTable(userSchema));
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
输出:
|
|
202
|
-
```markdown
|
|
203
|
-
| 字段 | 类型 | 描述 |
|
|
204
|
-
|------|------|------|
|
|
205
|
-
| id | String | 用户ID |
|
|
206
|
-
| name | String | 姓名 |
|
|
207
|
-
| profile.bio | String | 个人简介 |
|
|
208
|
-
| profile.avatar | String | 头像URL |
|
|
209
|
-
| tags[] | Array<String> | 标签 |
|
|
210
|
-
| skills | Record<String, Number> | 技能评分 |
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
### 路径格式说明
|
|
214
|
-
|
|
215
|
-
表格格式使用点 `.` 和 `[]` 表示嵌套结构:
|
|
216
|
-
|
|
217
|
-
| 格式 | 含义 | 示例 |
|
|
218
|
-
|------|------|------|
|
|
219
|
-
| `field` | 普通字段 | `name` |
|
|
220
|
-
| `parent.child` | 对象嵌套 | `profile.bio` |
|
|
221
|
-
| `field[]` | 数组 | `tags[]` |
|
|
222
|
-
| `array[].field` | 对象数组的字段 | `users[].name` |
|
|
223
|
-
| `a[].b[].c` | 多层嵌套 | `departments[].employees[].name` |
|
|
224
|
-
|
|
225
|
-
## API
|
|
226
|
-
|
|
227
|
-
### `zodSchemaToMarkdown(schema, indentLevel?)`
|
|
228
|
-
|
|
229
|
-
| 参数 | 类型 | 说明 |
|
|
230
|
-
|------|------|------|
|
|
231
|
-
| `schema` | `z.ZodTypeAny` | 要转换的 Zod schema |
|
|
232
|
-
| `indentLevel` | `number` | 初始缩进级别(默认: `0`) |
|
|
233
|
-
|
|
234
|
-
返回树状格式的 Markdown 字符串。
|
|
235
|
-
|
|
236
|
-
### `zodSchemaToTable(schema)`
|
|
237
|
-
|
|
238
|
-
| 参数 | 类型 | 说明 |
|
|
239
|
-
|------|------|------|
|
|
240
|
-
| `schema` | `z.ZodTypeAny` | 要转换的 Zod schema(仅支持 ZodObject) |
|
|
241
|
-
|
|
242
|
-
返回表格格式的 Markdown 字符串。非 ZodObject 类型会回退到树状格式。
|
|
243
|
-
|
|
244
|
-
## License
|
|
245
|
-
|
|
246
|
-
MIT License
|
|
1
|
+
# zod-to-markdown
|
|
2
|
+
|
|
3
|
+
将 Zod schema 转换为 Markdown 文档的工具函数,支持两种输出格式:树状列表和表格。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @chnak/zod-to-markdown
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 使用方法
|
|
12
|
+
|
|
13
|
+
### ESM / TypeScript
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { zodSchemaToMarkdown, zodSchemaToTable } from '@chnak/zod-to-markdown';
|
|
17
|
+
import { z } from 'zod';
|
|
18
|
+
|
|
19
|
+
const schema = z.object({
|
|
20
|
+
name: z.string(),
|
|
21
|
+
age: z.number(),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// 树状格式
|
|
25
|
+
const markdown = zodSchemaToMarkdown(schema);
|
|
26
|
+
console.log(markdown);
|
|
27
|
+
|
|
28
|
+
// 表格格式
|
|
29
|
+
const table = zodSchemaToTable(schema);
|
|
30
|
+
console.log(table);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### CommonJS / Node.js
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
const { zodSchemaToMarkdown, zodSchemaToTable } = require('@chnak/zod-to-markdown');
|
|
37
|
+
const { z } = require('zod');
|
|
38
|
+
|
|
39
|
+
const schema = z.object({
|
|
40
|
+
name: z.string(),
|
|
41
|
+
age: z.number(),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const markdown = zodSchemaToMarkdown(schema);
|
|
45
|
+
const table = zodSchemaToTable(schema);
|
|
46
|
+
console.log(markdown);
|
|
47
|
+
console.log(table);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### 输出结果
|
|
51
|
+
|
|
52
|
+
**树状格式 (`zodSchemaToMarkdown`)**:
|
|
53
|
+
|
|
54
|
+
```markdown
|
|
55
|
+
- name
|
|
56
|
+
- String
|
|
57
|
+
- age
|
|
58
|
+
- Number
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
**表格格式 (`zodSchemaToTable`)**:
|
|
62
|
+
|
|
63
|
+
```markdown
|
|
64
|
+
| 字段 | 类型 | 描述 |
|
|
65
|
+
|------|------|------|
|
|
66
|
+
| name | String | - |
|
|
67
|
+
| age | Number | - |
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## 支持的 Zod 类型
|
|
71
|
+
|
|
72
|
+
### 基础类型
|
|
73
|
+
- `ZodObject` - 对象 schema,包含嵌套属性
|
|
74
|
+
- `ZodArray` - 数组 schema,包含元素类型
|
|
75
|
+
- `ZodString` - 字符串,可选 minLength/maxLength
|
|
76
|
+
- `ZodNumber` - 数字,可选 minValue/maxValue
|
|
77
|
+
- `ZodBoolean` - 布尔类型
|
|
78
|
+
- `ZodEnum` - 枚举值
|
|
79
|
+
- `ZodBigInt` - BigInt 类型
|
|
80
|
+
- `ZodDate` - Date 类型
|
|
81
|
+
|
|
82
|
+
### 工具类型
|
|
83
|
+
- `ZodOptional` - 可选属性
|
|
84
|
+
- `ZodNullable` - 可为空属性
|
|
85
|
+
- `ZodDefault` - 默认值
|
|
86
|
+
- `ZodEffects` - transform/coerce 效果
|
|
87
|
+
|
|
88
|
+
### 高级类型
|
|
89
|
+
- `ZodUnion` - 联合类型
|
|
90
|
+
- `ZodDiscriminatedUnion` - 带鉴别键的联合类型
|
|
91
|
+
- `ZodIntersection` - 交叉类型
|
|
92
|
+
- `ZodRecord` - Record(字典)类型
|
|
93
|
+
- `ZodTuple` - 元组类型
|
|
94
|
+
|
|
95
|
+
### 特殊类型
|
|
96
|
+
- `ZodLiteral` - 字面量值(如 `"hello"`, `1`, `true`)
|
|
97
|
+
- `ZodNaN` - NaN 类型
|
|
98
|
+
- `ZodNever` - Never 类型
|
|
99
|
+
- `ZodUnknown` - Unknown 类型
|
|
100
|
+
- `ZodVoid` - Void 类型
|
|
101
|
+
|
|
102
|
+
## 示例
|
|
103
|
+
|
|
104
|
+
### 复杂 Schema
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
import { z } from 'zod';
|
|
108
|
+
|
|
109
|
+
const userSchema = z.object({
|
|
110
|
+
id: z.string().uuid(),
|
|
111
|
+
name: z.string(),
|
|
112
|
+
email: z.string().email(),
|
|
113
|
+
age: z.number().optional(),
|
|
114
|
+
role: z.enum(['admin', 'user', 'guest']),
|
|
115
|
+
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
console.log(zodSchemaToMarkdown(userSchema));
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
输出:
|
|
122
|
+
```markdown
|
|
123
|
+
- id
|
|
124
|
+
- String
|
|
125
|
+
- name
|
|
126
|
+
- String
|
|
127
|
+
- email
|
|
128
|
+
- String
|
|
129
|
+
- age
|
|
130
|
+
- Optional
|
|
131
|
+
- Number
|
|
132
|
+
- role
|
|
133
|
+
- Enum: admin, user, guest
|
|
134
|
+
- metadata
|
|
135
|
+
- Optional
|
|
136
|
+
- Record
|
|
137
|
+
Key:
|
|
138
|
+
- String
|
|
139
|
+
Value:
|
|
140
|
+
- Unknown
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### 鉴别联合类型 (Discriminated Union)
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
const messageSchema = z.discriminatedUnion('role', [
|
|
147
|
+
z.object({ role: z.literal('user'), content: z.string() }),
|
|
148
|
+
z.object({ role: z.literal('assistant'), content: z.string() }),
|
|
149
|
+
]);
|
|
150
|
+
|
|
151
|
+
console.log(zodSchemaToMarkdown(messageSchema));
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
输出:
|
|
155
|
+
```markdown
|
|
156
|
+
- DiscriminatedUnion (key: role)
|
|
157
|
+
- role
|
|
158
|
+
- Literal: "user"
|
|
159
|
+
- content
|
|
160
|
+
- String
|
|
161
|
+
|
|
|
162
|
+
- role
|
|
163
|
+
- Literal: "assistant"
|
|
164
|
+
- content
|
|
165
|
+
- String
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Transform 效果
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
const transformedSchema = z.string().transform(val => val.length);
|
|
172
|
+
|
|
173
|
+
console.log(zodSchemaToMarkdown(transformedSchema));
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
输出:
|
|
177
|
+
```markdown
|
|
178
|
+
- Effects (transform)
|
|
179
|
+
- String
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### 表格格式示例
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
import { z } from 'zod';
|
|
186
|
+
|
|
187
|
+
const userSchema = z.object({
|
|
188
|
+
id: z.string().uuid().describe('用户ID'),
|
|
189
|
+
name: z.string().describe('姓名'),
|
|
190
|
+
profile: z.object({
|
|
191
|
+
bio: z.string().describe('个人简介'),
|
|
192
|
+
avatar: z.string().describe('头像URL'),
|
|
193
|
+
}).describe('用户资料'),
|
|
194
|
+
tags: z.array(z.string()).describe('标签'),
|
|
195
|
+
skills: z.record(z.string(), z.number()).describe('技能评分'),
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
console.log(zodSchemaToTable(userSchema));
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
输出:
|
|
202
|
+
```markdown
|
|
203
|
+
| 字段 | 类型 | 描述 |
|
|
204
|
+
|------|------|------|
|
|
205
|
+
| id | String | 用户ID |
|
|
206
|
+
| name | String | 姓名 |
|
|
207
|
+
| profile.bio | String | 个人简介 |
|
|
208
|
+
| profile.avatar | String | 头像URL |
|
|
209
|
+
| tags[] | Array<String> | 标签 |
|
|
210
|
+
| skills | Record<String, Number> | 技能评分 |
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### 路径格式说明
|
|
214
|
+
|
|
215
|
+
表格格式使用点 `.` 和 `[]` 表示嵌套结构:
|
|
216
|
+
|
|
217
|
+
| 格式 | 含义 | 示例 |
|
|
218
|
+
|------|------|------|
|
|
219
|
+
| `field` | 普通字段 | `name` |
|
|
220
|
+
| `parent.child` | 对象嵌套 | `profile.bio` |
|
|
221
|
+
| `field[]` | 数组 | `tags[]` |
|
|
222
|
+
| `array[].field` | 对象数组的字段 | `users[].name` |
|
|
223
|
+
| `a[].b[].c` | 多层嵌套 | `departments[].employees[].name` |
|
|
224
|
+
|
|
225
|
+
## API
|
|
226
|
+
|
|
227
|
+
### `zodSchemaToMarkdown(schema, indentLevel?)`
|
|
228
|
+
|
|
229
|
+
| 参数 | 类型 | 说明 |
|
|
230
|
+
|------|------|------|
|
|
231
|
+
| `schema` | `z.ZodTypeAny` | 要转换的 Zod schema |
|
|
232
|
+
| `indentLevel` | `number` | 初始缩进级别(默认: `0`) |
|
|
233
|
+
|
|
234
|
+
返回树状格式的 Markdown 字符串。
|
|
235
|
+
|
|
236
|
+
### `zodSchemaToTable(schema)`
|
|
237
|
+
|
|
238
|
+
| 参数 | 类型 | 说明 |
|
|
239
|
+
|------|------|------|
|
|
240
|
+
| `schema` | `z.ZodTypeAny` | 要转换的 Zod schema(仅支持 ZodObject) |
|
|
241
|
+
|
|
242
|
+
返回表格格式的 Markdown 字符串。非 ZodObject 类型会回退到树状格式。
|
|
243
|
+
|
|
244
|
+
## License
|
|
245
|
+
|
|
246
|
+
MIT License
|
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { zodSchemaToTable } from '../src/index';
|
|
3
|
-
import * as fs from 'fs';
|
|
4
|
-
|
|
5
|
-
// 多层嵌套 Schema 示例
|
|
6
|
-
|
|
7
|
-
// 第一层:坐标
|
|
8
|
-
const coordinatesSchema = z.object({
|
|
9
|
-
lat: z.number().describe('纬度'),
|
|
10
|
-
lng: z.number().describe('经度'),
|
|
11
|
-
}).describe('地理坐标');
|
|
12
|
-
|
|
13
|
-
// 第二层:地址
|
|
14
|
-
const addressSchema = z.object({
|
|
15
|
-
street: z.string().describe('街道'),
|
|
16
|
-
city: z.string().describe('城市'),
|
|
17
|
-
country: z.string().describe('国家'),
|
|
18
|
-
coordinates: coordinatesSchema.describe('坐标'),
|
|
19
|
-
}).describe('地址');
|
|
20
|
-
|
|
21
|
-
// 爱好
|
|
22
|
-
const hobbySchema = z.object({
|
|
23
|
-
name: z.string().describe('爱好名称'),
|
|
24
|
-
level: z.enum(['beginner', 'intermediate', 'advanced']).describe('等级'),
|
|
25
|
-
equipment: z.array(z.object({
|
|
26
|
-
name: z.string().describe('装备名称'),
|
|
27
|
-
brand: z.string().optional().describe('品牌'),
|
|
28
|
-
})).optional().describe('相关装备'),
|
|
29
|
-
}).describe('爱好');
|
|
30
|
-
|
|
31
|
-
// 主要 Schema
|
|
32
|
-
const companySchema = z.object({
|
|
33
|
-
id: z.string().uuid().describe('公司ID'),
|
|
34
|
-
name: z.string().describe('公司名称'),
|
|
35
|
-
departments: z.array(z.object({
|
|
36
|
-
name: z.string().describe('部门名称'),
|
|
37
|
-
employees: z.array(z.object({
|
|
38
|
-
id: z.string().describe('员工ID'),
|
|
39
|
-
name: z.string().describe('姓名'),
|
|
40
|
-
title: z.string().describe('职位'),
|
|
41
|
-
skills: z.array(z.string()).describe('技能'),
|
|
42
|
-
contact: z.object({
|
|
43
|
-
email: z.string().describe('邮箱'),
|
|
44
|
-
phone: z.string().optional().describe('电话'),
|
|
45
|
-
}).describe('联系方式'),
|
|
46
|
-
})).describe('员工列表'),
|
|
47
|
-
location: addressSchema.describe('部门地址'),
|
|
48
|
-
})).describe('部门列表'),
|
|
49
|
-
foundedYear: z.number().describe('成立年份'),
|
|
50
|
-
tags: z.array(z.string()).describe('标签'),
|
|
51
|
-
}).describe('公司');
|
|
52
|
-
|
|
53
|
-
const markdown = zodSchemaToTable(companySchema);
|
|
54
|
-
|
|
55
|
-
console.log('## Company Schema 文档 (多层级嵌套)\n');
|
|
56
|
-
console.log(markdown);
|
|
57
|
-
|
|
58
|
-
// 保存到文件
|
|
59
|
-
fs.writeFileSync('examples/company-schema-table.md', markdown);
|
|
60
|
-
console.log('\n已保存到 examples/company-schema-table.md');
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { zodSchemaToTable } from '../src/index';
|
|
3
|
+
import * as fs from 'fs';
|
|
4
|
+
|
|
5
|
+
// 多层嵌套 Schema 示例
|
|
6
|
+
|
|
7
|
+
// 第一层:坐标
|
|
8
|
+
const coordinatesSchema = z.object({
|
|
9
|
+
lat: z.number().describe('纬度'),
|
|
10
|
+
lng: z.number().describe('经度'),
|
|
11
|
+
}).describe('地理坐标');
|
|
12
|
+
|
|
13
|
+
// 第二层:地址
|
|
14
|
+
const addressSchema = z.object({
|
|
15
|
+
street: z.string().describe('街道'),
|
|
16
|
+
city: z.string().describe('城市'),
|
|
17
|
+
country: z.string().describe('国家'),
|
|
18
|
+
coordinates: coordinatesSchema.describe('坐标'),
|
|
19
|
+
}).describe('地址');
|
|
20
|
+
|
|
21
|
+
// 爱好
|
|
22
|
+
const hobbySchema = z.object({
|
|
23
|
+
name: z.string().describe('爱好名称'),
|
|
24
|
+
level: z.enum(['beginner', 'intermediate', 'advanced']).describe('等级'),
|
|
25
|
+
equipment: z.array(z.object({
|
|
26
|
+
name: z.string().describe('装备名称'),
|
|
27
|
+
brand: z.string().optional().describe('品牌'),
|
|
28
|
+
})).optional().describe('相关装备'),
|
|
29
|
+
}).describe('爱好');
|
|
30
|
+
|
|
31
|
+
// 主要 Schema
|
|
32
|
+
const companySchema = z.object({
|
|
33
|
+
id: z.string().uuid().describe('公司ID'),
|
|
34
|
+
name: z.string().describe('公司名称'),
|
|
35
|
+
departments: z.array(z.object({
|
|
36
|
+
name: z.string().describe('部门名称'),
|
|
37
|
+
employees: z.array(z.object({
|
|
38
|
+
id: z.string().describe('员工ID'),
|
|
39
|
+
name: z.string().describe('姓名'),
|
|
40
|
+
title: z.string().describe('职位'),
|
|
41
|
+
skills: z.array(z.string()).describe('技能'),
|
|
42
|
+
contact: z.object({
|
|
43
|
+
email: z.string().describe('邮箱'),
|
|
44
|
+
phone: z.string().optional().describe('电话'),
|
|
45
|
+
}).describe('联系方式'),
|
|
46
|
+
})).describe('员工列表'),
|
|
47
|
+
location: addressSchema.describe('部门地址'),
|
|
48
|
+
})).describe('部门列表'),
|
|
49
|
+
foundedYear: z.number().describe('成立年份'),
|
|
50
|
+
tags: z.array(z.string()).describe('标签'),
|
|
51
|
+
}).describe('公司');
|
|
52
|
+
|
|
53
|
+
const markdown = zodSchemaToTable(companySchema);
|
|
54
|
+
|
|
55
|
+
console.log('## Company Schema 文档 (多层级嵌套)\n');
|
|
56
|
+
console.log(markdown);
|
|
57
|
+
|
|
58
|
+
// 保存到文件
|
|
59
|
+
fs.writeFileSync('examples/company-schema-table.md', markdown);
|
|
60
|
+
console.log('\n已保存到 examples/company-schema-table.md');
|