@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
|
@@ -1,152 +1,152 @@
|
|
|
1
|
-
# Zod to Markdown - Complex Types Examples
|
|
2
|
-
|
|
3
|
-
This document demonstrates the `zodSchemaToMarkdown` function's support for complex Zod types used in OpenAI API schemas.
|
|
4
|
-
|
|
5
|
-
## ZodEffects (Transform)
|
|
6
|
-
|
|
7
|
-
```typescript
|
|
8
|
-
const schema = z.string().transform(val => val.length);
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
Output:
|
|
12
|
-
```markdown
|
|
13
|
-
- Effects (transform)
|
|
14
|
-
- String
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
## ZodDiscriminatedUnion
|
|
18
|
-
|
|
19
|
-
```typescript
|
|
20
|
-
const schema = z.discriminatedUnion('type', [
|
|
21
|
-
z.object({ type: z.literal('a'), a: z.string() }),
|
|
22
|
-
z.object({ type: z.literal('b'), b: z.number() })
|
|
23
|
-
]);
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
Output:
|
|
27
|
-
```markdown
|
|
28
|
-
- DiscriminatedUnion (key: type)
|
|
29
|
-
- type
|
|
30
|
-
- Literal: "a"
|
|
31
|
-
- a
|
|
32
|
-
- String
|
|
33
|
-
|
|
|
34
|
-
- type
|
|
35
|
-
- Literal: "b"
|
|
36
|
-
- b
|
|
37
|
-
- Number
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
## ZodIntersection
|
|
41
|
-
|
|
42
|
-
```typescript
|
|
43
|
-
const schema = z.object({ a: z.string() }).and(z.object({ b: z.number() }));
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
Output:
|
|
47
|
-
```markdown
|
|
48
|
-
- Intersection
|
|
49
|
-
Left:
|
|
50
|
-
- a
|
|
51
|
-
- String
|
|
52
|
-
Right:
|
|
53
|
-
- b
|
|
54
|
-
- Number
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
## ZodRecord
|
|
58
|
-
|
|
59
|
-
```typescript
|
|
60
|
-
const schema = z.record(z.string(), z.number());
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
Output:
|
|
64
|
-
```markdown
|
|
65
|
-
- Record
|
|
66
|
-
Key:
|
|
67
|
-
- String
|
|
68
|
-
Value:
|
|
69
|
-
- Number
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
## ZodTuple
|
|
73
|
-
|
|
74
|
-
```typescript
|
|
75
|
-
const schema = z.tuple([z.string(), z.number()]);
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
Output:
|
|
79
|
-
```markdown
|
|
80
|
-
- Tuple
|
|
81
|
-
[0]:
|
|
82
|
-
- String
|
|
83
|
-
[1]:
|
|
84
|
-
- Number
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
## ZodLiteral
|
|
88
|
-
|
|
89
|
-
```typescript
|
|
90
|
-
const schema = z.literal('hello');
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
Output:
|
|
94
|
-
```markdown
|
|
95
|
-
- Literal: "hello"
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
## Primitive Types
|
|
99
|
-
|
|
100
|
-
### ZodBigInt
|
|
101
|
-
```typescript
|
|
102
|
-
z.bigint()
|
|
103
|
-
```
|
|
104
|
-
Output:
|
|
105
|
-
```markdown
|
|
106
|
-
- BigInt
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
### ZodDate
|
|
110
|
-
```typescript
|
|
111
|
-
z.date()
|
|
112
|
-
```
|
|
113
|
-
Output:
|
|
114
|
-
```markdown
|
|
115
|
-
- Date
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
### ZodNaN
|
|
119
|
-
```typescript
|
|
120
|
-
z.nan()
|
|
121
|
-
```
|
|
122
|
-
Output:
|
|
123
|
-
```markdown
|
|
124
|
-
- NaN
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
### ZodNever
|
|
128
|
-
```typescript
|
|
129
|
-
z.never()
|
|
130
|
-
```
|
|
131
|
-
Output:
|
|
132
|
-
```markdown
|
|
133
|
-
- Never
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
### ZodUnknown
|
|
137
|
-
```typescript
|
|
138
|
-
z.unknown()
|
|
139
|
-
```
|
|
140
|
-
Output:
|
|
141
|
-
```markdown
|
|
142
|
-
- Unknown
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
### ZodVoid
|
|
146
|
-
```typescript
|
|
147
|
-
z.void()
|
|
148
|
-
```
|
|
149
|
-
Output:
|
|
150
|
-
```markdown
|
|
151
|
-
- Void
|
|
152
|
-
```
|
|
1
|
+
# Zod to Markdown - Complex Types Examples
|
|
2
|
+
|
|
3
|
+
This document demonstrates the `zodSchemaToMarkdown` function's support for complex Zod types used in OpenAI API schemas.
|
|
4
|
+
|
|
5
|
+
## ZodEffects (Transform)
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
const schema = z.string().transform(val => val.length);
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Output:
|
|
12
|
+
```markdown
|
|
13
|
+
- Effects (transform)
|
|
14
|
+
- String
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## ZodDiscriminatedUnion
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
const schema = z.discriminatedUnion('type', [
|
|
21
|
+
z.object({ type: z.literal('a'), a: z.string() }),
|
|
22
|
+
z.object({ type: z.literal('b'), b: z.number() })
|
|
23
|
+
]);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Output:
|
|
27
|
+
```markdown
|
|
28
|
+
- DiscriminatedUnion (key: type)
|
|
29
|
+
- type
|
|
30
|
+
- Literal: "a"
|
|
31
|
+
- a
|
|
32
|
+
- String
|
|
33
|
+
|
|
|
34
|
+
- type
|
|
35
|
+
- Literal: "b"
|
|
36
|
+
- b
|
|
37
|
+
- Number
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## ZodIntersection
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
const schema = z.object({ a: z.string() }).and(z.object({ b: z.number() }));
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Output:
|
|
47
|
+
```markdown
|
|
48
|
+
- Intersection
|
|
49
|
+
Left:
|
|
50
|
+
- a
|
|
51
|
+
- String
|
|
52
|
+
Right:
|
|
53
|
+
- b
|
|
54
|
+
- Number
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## ZodRecord
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
const schema = z.record(z.string(), z.number());
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Output:
|
|
64
|
+
```markdown
|
|
65
|
+
- Record
|
|
66
|
+
Key:
|
|
67
|
+
- String
|
|
68
|
+
Value:
|
|
69
|
+
- Number
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## ZodTuple
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
const schema = z.tuple([z.string(), z.number()]);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Output:
|
|
79
|
+
```markdown
|
|
80
|
+
- Tuple
|
|
81
|
+
[0]:
|
|
82
|
+
- String
|
|
83
|
+
[1]:
|
|
84
|
+
- Number
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## ZodLiteral
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
const schema = z.literal('hello');
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Output:
|
|
94
|
+
```markdown
|
|
95
|
+
- Literal: "hello"
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Primitive Types
|
|
99
|
+
|
|
100
|
+
### ZodBigInt
|
|
101
|
+
```typescript
|
|
102
|
+
z.bigint()
|
|
103
|
+
```
|
|
104
|
+
Output:
|
|
105
|
+
```markdown
|
|
106
|
+
- BigInt
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### ZodDate
|
|
110
|
+
```typescript
|
|
111
|
+
z.date()
|
|
112
|
+
```
|
|
113
|
+
Output:
|
|
114
|
+
```markdown
|
|
115
|
+
- Date
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### ZodNaN
|
|
119
|
+
```typescript
|
|
120
|
+
z.nan()
|
|
121
|
+
```
|
|
122
|
+
Output:
|
|
123
|
+
```markdown
|
|
124
|
+
- NaN
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### ZodNever
|
|
128
|
+
```typescript
|
|
129
|
+
z.never()
|
|
130
|
+
```
|
|
131
|
+
Output:
|
|
132
|
+
```markdown
|
|
133
|
+
- Never
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### ZodUnknown
|
|
137
|
+
```typescript
|
|
138
|
+
z.unknown()
|
|
139
|
+
```
|
|
140
|
+
Output:
|
|
141
|
+
```markdown
|
|
142
|
+
- Unknown
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### ZodVoid
|
|
146
|
+
```typescript
|
|
147
|
+
z.void()
|
|
148
|
+
```
|
|
149
|
+
Output:
|
|
150
|
+
```markdown
|
|
151
|
+
- Void
|
|
152
|
+
```
|
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { zodSchemaToTable } from '../src/index';
|
|
3
|
-
import * as fs from 'fs';
|
|
4
|
-
|
|
5
|
-
// 纯对象嵌套示例
|
|
6
|
-
const coordinatesSchema = z.object({
|
|
7
|
-
lat: z.number().describe('纬度'),
|
|
8
|
-
lng: z.number().describe('经度'),
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
const addressSchema = z.object({
|
|
12
|
-
street: z.string().describe('街道'),
|
|
13
|
-
city: z.string().describe('城市'),
|
|
14
|
-
country: z.string().describe('国家'),
|
|
15
|
-
coordinates: coordinatesSchema.describe('坐标'),
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
const userSchema = z.object({
|
|
19
|
-
id: z.string().uuid().describe('用户ID'),
|
|
20
|
-
name: z.string().describe('姓名'),
|
|
21
|
-
profile: z.object({
|
|
22
|
-
bio: z.string().describe('个人简介'),
|
|
23
|
-
avatar: z.string().describe('头像URL'),
|
|
24
|
-
age: z.number().describe('年龄'),
|
|
25
|
-
}).describe('用户资料'),
|
|
26
|
-
settings: z.object({
|
|
27
|
-
theme: z.enum(['light', 'dark']).describe('主题'),
|
|
28
|
-
language: z.string().describe('语言'),
|
|
29
|
-
notifications: z.object({
|
|
30
|
-
email: z.boolean().describe('邮件通知'),
|
|
31
|
-
push: z.boolean().describe('推送通知'),
|
|
32
|
-
}).describe('通知设置'),
|
|
33
|
-
}).describe('设置'),
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
const markdown = zodSchemaToTable(userSchema);
|
|
37
|
-
|
|
38
|
-
console.log('## User Schema (Object 嵌套)\n');
|
|
39
|
-
console.log(markdown);
|
|
40
|
-
|
|
41
|
-
fs.writeFileSync('examples/nested-object-example.md', markdown);
|
|
42
|
-
console.log('已保存到 examples/nested-object-example.md');
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { zodSchemaToTable } from '../src/index';
|
|
3
|
+
import * as fs from 'fs';
|
|
4
|
+
|
|
5
|
+
// 纯对象嵌套示例
|
|
6
|
+
const coordinatesSchema = z.object({
|
|
7
|
+
lat: z.number().describe('纬度'),
|
|
8
|
+
lng: z.number().describe('经度'),
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const addressSchema = z.object({
|
|
12
|
+
street: z.string().describe('街道'),
|
|
13
|
+
city: z.string().describe('城市'),
|
|
14
|
+
country: z.string().describe('国家'),
|
|
15
|
+
coordinates: coordinatesSchema.describe('坐标'),
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const userSchema = z.object({
|
|
19
|
+
id: z.string().uuid().describe('用户ID'),
|
|
20
|
+
name: z.string().describe('姓名'),
|
|
21
|
+
profile: z.object({
|
|
22
|
+
bio: z.string().describe('个人简介'),
|
|
23
|
+
avatar: z.string().describe('头像URL'),
|
|
24
|
+
age: z.number().describe('年龄'),
|
|
25
|
+
}).describe('用户资料'),
|
|
26
|
+
settings: z.object({
|
|
27
|
+
theme: z.enum(['light', 'dark']).describe('主题'),
|
|
28
|
+
language: z.string().describe('语言'),
|
|
29
|
+
notifications: z.object({
|
|
30
|
+
email: z.boolean().describe('邮件通知'),
|
|
31
|
+
push: z.boolean().describe('推送通知'),
|
|
32
|
+
}).describe('通知设置'),
|
|
33
|
+
}).describe('设置'),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const markdown = zodSchemaToTable(userSchema);
|
|
37
|
+
|
|
38
|
+
console.log('## User Schema (Object 嵌套)\n');
|
|
39
|
+
console.log(markdown);
|
|
40
|
+
|
|
41
|
+
fs.writeFileSync('examples/nested-object-example.md', markdown);
|
|
42
|
+
console.log('已保存到 examples/nested-object-example.md');
|