@chnak/zod-to-markdown 1.0.2 → 1.0.4

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,177 +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 } 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
- const markdown = zodSchemaToMarkdown(schema);
25
- console.log(markdown);
26
- ```
27
-
28
- ### CommonJS / Node.js
29
-
30
- ```javascript
31
- const { zodSchemaToMarkdown } = require('@chnak/zod-to-markdown');
32
- const { z } = require('zod');
33
-
34
- const schema = z.object({
35
- name: z.string(),
36
- age: z.number(),
37
- });
38
-
39
- const markdown = zodSchemaToMarkdown(schema);
40
- console.log(markdown);
41
- ```
42
-
43
- ### 输出结果
44
-
45
- ```markdown
46
- - name
47
- - String
48
- - age
49
- - Number
50
- ```
51
-
52
- ## 支持的 Zod 类型
53
-
54
- ### 基础类型
55
- - `ZodObject` - 对象 schema,包含嵌套属性
56
- - `ZodArray` - 数组 schema,包含元素类型
57
- - `ZodString` - 字符串,可选 minLength/maxLength
58
- - `ZodNumber` - 数字,可选 minValue/maxValue
59
- - `ZodBoolean` - 布尔类型
60
- - `ZodEnum` - 枚举值
61
- - `ZodBigInt` - BigInt 类型
62
- - `ZodDate` - Date 类型
63
-
64
- ### 工具类型
65
- - `ZodOptional` - 可选属性
66
- - `ZodNullable` - 可为空属性
67
- - `ZodDefault` - 默认值
68
- - `ZodEffects` - transform/coerce 效果
69
-
70
- ### 高级类型
71
- - `ZodUnion` - 联合类型
72
- - `ZodDiscriminatedUnion` - 带鉴别键的联合类型
73
- - `ZodIntersection` - 交叉类型
74
- - `ZodRecord` - Record(字典)类型
75
- - `ZodTuple` - 元组类型
76
-
77
- ### 特殊类型
78
- - `ZodLiteral` - 字面量值(如 `"hello"`, `1`, `true`)
79
- - `ZodNaN` - NaN 类型
80
- - `ZodNever` - Never 类型
81
- - `ZodUnknown` - Unknown 类型
82
- - `ZodVoid` - Void 类型
83
-
84
- ## 示例
85
-
86
- ### 复杂 Schema
87
-
88
- ```typescript
89
- import { z } from 'zod';
90
-
91
- const userSchema = z.object({
92
- id: z.string().uuid(),
93
- name: z.string(),
94
- email: z.string().email(),
95
- age: z.number().optional(),
96
- role: z.enum(['admin', 'user', 'guest']),
97
- metadata: z.record(z.string(), z.unknown()).optional(),
98
- });
99
-
100
- console.log(zodSchemaToMarkdown(userSchema));
101
- ```
102
-
103
- 输出:
104
- ```markdown
105
- - id
106
- - String
107
- - name
108
- - String
109
- - email
110
- - String
111
- - age
112
- - Optional
113
- - Number
114
- - role
115
- - Enum: admin, user, guest
116
- - metadata
117
- - Optional
118
- - Record
119
- Key:
120
- - String
121
- Value:
122
- - Unknown
123
- ```
124
-
125
- ### 鉴别联合类型 (Discriminated Union)
126
-
127
- ```typescript
128
- const messageSchema = z.discriminatedUnion('role', [
129
- z.object({ role: z.literal('user'), content: z.string() }),
130
- z.object({ role: z.literal('assistant'), content: z.string() }),
131
- ]);
132
-
133
- console.log(zodSchemaToMarkdown(messageSchema));
134
- ```
135
-
136
- 输出:
137
- ```markdown
138
- - DiscriminatedUnion (key: role)
139
- - role
140
- - Literal: "user"
141
- - content
142
- - String
143
- |
144
- - role
145
- - Literal: "assistant"
146
- - content
147
- - String
148
- ```
149
-
150
- ### Transform 效果
151
-
152
- ```typescript
153
- const transformedSchema = z.string().transform(val => val.length);
154
-
155
- console.log(zodSchemaToMarkdown(transformedSchema));
156
- ```
157
-
158
- 输出:
159
- ```markdown
160
- - Effects (transform)
161
- - String
162
- ```
163
-
164
- ## API
165
-
166
- ### `zodSchemaToMarkdown(schema, indentLevel?)`
167
-
168
- | 参数 | 类型 | 说明 |
169
- |------|------|------|
170
- | `schema` | `z.ZodTypeAny` | 要转换的 Zod schema |
171
- | `indentLevel` | `number` | 初始缩进级别(默认: `0`) |
172
-
173
- 返回转换后的 Markdown 字符串。
174
-
175
- ## License
176
-
177
- 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
@@ -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> | 标签 |
@@ -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');