@chnak/zod-to-markdown 1.0.2 → 1.0.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # zod-to-markdown
2
2
 
3
- 将 Zod schema 转换为 Markdown 文档的工具函数。
3
+ 将 Zod schema 转换为 Markdown 文档的工具函数,支持两种输出格式:树状列表和表格。
4
4
 
5
5
  ## 安装
6
6
 
@@ -13,7 +13,7 @@ npm install @chnak/zod-to-markdown
13
13
  ### ESM / TypeScript
14
14
 
15
15
  ```typescript
16
- import { zodSchemaToMarkdown } from '@chnak/zod-to-markdown';
16
+ import { zodSchemaToMarkdown, zodSchemaToTable } from '@chnak/zod-to-markdown';
17
17
  import { z } from 'zod';
18
18
 
19
19
  const schema = z.object({
@@ -21,14 +21,19 @@ const schema = z.object({
21
21
  age: z.number(),
22
22
  });
23
23
 
24
+ // 树状格式
24
25
  const markdown = zodSchemaToMarkdown(schema);
25
26
  console.log(markdown);
27
+
28
+ // 表格格式
29
+ const table = zodSchemaToTable(schema);
30
+ console.log(table);
26
31
  ```
27
32
 
28
33
  ### CommonJS / Node.js
29
34
 
30
35
  ```javascript
31
- const { zodSchemaToMarkdown } = require('@chnak/zod-to-markdown');
36
+ const { zodSchemaToMarkdown, zodSchemaToTable } = require('@chnak/zod-to-markdown');
32
37
  const { z } = require('zod');
33
38
 
34
39
  const schema = z.object({
@@ -37,11 +42,15 @@ const schema = z.object({
37
42
  });
38
43
 
39
44
  const markdown = zodSchemaToMarkdown(schema);
45
+ const table = zodSchemaToTable(schema);
40
46
  console.log(markdown);
47
+ console.log(table);
41
48
  ```
42
49
 
43
50
  ### 输出结果
44
51
 
52
+ **树状格式 (`zodSchemaToMarkdown`)**:
53
+
45
54
  ```markdown
46
55
  - name
47
56
  - String
@@ -49,6 +58,15 @@ console.log(markdown);
49
58
  - Number
50
59
  ```
51
60
 
61
+ **表格格式 (`zodSchemaToTable`)**:
62
+
63
+ ```markdown
64
+ | 字段 | 类型 | 描述 |
65
+ |------|------|------|
66
+ | name | String | - |
67
+ | age | Number | - |
68
+ ```
69
+
52
70
  ## 支持的 Zod 类型
53
71
 
54
72
  ### 基础类型
@@ -161,6 +179,49 @@ console.log(zodSchemaToMarkdown(transformedSchema));
161
179
  - String
162
180
  ```
163
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
+
164
225
  ## API
165
226
 
166
227
  ### `zodSchemaToMarkdown(schema, indentLevel?)`
@@ -170,7 +231,15 @@ console.log(zodSchemaToMarkdown(transformedSchema));
170
231
  | `schema` | `z.ZodTypeAny` | 要转换的 Zod schema |
171
232
  | `indentLevel` | `number` | 初始缩进级别(默认: `0`) |
172
233
 
173
- 返回转换后的 Markdown 字符串。
234
+ 返回树状格式的 Markdown 字符串。
235
+
236
+ ### `zodSchemaToTable(schema)`
237
+
238
+ | 参数 | 类型 | 说明 |
239
+ |------|------|------|
240
+ | `schema` | `z.ZodTypeAny` | 要转换的 Zod schema(仅支持 ZodObject) |
241
+
242
+ 返回表格格式的 Markdown 字符串。非 ZodObject 类型会回退到树状格式。
174
243
 
175
244
  ## License
176
245
 
@@ -2,17 +2,17 @@
2
2
  |------|------|------|
3
3
  | id | String | 公司ID |
4
4
  | name | String | 公司名称 |
5
- | departments[] | name | 部门名称 |
6
- | departments[] | employees[] | 员工ID |
7
- | departments[] | employees[] | 姓名 |
8
- | departments[] | employees[] | 职位 |
9
- | departments[] | employees[] | 技能 |
10
- | departments[] | employees[] | 邮箱 |
11
- | departments[] | employees[] | 电话 |
12
- | departments[] | location.street | 街道 |
13
- | departments[] | location.city | 城市 |
14
- | departments[] | location.country | 国家 |
15
- | departments[] | location.coordinates.lat | 纬度 |
16
- | departments[] | location.coordinates.lng | 经度 |
5
+ | departments[].name | String | 部门名称 |
6
+ | departments[].employees[].id | String | 员工ID |
7
+ | departments[].employees[].name | String | 姓名 |
8
+ | departments[].employees[].title | String | 职位 |
9
+ | departments[].employees[].skills[] | Array<String> | 技能 |
10
+ | departments[].employees[].contact.email | String | 邮箱 |
11
+ | departments[].employees[].contact.phone | Optional<String> | 电话 |
12
+ | departments[].location.street | String | 街道 |
13
+ | departments[].location.city | String | 城市 |
14
+ | departments[].location.country | String | 国家 |
15
+ | departments[].location.coordinates.lat | Number | 纬度 |
16
+ | departments[].location.coordinates.lng | Number | 经度 |
17
17
  | foundedYear | Number | 成立年份 |
18
18
  | tags[] | Array<String> | 标签 |
package/lib/index.js CHANGED
@@ -72,9 +72,10 @@ function collectFields(schema, prefix, fields) {
72
72
  const innerFields = [];
73
73
  collectFields(element, '', innerFields);
74
74
  innerFields.forEach(field => {
75
+ const fieldPath = field.path || 'value';
75
76
  fields.push({
76
- path: `${prefix}[]`,
77
- type: field.path || getTypeString(element),
77
+ path: `${prefix}[].${fieldPath}`,
78
+ type: field.type,
78
79
  description: field.description,
79
80
  });
80
81
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chnak/zod-to-markdown",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "A utility function to convert Zod schemas to Markdown documentation",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",