@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 +246 -177
- package/examples/company-schema-table.md +12 -12
- 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 +222 -13
- package/lib/index.test.js +174 -70
- package/package.json +41 -41
package/lib/index.js
CHANGED
|
@@ -26,19 +26,24 @@ function zodSchemaToTable(schema) {
|
|
|
26
26
|
exports.zodSchemaToTable = zodSchemaToTable;
|
|
27
27
|
/**
|
|
28
28
|
* 递归收集所有嵌套字段
|
|
29
|
+
* @param wrapperType - 当处于 Optional/Nullable 包装内部时,传入 'Optional' 或 'Nullable'
|
|
29
30
|
*/
|
|
30
|
-
function collectFields(schema, prefix, fields) {
|
|
31
|
+
function collectFields(schema, prefix, fields, wrapperType) {
|
|
31
32
|
// 处理 Optional - 包装类型
|
|
32
33
|
if (schema instanceof zod_1.z.ZodOptional) {
|
|
33
|
-
const
|
|
34
|
+
const inner = schema.unwrap();
|
|
34
35
|
// 对于简单类型直接包装
|
|
35
|
-
if (
|
|
36
|
-
collectFields(
|
|
36
|
+
if (inner instanceof zod_1.z.ZodObject || inner instanceof zod_1.z.ZodArray) {
|
|
37
|
+
collectFields(inner, prefix, fields, 'Optional');
|
|
38
|
+
}
|
|
39
|
+
else if (inner instanceof zod_1.z.ZodUnion || inner instanceof zod_1.z.ZodDiscriminatedUnion || inner instanceof zod_1.z.ZodIntersection) {
|
|
40
|
+
// 嵌套的复杂类型也需要展开
|
|
41
|
+
collectFields(inner, prefix, fields, 'Optional');
|
|
37
42
|
}
|
|
38
43
|
else {
|
|
39
44
|
fields.push({
|
|
40
45
|
path: prefix,
|
|
41
|
-
type: `Optional<${
|
|
46
|
+
type: `Optional<${getTypeString(inner)}>`,
|
|
42
47
|
description: schema.description || '-',
|
|
43
48
|
});
|
|
44
49
|
}
|
|
@@ -46,14 +51,18 @@ function collectFields(schema, prefix, fields) {
|
|
|
46
51
|
}
|
|
47
52
|
// 处理 Nullable - 包装类型
|
|
48
53
|
if (schema instanceof zod_1.z.ZodNullable) {
|
|
49
|
-
const
|
|
50
|
-
if (
|
|
51
|
-
collectFields(
|
|
54
|
+
const inner = schema.unwrap();
|
|
55
|
+
if (inner instanceof zod_1.z.ZodObject || inner instanceof zod_1.z.ZodArray) {
|
|
56
|
+
collectFields(inner, prefix, fields, 'Nullable');
|
|
57
|
+
}
|
|
58
|
+
else if (inner instanceof zod_1.z.ZodUnion || inner instanceof zod_1.z.ZodDiscriminatedUnion || inner instanceof zod_1.z.ZodIntersection) {
|
|
59
|
+
// 嵌套的复杂类型也需要展开
|
|
60
|
+
collectFields(inner, prefix, fields, 'Nullable');
|
|
52
61
|
}
|
|
53
62
|
else {
|
|
54
63
|
fields.push({
|
|
55
64
|
path: prefix,
|
|
56
|
-
type: `Nullable<${
|
|
65
|
+
type: `Nullable<${getTypeString(inner)}>`,
|
|
57
66
|
description: schema.description || '-',
|
|
58
67
|
});
|
|
59
68
|
}
|
|
@@ -72,13 +81,85 @@ function collectFields(schema, prefix, fields) {
|
|
|
72
81
|
const innerFields = [];
|
|
73
82
|
collectFields(element, '', innerFields);
|
|
74
83
|
innerFields.forEach(field => {
|
|
84
|
+
const fieldPath = field.path || 'value';
|
|
75
85
|
fields.push({
|
|
76
|
-
path: `${prefix}[]`,
|
|
77
|
-
type: field.
|
|
86
|
+
path: `${prefix}[].${fieldPath}`,
|
|
87
|
+
type: field.type,
|
|
78
88
|
description: field.description,
|
|
79
89
|
});
|
|
80
90
|
});
|
|
81
91
|
}
|
|
92
|
+
else if (element instanceof zod_1.z.ZodUnion || element instanceof zod_1.z.ZodDiscriminatedUnion) {
|
|
93
|
+
// 数组元素是 Union 类型时,展开每个选项
|
|
94
|
+
element.options.forEach((opt, index) => {
|
|
95
|
+
// 对于 ZodOptional/ZodNullable,保留包装类型信息
|
|
96
|
+
if (opt instanceof zod_1.z.ZodOptional || opt instanceof zod_1.z.ZodNullable) {
|
|
97
|
+
const typeStr = getTypeString(opt);
|
|
98
|
+
const inner = opt instanceof zod_1.z.ZodOptional ? opt.unwrap() : opt.unwrap();
|
|
99
|
+
if (inner instanceof zod_1.z.ZodObject || inner instanceof zod_1.z.ZodIntersection) {
|
|
100
|
+
const innerFields = [];
|
|
101
|
+
collectFields(inner, '', innerFields);
|
|
102
|
+
innerFields.forEach(field => {
|
|
103
|
+
fields.push({
|
|
104
|
+
path: `${prefix}[].${field.path} (option ${index + 1})`,
|
|
105
|
+
type: field.type,
|
|
106
|
+
description: field.description,
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
fields.push({
|
|
112
|
+
path: `${prefix}[] (option ${index + 1})`,
|
|
113
|
+
type: typeStr,
|
|
114
|
+
description: opt.description || '-',
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else if (opt instanceof zod_1.z.ZodObject || opt instanceof zod_1.z.ZodIntersection) {
|
|
119
|
+
// 对象类型或交叉类型,递归展开
|
|
120
|
+
const innerFields = [];
|
|
121
|
+
collectFields(opt, '', innerFields);
|
|
122
|
+
innerFields.forEach(field => {
|
|
123
|
+
fields.push({
|
|
124
|
+
path: `${prefix}[].${field.path} (option ${index + 1})`,
|
|
125
|
+
type: field.type,
|
|
126
|
+
description: field.description,
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
// 其他简单类型
|
|
132
|
+
fields.push({
|
|
133
|
+
path: `${prefix}[] (option ${index + 1})`,
|
|
134
|
+
type: getTypeString(opt),
|
|
135
|
+
description: opt.description || '-',
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
else if (element instanceof zod_1.z.ZodOptional) {
|
|
141
|
+
// 数组元素是 Optional 类型时,展开内部类型
|
|
142
|
+
const inner = element.unwrap();
|
|
143
|
+
if (inner instanceof zod_1.z.ZodObject) {
|
|
144
|
+
const innerFields = [];
|
|
145
|
+
collectFields(inner, '', innerFields);
|
|
146
|
+
innerFields.forEach(field => {
|
|
147
|
+
const fieldPath = field.path || 'value';
|
|
148
|
+
fields.push({
|
|
149
|
+
path: `${prefix}[].${fieldPath}`,
|
|
150
|
+
type: `Optional<${field.type}>`,
|
|
151
|
+
description: field.description,
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
fields.push({
|
|
157
|
+
path: `${prefix}[]`,
|
|
158
|
+
type: `Optional<${getTypeString(inner)}>`,
|
|
159
|
+
description: element.description || '-',
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
82
163
|
else {
|
|
83
164
|
// 非对象数组,保持原样
|
|
84
165
|
fields.push({
|
|
@@ -95,7 +176,128 @@ function collectFields(schema, prefix, fields) {
|
|
|
95
176
|
Object.keys(shape).forEach(key => {
|
|
96
177
|
const subSchema = shape[key];
|
|
97
178
|
const newPath = prefix ? `${prefix}.${key}` : key;
|
|
98
|
-
collectFields(subSchema, newPath, fields);
|
|
179
|
+
collectFields(subSchema, newPath, fields, wrapperType);
|
|
180
|
+
});
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
// 处理 ZodUnion - 展开每个选项
|
|
184
|
+
if (schema instanceof zod_1.z.ZodUnion) {
|
|
185
|
+
schema.options.forEach((opt, index) => {
|
|
186
|
+
// 对于 ZodOptional/ZodNullable,保留包装类型信息
|
|
187
|
+
if (opt instanceof zod_1.z.ZodOptional || opt instanceof zod_1.z.ZodNullable) {
|
|
188
|
+
const typeStr = getTypeString(opt);
|
|
189
|
+
const innerWrapperType = opt instanceof zod_1.z.ZodOptional ? 'Optional' : 'Nullable';
|
|
190
|
+
const inner = opt instanceof zod_1.z.ZodOptional ? opt.unwrap() : opt.unwrap();
|
|
191
|
+
// 只有内部是对象或交叉类型才展开,否则直接使用类型字符串
|
|
192
|
+
if (inner instanceof zod_1.z.ZodObject || inner instanceof zod_1.z.ZodIntersection) {
|
|
193
|
+
const innerFields = [];
|
|
194
|
+
collectFields(inner, '', innerFields, innerWrapperType);
|
|
195
|
+
innerFields.forEach(field => {
|
|
196
|
+
fields.push({
|
|
197
|
+
path: field.path ? `${prefix} (option ${index + 1}): ${field.path}` : `${prefix} (option ${index + 1})`,
|
|
198
|
+
type: field.type,
|
|
199
|
+
description: field.description,
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
fields.push({
|
|
205
|
+
path: `${prefix} (option ${index + 1})`,
|
|
206
|
+
type: typeStr,
|
|
207
|
+
description: opt.description || '-',
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
else if (opt instanceof zod_1.z.ZodObject || opt instanceof zod_1.z.ZodIntersection) {
|
|
212
|
+
// 对象类型或交叉类型,递归展开
|
|
213
|
+
const innerFields = [];
|
|
214
|
+
collectFields(opt, '', innerFields);
|
|
215
|
+
innerFields.forEach(field => {
|
|
216
|
+
fields.push({
|
|
217
|
+
path: field.path ? `${prefix} (option ${index + 1}): ${field.path}` : `${prefix} (option ${index + 1})`,
|
|
218
|
+
type: field.type,
|
|
219
|
+
description: field.description,
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
// 其他简单类型
|
|
225
|
+
fields.push({
|
|
226
|
+
path: `${prefix} (option ${index + 1})`,
|
|
227
|
+
type: getTypeString(opt),
|
|
228
|
+
description: opt.description || '-',
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
// 处理 ZodDiscriminatedUnion - 类似 Union 但有关联键
|
|
235
|
+
if (schema instanceof zod_1.z.ZodDiscriminatedUnion) {
|
|
236
|
+
schema.options.forEach((opt, index) => {
|
|
237
|
+
// 对于 ZodOptional/ZodNullable,保留包装类型信息
|
|
238
|
+
if (opt instanceof zod_1.z.ZodOptional || opt instanceof zod_1.z.ZodNullable) {
|
|
239
|
+
const typeStr = getTypeString(opt);
|
|
240
|
+
const innerWrapperType = opt instanceof zod_1.z.ZodOptional ? 'Optional' : 'Nullable';
|
|
241
|
+
const inner = opt instanceof zod_1.z.ZodOptional ? opt.unwrap() : opt.unwrap();
|
|
242
|
+
if (inner instanceof zod_1.z.ZodObject || inner instanceof zod_1.z.ZodIntersection) {
|
|
243
|
+
const innerFields = [];
|
|
244
|
+
collectFields(inner, '', innerFields, innerWrapperType);
|
|
245
|
+
innerFields.forEach(field => {
|
|
246
|
+
fields.push({
|
|
247
|
+
path: field.path ? `${prefix} (option ${index + 1}): ${field.path}` : `${prefix} (option ${index + 1})`,
|
|
248
|
+
type: field.type,
|
|
249
|
+
description: field.description,
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
fields.push({
|
|
255
|
+
path: `${prefix} (option ${index + 1})`,
|
|
256
|
+
type: typeStr,
|
|
257
|
+
description: opt.description || '-',
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
else if (opt instanceof zod_1.z.ZodObject || opt instanceof zod_1.z.ZodIntersection) {
|
|
262
|
+
const innerFields = [];
|
|
263
|
+
collectFields(opt, '', innerFields);
|
|
264
|
+
innerFields.forEach(field => {
|
|
265
|
+
fields.push({
|
|
266
|
+
path: field.path ? `${prefix} (option ${index + 1}): ${field.path}` : `${prefix} (option ${index + 1})`,
|
|
267
|
+
type: field.type,
|
|
268
|
+
description: field.description,
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
fields.push({
|
|
274
|
+
path: `${prefix} (option ${index + 1})`,
|
|
275
|
+
type: getTypeString(opt),
|
|
276
|
+
description: opt.description || '-',
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
// 处理 ZodIntersection - 展开左右两部分
|
|
283
|
+
if (schema instanceof zod_1.z.ZodIntersection) {
|
|
284
|
+
const leftFields = [];
|
|
285
|
+
const rightFields = [];
|
|
286
|
+
collectFields(schema._def.left, '', leftFields);
|
|
287
|
+
collectFields(schema._def.right, '', rightFields);
|
|
288
|
+
leftFields.forEach(field => {
|
|
289
|
+
fields.push({
|
|
290
|
+
path: `${prefix}: ${field.path} (Left)`,
|
|
291
|
+
type: field.type,
|
|
292
|
+
description: field.description,
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
rightFields.forEach(field => {
|
|
296
|
+
fields.push({
|
|
297
|
+
path: `${prefix}: ${field.path} (Right)`,
|
|
298
|
+
type: field.type,
|
|
299
|
+
description: field.description,
|
|
300
|
+
});
|
|
99
301
|
});
|
|
100
302
|
return;
|
|
101
303
|
}
|
|
@@ -119,9 +321,16 @@ function collectFields(schema, prefix, fields) {
|
|
|
119
321
|
return;
|
|
120
322
|
}
|
|
121
323
|
// 其他类型直接添加
|
|
324
|
+
let typeStr = getTypeString(schema);
|
|
325
|
+
if (wrapperType === 'Optional' && !typeStr.startsWith('Optional<') && !typeStr.startsWith('Nullable<')) {
|
|
326
|
+
typeStr = `Optional<${typeStr}>`;
|
|
327
|
+
}
|
|
328
|
+
else if (wrapperType === 'Nullable' && !typeStr.startsWith('Optional<') && !typeStr.startsWith('Nullable<')) {
|
|
329
|
+
typeStr = `Nullable<${typeStr}>`;
|
|
330
|
+
}
|
|
122
331
|
fields.push({
|
|
123
332
|
path: prefix,
|
|
124
|
-
type:
|
|
333
|
+
type: typeStr,
|
|
125
334
|
description: schema.description || '-',
|
|
126
335
|
});
|
|
127
336
|
}
|
package/lib/index.test.js
CHANGED
|
@@ -8,33 +8,33 @@ describe('zodSchemaToMarkdown', () => {
|
|
|
8
8
|
name: zod_1.z.string(),
|
|
9
9
|
age: zod_1.z.number(),
|
|
10
10
|
});
|
|
11
|
-
const expected = `- name
|
|
12
|
-
- String
|
|
13
|
-
- age
|
|
14
|
-
- Number
|
|
11
|
+
const expected = `- name
|
|
12
|
+
- String
|
|
13
|
+
- age
|
|
14
|
+
- Number
|
|
15
15
|
`;
|
|
16
16
|
expect((0, index_1.zodSchemaToMarkdown)(schema)).toBe(expected);
|
|
17
17
|
});
|
|
18
18
|
it('should convert an array schema to markdown', () => {
|
|
19
19
|
const schema = zod_1.z.array(zod_1.z.string());
|
|
20
|
-
const expected = `- Array
|
|
21
|
-
- String
|
|
20
|
+
const expected = `- Array
|
|
21
|
+
- String
|
|
22
22
|
`;
|
|
23
23
|
expect((0, index_1.zodSchemaToMarkdown)(schema)).toBe(expected);
|
|
24
24
|
});
|
|
25
25
|
it('should convert a union schema to markdown', () => {
|
|
26
26
|
const schema = zod_1.z.union([zod_1.z.string(), zod_1.z.number()]);
|
|
27
|
-
const expected = `- Union
|
|
28
|
-
- String
|
|
29
|
-
|
|
|
30
|
-
- Number
|
|
27
|
+
const expected = `- Union
|
|
28
|
+
- String
|
|
29
|
+
|
|
|
30
|
+
- Number
|
|
31
31
|
`;
|
|
32
32
|
expect((0, index_1.zodSchemaToMarkdown)(schema)).toBe(expected);
|
|
33
33
|
});
|
|
34
34
|
it('should convert ZodEffects (transform) to markdown', () => {
|
|
35
35
|
const schema = zod_1.z.string().transform(val => val.length);
|
|
36
|
-
const expected = `- Effects (transform)
|
|
37
|
-
- String
|
|
36
|
+
const expected = `- Effects (transform)
|
|
37
|
+
- String
|
|
38
38
|
`;
|
|
39
39
|
expect((0, index_1.zodSchemaToMarkdown)(schema)).toBe(expected);
|
|
40
40
|
});
|
|
@@ -43,90 +43,90 @@ describe('zodSchemaToMarkdown', () => {
|
|
|
43
43
|
zod_1.z.object({ type: zod_1.z.literal('a'), a: zod_1.z.string() }),
|
|
44
44
|
zod_1.z.object({ type: zod_1.z.literal('b'), b: zod_1.z.number() })
|
|
45
45
|
]);
|
|
46
|
-
const expected = `- DiscriminatedUnion (key: type)
|
|
47
|
-
- type
|
|
48
|
-
- Literal: "a"
|
|
49
|
-
- a
|
|
50
|
-
- String
|
|
51
|
-
|
|
|
52
|
-
- type
|
|
53
|
-
- Literal: "b"
|
|
54
|
-
- b
|
|
55
|
-
- Number
|
|
46
|
+
const expected = `- DiscriminatedUnion (key: type)
|
|
47
|
+
- type
|
|
48
|
+
- Literal: "a"
|
|
49
|
+
- a
|
|
50
|
+
- String
|
|
51
|
+
|
|
|
52
|
+
- type
|
|
53
|
+
- Literal: "b"
|
|
54
|
+
- b
|
|
55
|
+
- Number
|
|
56
56
|
`;
|
|
57
57
|
expect((0, index_1.zodSchemaToMarkdown)(schema)).toBe(expected);
|
|
58
58
|
});
|
|
59
59
|
it('should convert ZodIntersection to markdown', () => {
|
|
60
60
|
const schema = zod_1.z.object({ a: zod_1.z.string() }).and(zod_1.z.object({ b: zod_1.z.number() }));
|
|
61
|
-
const expected = `- Intersection
|
|
62
|
-
Left:
|
|
63
|
-
- a
|
|
64
|
-
- String
|
|
65
|
-
Right:
|
|
66
|
-
- b
|
|
67
|
-
- Number
|
|
61
|
+
const expected = `- Intersection
|
|
62
|
+
Left:
|
|
63
|
+
- a
|
|
64
|
+
- String
|
|
65
|
+
Right:
|
|
66
|
+
- b
|
|
67
|
+
- Number
|
|
68
68
|
`;
|
|
69
69
|
expect((0, index_1.zodSchemaToMarkdown)(schema)).toBe(expected);
|
|
70
70
|
});
|
|
71
71
|
it('should convert ZodRecord to markdown', () => {
|
|
72
72
|
const schema = zod_1.z.record(zod_1.z.string(), zod_1.z.number());
|
|
73
|
-
const expected = `- Record
|
|
74
|
-
Key:
|
|
75
|
-
- String
|
|
76
|
-
Value:
|
|
77
|
-
- Number
|
|
73
|
+
const expected = `- Record
|
|
74
|
+
Key:
|
|
75
|
+
- String
|
|
76
|
+
Value:
|
|
77
|
+
- Number
|
|
78
78
|
`;
|
|
79
79
|
expect((0, index_1.zodSchemaToMarkdown)(schema)).toBe(expected);
|
|
80
80
|
});
|
|
81
81
|
it('should convert ZodTuple to markdown', () => {
|
|
82
82
|
const schema = zod_1.z.tuple([zod_1.z.string(), zod_1.z.number()]);
|
|
83
|
-
const expected = `- Tuple
|
|
84
|
-
[0]:
|
|
85
|
-
- String
|
|
86
|
-
[1]:
|
|
87
|
-
- Number
|
|
83
|
+
const expected = `- Tuple
|
|
84
|
+
[0]:
|
|
85
|
+
- String
|
|
86
|
+
[1]:
|
|
87
|
+
- Number
|
|
88
88
|
`;
|
|
89
89
|
expect((0, index_1.zodSchemaToMarkdown)(schema)).toBe(expected);
|
|
90
90
|
});
|
|
91
91
|
it('should convert ZodLiteral to markdown', () => {
|
|
92
92
|
const schema = zod_1.z.literal('hello');
|
|
93
|
-
const expected = `- Literal: "hello"
|
|
93
|
+
const expected = `- Literal: "hello"
|
|
94
94
|
`;
|
|
95
95
|
expect((0, index_1.zodSchemaToMarkdown)(schema)).toBe(expected);
|
|
96
96
|
});
|
|
97
97
|
it('should convert ZodBigInt to markdown', () => {
|
|
98
98
|
const schema = zod_1.z.bigint();
|
|
99
|
-
const expected = `- BigInt
|
|
99
|
+
const expected = `- BigInt
|
|
100
100
|
`;
|
|
101
101
|
expect((0, index_1.zodSchemaToMarkdown)(schema)).toBe(expected);
|
|
102
102
|
});
|
|
103
103
|
it('should convert ZodDate to markdown', () => {
|
|
104
104
|
const schema = zod_1.z.date();
|
|
105
|
-
const expected = `- Date
|
|
105
|
+
const expected = `- Date
|
|
106
106
|
`;
|
|
107
107
|
expect((0, index_1.zodSchemaToMarkdown)(schema)).toBe(expected);
|
|
108
108
|
});
|
|
109
109
|
it('should convert ZodNaN to markdown', () => {
|
|
110
110
|
const schema = zod_1.z.nan();
|
|
111
|
-
const expected = `- NaN
|
|
111
|
+
const expected = `- NaN
|
|
112
112
|
`;
|
|
113
113
|
expect((0, index_1.zodSchemaToMarkdown)(schema)).toBe(expected);
|
|
114
114
|
});
|
|
115
115
|
it('should convert ZodNever to markdown', () => {
|
|
116
116
|
const schema = zod_1.z.never();
|
|
117
|
-
const expected = `- Never
|
|
117
|
+
const expected = `- Never
|
|
118
118
|
`;
|
|
119
119
|
expect((0, index_1.zodSchemaToMarkdown)(schema)).toBe(expected);
|
|
120
120
|
});
|
|
121
121
|
it('should convert ZodUnknown to markdown', () => {
|
|
122
122
|
const schema = zod_1.z.unknown();
|
|
123
|
-
const expected = `- Unknown
|
|
123
|
+
const expected = `- Unknown
|
|
124
124
|
`;
|
|
125
125
|
expect((0, index_1.zodSchemaToMarkdown)(schema)).toBe(expected);
|
|
126
126
|
});
|
|
127
127
|
it('should convert ZodVoid to markdown', () => {
|
|
128
128
|
const schema = zod_1.z.void();
|
|
129
|
-
const expected = `- Void
|
|
129
|
+
const expected = `- Void
|
|
130
130
|
`;
|
|
131
131
|
expect((0, index_1.zodSchemaToMarkdown)(schema)).toBe(expected);
|
|
132
132
|
});
|
|
@@ -137,10 +137,10 @@ describe('zodSchemaToTable', () => {
|
|
|
137
137
|
name: zod_1.z.string(),
|
|
138
138
|
age: zod_1.z.number(),
|
|
139
139
|
});
|
|
140
|
-
const expected = `| 字段 | 类型 | 描述 |
|
|
141
|
-
|------|------|------|
|
|
142
|
-
| name | String | - |
|
|
143
|
-
| age | Number | - |
|
|
140
|
+
const expected = `| 字段 | 类型 | 描述 |
|
|
141
|
+
|------|------|------|
|
|
142
|
+
| name | String | - |
|
|
143
|
+
| age | Number | - |
|
|
144
144
|
`;
|
|
145
145
|
expect((0, index_1.zodSchemaToTable)(schema)).toBe(expected);
|
|
146
146
|
});
|
|
@@ -149,10 +149,10 @@ describe('zodSchemaToTable', () => {
|
|
|
149
149
|
name: zod_1.z.string().optional(),
|
|
150
150
|
email: zod_1.z.string().nullable(),
|
|
151
151
|
});
|
|
152
|
-
const expected = `| 字段 | 类型 | 描述 |
|
|
153
|
-
|------|------|------|
|
|
154
|
-
| name | Optional<String> | - |
|
|
155
|
-
| email | Nullable<String> | - |
|
|
152
|
+
const expected = `| 字段 | 类型 | 描述 |
|
|
153
|
+
|------|------|------|
|
|
154
|
+
| name | Optional<String> | - |
|
|
155
|
+
| email | Nullable<String> | - |
|
|
156
156
|
`;
|
|
157
157
|
expect((0, index_1.zodSchemaToTable)(schema)).toBe(expected);
|
|
158
158
|
});
|
|
@@ -160,9 +160,9 @@ describe('zodSchemaToTable', () => {
|
|
|
160
160
|
const schema = zod_1.z.object({
|
|
161
161
|
role: zod_1.z.enum(['admin', 'user', 'guest']),
|
|
162
162
|
});
|
|
163
|
-
const expected = `| 字段 | 类型 | 描述 |
|
|
164
|
-
|------|------|------|
|
|
165
|
-
| role | Enum(admin | user | guest) | - |
|
|
163
|
+
const expected = `| 字段 | 类型 | 描述 |
|
|
164
|
+
|------|------|------|
|
|
165
|
+
| role | Enum(admin | user | guest) | - |
|
|
166
166
|
`;
|
|
167
167
|
expect((0, index_1.zodSchemaToTable)(schema)).toBe(expected);
|
|
168
168
|
});
|
|
@@ -171,10 +171,10 @@ describe('zodSchemaToTable', () => {
|
|
|
171
171
|
tags: zod_1.z.array(zod_1.z.string()),
|
|
172
172
|
scores: zod_1.z.array(zod_1.z.number()),
|
|
173
173
|
});
|
|
174
|
-
const expected = `| 字段 | 类型 | 描述 |
|
|
175
|
-
|------|------|------|
|
|
176
|
-
| tags[] | Array<String> | - |
|
|
177
|
-
| scores[] | Array<Number> | - |
|
|
174
|
+
const expected = `| 字段 | 类型 | 描述 |
|
|
175
|
+
|------|------|------|
|
|
176
|
+
| tags[] | Array<String> | - |
|
|
177
|
+
| scores[] | Array<Number> | - |
|
|
178
178
|
`;
|
|
179
179
|
expect((0, index_1.zodSchemaToTable)(schema)).toBe(expected);
|
|
180
180
|
});
|
|
@@ -183,10 +183,10 @@ describe('zodSchemaToTable', () => {
|
|
|
183
183
|
name: zod_1.z.string().describe('用户名称'),
|
|
184
184
|
age: zod_1.z.number().describe('用户年龄'),
|
|
185
185
|
});
|
|
186
|
-
const expected = `| 字段 | 类型 | 描述 |
|
|
187
|
-
|------|------|------|
|
|
188
|
-
| name | String | 用户名称 |
|
|
189
|
-
| age | Number | 用户年龄 |
|
|
186
|
+
const expected = `| 字段 | 类型 | 描述 |
|
|
187
|
+
|------|------|------|
|
|
188
|
+
| name | String | 用户名称 |
|
|
189
|
+
| age | Number | 用户年龄 |
|
|
190
190
|
`;
|
|
191
191
|
expect((0, index_1.zodSchemaToTable)(schema)).toBe(expected);
|
|
192
192
|
});
|
|
@@ -195,16 +195,120 @@ describe('zodSchemaToTable', () => {
|
|
|
195
195
|
status: zod_1.z.literal('active'),
|
|
196
196
|
count: zod_1.z.literal(1),
|
|
197
197
|
});
|
|
198
|
-
const expected = `| 字段 | 类型 | 描述 |
|
|
199
|
-
|------|------|------|
|
|
200
|
-
| status | Literal("active") | - |
|
|
201
|
-
| count | Literal(1) | - |
|
|
198
|
+
const expected = `| 字段 | 类型 | 描述 |
|
|
199
|
+
|------|------|------|
|
|
200
|
+
| status | Literal("active") | - |
|
|
201
|
+
| count | Literal(1) | - |
|
|
202
202
|
`;
|
|
203
203
|
expect((0, index_1.zodSchemaToTable)(schema)).toBe(expected);
|
|
204
204
|
});
|
|
205
205
|
it('should fallback to markdown for non-object schema', () => {
|
|
206
206
|
const schema = zod_1.z.string();
|
|
207
|
-
const expected = `- String
|
|
207
|
+
const expected = `- String
|
|
208
|
+
`;
|
|
209
|
+
expect((0, index_1.zodSchemaToTable)(schema)).toBe(expected);
|
|
210
|
+
});
|
|
211
|
+
it('should convert object schema with union to table', () => {
|
|
212
|
+
const schema = zod_1.z.object({
|
|
213
|
+
type: zod_1.z.union([zod_1.z.string(), zod_1.z.number()]),
|
|
214
|
+
});
|
|
215
|
+
const expected = `| 字段 | 类型 | 描述 |
|
|
216
|
+
|------|------|------|
|
|
217
|
+
| type (option 1) | String | - |
|
|
218
|
+
| type (option 2) | Number | - |
|
|
219
|
+
`;
|
|
220
|
+
expect((0, index_1.zodSchemaToTable)(schema)).toBe(expected);
|
|
221
|
+
});
|
|
222
|
+
it('should convert object schema with union of objects to table', () => {
|
|
223
|
+
const schema = zod_1.z.object({
|
|
224
|
+
data: zod_1.z.union([
|
|
225
|
+
zod_1.z.object({ name: zod_1.z.string() }),
|
|
226
|
+
zod_1.z.object({ age: zod_1.z.number() }),
|
|
227
|
+
]),
|
|
228
|
+
});
|
|
229
|
+
const expected = `| 字段 | 类型 | 描述 |
|
|
230
|
+
|------|------|------|
|
|
231
|
+
| data (option 1): name | String | - |
|
|
232
|
+
| data (option 2): age | Number | - |
|
|
233
|
+
`;
|
|
234
|
+
expect((0, index_1.zodSchemaToTable)(schema)).toBe(expected);
|
|
235
|
+
});
|
|
236
|
+
it('should convert object schema with discriminated union to table', () => {
|
|
237
|
+
const schema = zod_1.z.object({
|
|
238
|
+
message: zod_1.z.discriminatedUnion('role', [
|
|
239
|
+
zod_1.z.object({ role: zod_1.z.literal('user'), name: zod_1.z.string() }),
|
|
240
|
+
zod_1.z.object({ role: zod_1.z.literal('admin'), level: zod_1.z.number() }),
|
|
241
|
+
]),
|
|
242
|
+
});
|
|
243
|
+
const expected = `| 字段 | 类型 | 描述 |
|
|
244
|
+
|------|------|------|
|
|
245
|
+
| message (option 1): role | Literal("user") | - |
|
|
246
|
+
| message (option 1): name | String | - |
|
|
247
|
+
| message (option 2): role | Literal("admin") | - |
|
|
248
|
+
| message (option 2): level | Number | - |
|
|
249
|
+
`;
|
|
250
|
+
expect((0, index_1.zodSchemaToTable)(schema)).toBe(expected);
|
|
251
|
+
});
|
|
252
|
+
it('should convert object schema with intersection to table', () => {
|
|
253
|
+
const schema = zod_1.z.object({
|
|
254
|
+
merged: zod_1.z.object({ a: zod_1.z.string() }).and(zod_1.z.object({ b: zod_1.z.number() })),
|
|
255
|
+
});
|
|
256
|
+
const expected = `| 字段 | 类型 | 描述 |
|
|
257
|
+
|------|------|------|
|
|
258
|
+
| merged: a (Left) | String | - |
|
|
259
|
+
| merged: b (Right) | Number | - |
|
|
260
|
+
`;
|
|
261
|
+
expect((0, index_1.zodSchemaToTable)(schema)).toBe(expected);
|
|
262
|
+
});
|
|
263
|
+
it('should convert object schema with optional of union to table', () => {
|
|
264
|
+
const schema = zod_1.z.object({
|
|
265
|
+
value: zod_1.z.string().optional(),
|
|
266
|
+
data: zod_1.z.union([zod_1.z.string(), zod_1.z.number()]).optional(),
|
|
267
|
+
});
|
|
268
|
+
const expected = `| 字段 | 类型 | 描述 |
|
|
269
|
+
|------|------|------|
|
|
270
|
+
| value | Optional<String> | - |
|
|
271
|
+
| data (option 1) | String | - |
|
|
272
|
+
| data (option 2) | Number | - |
|
|
273
|
+
`;
|
|
274
|
+
expect((0, index_1.zodSchemaToTable)(schema)).toBe(expected);
|
|
275
|
+
});
|
|
276
|
+
it('should convert object schema with array of union to table', () => {
|
|
277
|
+
const schema = zod_1.z.object({
|
|
278
|
+
items: zod_1.z.array(zod_1.z.union([zod_1.z.string(), zod_1.z.number()])),
|
|
279
|
+
});
|
|
280
|
+
const expected = `| 字段 | 类型 | 描述 |
|
|
281
|
+
|------|------|------|
|
|
282
|
+
| items[] (option 1) | String | - |
|
|
283
|
+
| items[] (option 2) | Number | - |
|
|
284
|
+
`;
|
|
285
|
+
expect((0, index_1.zodSchemaToTable)(schema)).toBe(expected);
|
|
286
|
+
});
|
|
287
|
+
it('should convert union with optional object to table', () => {
|
|
288
|
+
const schema = zod_1.z.object({
|
|
289
|
+
data: zod_1.z.union([
|
|
290
|
+
zod_1.z.object({ name: zod_1.z.string() }).optional(),
|
|
291
|
+
zod_1.z.object({ age: zod_1.z.number() }),
|
|
292
|
+
]),
|
|
293
|
+
});
|
|
294
|
+
const expected = `| 字段 | 类型 | 描述 |
|
|
295
|
+
|------|------|------|
|
|
296
|
+
| data (option 1): name | Optional<String> | - |
|
|
297
|
+
| data (option 2): age | Number | - |
|
|
298
|
+
`;
|
|
299
|
+
expect((0, index_1.zodSchemaToTable)(schema)).toBe(expected);
|
|
300
|
+
});
|
|
301
|
+
it('should convert array of union with objects to table', () => {
|
|
302
|
+
const schema = zod_1.z.object({
|
|
303
|
+
items: zod_1.z.array(zod_1.z.union([
|
|
304
|
+
zod_1.z.object({ a: zod_1.z.string() }),
|
|
305
|
+
zod_1.z.object({ b: zod_1.z.number() }),
|
|
306
|
+
])),
|
|
307
|
+
});
|
|
308
|
+
const expected = `| 字段 | 类型 | 描述 |
|
|
309
|
+
|------|------|------|
|
|
310
|
+
| items[].a (option 1) | String | - |
|
|
311
|
+
| items[].b (option 2) | Number | - |
|
|
208
312
|
`;
|
|
209
313
|
expect((0, index_1.zodSchemaToTable)(schema)).toBe(expected);
|
|
210
314
|
});
|