schema-dsl 2.3.0
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/.eslintignore +10 -0
- package/.eslintrc.json +27 -0
- package/.github/CODE_OF_CONDUCT.md +45 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +57 -0
- package/.github/ISSUE_TEMPLATE/config.yml +11 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +45 -0
- package/.github/ISSUE_TEMPLATE/question.md +31 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +70 -0
- package/.github/SECURITY.md +184 -0
- package/.github/workflows/ci.yml +35 -0
- package/CHANGELOG.md +633 -0
- package/CONTRIBUTING.md +368 -0
- package/LICENSE +21 -0
- package/README.md +1122 -0
- package/STATUS.md +273 -0
- package/docs/FEATURE-INDEX.md +521 -0
- package/docs/INDEX.md +224 -0
- package/docs/api-reference.md +1098 -0
- package/docs/best-practices.md +672 -0
- package/docs/cache-manager.md +336 -0
- package/docs/design-philosophy.md +602 -0
- package/docs/dsl-syntax.md +654 -0
- package/docs/dynamic-locale.md +552 -0
- package/docs/error-handling.md +703 -0
- package/docs/export-guide.md +459 -0
- package/docs/faq.md +576 -0
- package/docs/frontend-i18n-guide.md +290 -0
- package/docs/i18n-user-guide.md +488 -0
- package/docs/label-vs-description.md +262 -0
- package/docs/markdown-exporter.md +398 -0
- package/docs/mongodb-exporter.md +279 -0
- package/docs/multi-type-support.md +319 -0
- package/docs/mysql-exporter.md +257 -0
- package/docs/plugin-system.md +542 -0
- package/docs/postgresql-exporter.md +290 -0
- package/docs/quick-start.md +761 -0
- package/docs/schema-helper.md +340 -0
- package/docs/schema-utils.md +492 -0
- package/docs/string-extensions.md +480 -0
- package/docs/troubleshooting.md +471 -0
- package/docs/type-converter.md +319 -0
- package/docs/type-reference.md +219 -0
- package/docs/validate.md +486 -0
- package/docs/validation-guide.md +484 -0
- package/examples/array-dsl-example.js +227 -0
- package/examples/custom-extension.js +85 -0
- package/examples/dsl-match-example.js +74 -0
- package/examples/dsl-style.js +118 -0
- package/examples/dynamic-locale-configuration.js +348 -0
- package/examples/dynamic-locale-example.js +287 -0
- package/examples/export-demo.js +130 -0
- package/examples/i18n-full-demo.js +310 -0
- package/examples/i18n-memory-safety.examples.js +268 -0
- package/examples/markdown-export.js +71 -0
- package/examples/middleware-usage.js +93 -0
- package/examples/password-reset/README.md +153 -0
- package/examples/password-reset/schema.js +26 -0
- package/examples/password-reset/test.js +101 -0
- package/examples/plugin-system.examples.js +205 -0
- package/examples/simple-example.js +122 -0
- package/examples/string-extensions.js +297 -0
- package/examples/user-registration/README.md +156 -0
- package/examples/user-registration/routes.js +92 -0
- package/examples/user-registration/schema.js +150 -0
- package/examples/user-registration/server.js +74 -0
- package/index.d.ts +1999 -0
- package/index.js +270 -0
- package/index.mjs +30 -0
- package/lib/adapters/DslAdapter.js +653 -0
- package/lib/adapters/index.js +20 -0
- package/lib/config/constants.js +286 -0
- package/lib/config/patterns/creditCard.js +9 -0
- package/lib/config/patterns/idCard.js +9 -0
- package/lib/config/patterns/index.js +8 -0
- package/lib/config/patterns/licensePlate.js +4 -0
- package/lib/config/patterns/passport.js +4 -0
- package/lib/config/patterns/phone.js +9 -0
- package/lib/config/patterns/postalCode.js +5 -0
- package/lib/core/CacheManager.js +376 -0
- package/lib/core/DslBuilder.js +740 -0
- package/lib/core/ErrorCodes.js +233 -0
- package/lib/core/ErrorFormatter.js +342 -0
- package/lib/core/JSONSchemaCore.js +347 -0
- package/lib/core/Locale.js +119 -0
- package/lib/core/MessageTemplate.js +89 -0
- package/lib/core/PluginManager.js +448 -0
- package/lib/core/StringExtensions.js +209 -0
- package/lib/core/Validator.js +316 -0
- package/lib/exporters/MarkdownExporter.js +420 -0
- package/lib/exporters/MongoDBExporter.js +162 -0
- package/lib/exporters/MySQLExporter.js +212 -0
- package/lib/exporters/PostgreSQLExporter.js +289 -0
- package/lib/exporters/index.js +24 -0
- package/lib/locales/en-US.js +65 -0
- package/lib/locales/es-ES.js +66 -0
- package/lib/locales/fr-FR.js +66 -0
- package/lib/locales/index.js +8 -0
- package/lib/locales/ja-JP.js +66 -0
- package/lib/locales/zh-CN.js +93 -0
- package/lib/utils/LRUCache.js +174 -0
- package/lib/utils/SchemaHelper.js +240 -0
- package/lib/utils/SchemaUtils.js +313 -0
- package/lib/utils/TypeConverter.js +245 -0
- package/lib/utils/index.js +13 -0
- package/lib/validators/CustomKeywords.js +203 -0
- package/lib/validators/index.js +11 -0
- package/package.json +70 -0
- package/plugins/custom-format.js +101 -0
- package/plugins/custom-validator.js +200 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
# label vs description 使用指南
|
|
2
|
+
|
|
3
|
+
## 📋 快速对比
|
|
4
|
+
|
|
5
|
+
| 属性 | 用途 | 显示位置 | 示例 |
|
|
6
|
+
|------|------|----------|------|
|
|
7
|
+
| **label** | 字段名称 | 错误消息中 | "邮箱地址不能为空" |
|
|
8
|
+
| **description** | 字段说明 | 表单提示/文档 | "用于登录和接收通知" |
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## 🎯 详细说明
|
|
13
|
+
|
|
14
|
+
### label(标签)
|
|
15
|
+
|
|
16
|
+
**作用**: 在错误消息中替换字段名
|
|
17
|
+
|
|
18
|
+
**使用场景**:
|
|
19
|
+
- 让错误消息更友好
|
|
20
|
+
- 中文化字段名
|
|
21
|
+
- 简化技术字段名
|
|
22
|
+
|
|
23
|
+
**示例**:
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
// 不使用label
|
|
27
|
+
email: 'email!'
|
|
28
|
+
// 错误消息: "email is required" ❌ 不友好
|
|
29
|
+
|
|
30
|
+
// 使用label
|
|
31
|
+
email: 'email!'.label('邮箱地址')
|
|
32
|
+
// 错误消息: "邮箱地址不能为空" ✅ 友好
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
**完整示例**:
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
const schema = dsl({
|
|
39
|
+
userEmail: 'email!'
|
|
40
|
+
.label('用户邮箱')
|
|
41
|
+
.messages({
|
|
42
|
+
'required': '{{#label}}不能为空', // 使用label值
|
|
43
|
+
'format': '{{#label}}格式不正确'
|
|
44
|
+
})
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// 验证失败时:
|
|
48
|
+
// 错误: "用户邮箱不能为空"
|
|
49
|
+
// 错误: "用户邮箱格式不正确"
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
### description(描述)
|
|
55
|
+
|
|
56
|
+
**作用**: 提供字段的详细说明
|
|
57
|
+
|
|
58
|
+
**使用场景**:
|
|
59
|
+
- 表单输入提示
|
|
60
|
+
- API文档生成
|
|
61
|
+
- Schema文档
|
|
62
|
+
- 帮助用户理解字段用途
|
|
63
|
+
|
|
64
|
+
**示例**:
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
email: 'email!'
|
|
68
|
+
.label('邮箱地址')
|
|
69
|
+
.description('用于登录和接收系统通知')
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
**在表单中使用**:
|
|
73
|
+
|
|
74
|
+
```html
|
|
75
|
+
<div class="form-field">
|
|
76
|
+
<label>邮箱地址</label> <!-- 来自 label -->
|
|
77
|
+
<input type="email" />
|
|
78
|
+
<span class="hint">用于登录和接收系统通知</span> <!-- 来自 description -->
|
|
79
|
+
</div>
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
**在API文档中**:
|
|
83
|
+
|
|
84
|
+
```json
|
|
85
|
+
{
|
|
86
|
+
"email": {
|
|
87
|
+
"type": "string",
|
|
88
|
+
"format": "email",
|
|
89
|
+
"title": "邮箱地址", // 来自 label
|
|
90
|
+
"description": "用于登录和接收系统通知" // 来自 description
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## 💡 最佳实践
|
|
98
|
+
|
|
99
|
+
### 1. label 是必需的(用户可见字段)
|
|
100
|
+
|
|
101
|
+
```javascript
|
|
102
|
+
const schema = dsl({
|
|
103
|
+
// ✅ 好:所有用户可见字段都有label
|
|
104
|
+
username: 'string:3-32!'.label('用户名'),
|
|
105
|
+
email: 'email!'.label('邮箱地址'),
|
|
106
|
+
password: 'string:8-64!'.label('密码'),
|
|
107
|
+
|
|
108
|
+
// ⚠️ 可以:内部字段可以不用label
|
|
109
|
+
userId: 'uuid!',
|
|
110
|
+
createdAt: 'date!'
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### 2. description 是可选的(需要说明时使用)
|
|
115
|
+
|
|
116
|
+
```javascript
|
|
117
|
+
const schema = dsl({
|
|
118
|
+
// ✅ 复杂字段:添加description
|
|
119
|
+
apiKey: 'string:32!'
|
|
120
|
+
.label('API密钥')
|
|
121
|
+
.description('用于调用第三方API,请妥善保管'),
|
|
122
|
+
|
|
123
|
+
// ✅ 简单字段:不需要description
|
|
124
|
+
name: 'string:1-50!'.label('姓名'),
|
|
125
|
+
|
|
126
|
+
// ✅ 有特殊要求的字段:添加description
|
|
127
|
+
password: 'string:8-64!'
|
|
128
|
+
.label('密码')
|
|
129
|
+
.description('必须包含大小写字母和数字')
|
|
130
|
+
.pattern(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$/)
|
|
131
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### 3. 组合使用示例
|
|
135
|
+
|
|
136
|
+
```javascript
|
|
137
|
+
const userSchema = dsl({
|
|
138
|
+
// 完整的字段定义
|
|
139
|
+
email: 'email!'
|
|
140
|
+
.label('邮箱地址') // 错误消息中显示
|
|
141
|
+
.description('用于登录和接收通知') // 表单提示/文档
|
|
142
|
+
.messages({
|
|
143
|
+
'required': '{{#label}}不能为空',
|
|
144
|
+
'format': '请输入有效的{{#label}}'
|
|
145
|
+
}),
|
|
146
|
+
|
|
147
|
+
// 简单字段
|
|
148
|
+
age: 'number:18-120'.label('年龄'),
|
|
149
|
+
|
|
150
|
+
// 复杂字段
|
|
151
|
+
bio: 'string:500'
|
|
152
|
+
.label('个人简介')
|
|
153
|
+
.description('介绍你自己,最多500字'),
|
|
154
|
+
|
|
155
|
+
// 内部字段(无需label/description)
|
|
156
|
+
userId: 'uuid!',
|
|
157
|
+
createdAt: 'date!'
|
|
158
|
+
});
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
## 📊 使用场景对比
|
|
164
|
+
|
|
165
|
+
| 场景 | label | description |
|
|
166
|
+
|------|-------|-------------|
|
|
167
|
+
| **错误消息** | ✅ 必需 | ❌ 不使用 |
|
|
168
|
+
| **表单标签** | ✅ 推荐 | ⚠️ 可选 |
|
|
169
|
+
| **表单提示** | ❌ 不使用 | ✅ 推荐 |
|
|
170
|
+
| **API文档** | ✅ 作为title | ✅ 作为说明 |
|
|
171
|
+
| **Schema文档** | ✅ 字段名 | ✅ 字段说明 |
|
|
172
|
+
| **内部字段** | ⚠️ 可选 | ⚠️ 可选 |
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
## 🎨 实际效果
|
|
177
|
+
|
|
178
|
+
### 验证错误显示
|
|
179
|
+
|
|
180
|
+
```javascript
|
|
181
|
+
// Schema定义
|
|
182
|
+
const schema = dsl({
|
|
183
|
+
email: 'email!'
|
|
184
|
+
.label('邮箱地址')
|
|
185
|
+
.messages({
|
|
186
|
+
'required': '{{#label}}不能为空',
|
|
187
|
+
'format': '{{#label}}格式不正确'
|
|
188
|
+
})
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// 验证空值
|
|
192
|
+
validator.validate(schema, { email: '' });
|
|
193
|
+
// 错误: "邮箱地址不能为空" ← 使用了label
|
|
194
|
+
|
|
195
|
+
// 验证错误格式
|
|
196
|
+
validator.validate(schema, { email: 'invalid' });
|
|
197
|
+
// 错误: "邮箱地址格式不正确" ← 使用了label
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### 表单渲染
|
|
201
|
+
|
|
202
|
+
```javascript
|
|
203
|
+
const schema = dsl({
|
|
204
|
+
password: 'string:8-64!'
|
|
205
|
+
.label('登录密码')
|
|
206
|
+
.description('8-64位,包含大小写字母和数字')
|
|
207
|
+
.pattern(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$/)
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
// 渲染为HTML
|
|
211
|
+
<div class="form-field">
|
|
212
|
+
<label>登录密码</label> ← label
|
|
213
|
+
<input type="password" />
|
|
214
|
+
<span class="hint">8-64位,包含大小写字母和数字</span> ← description
|
|
215
|
+
</div>
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
## ✅ 总结
|
|
221
|
+
|
|
222
|
+
### label
|
|
223
|
+
|
|
224
|
+
- **必需性**: 用户可见字段推荐使用
|
|
225
|
+
- **用途**: 让错误消息更友好
|
|
226
|
+
- **位置**: 错误消息、表单标签
|
|
227
|
+
- **格式**: 简短的名词(2-6个字)
|
|
228
|
+
|
|
229
|
+
### description
|
|
230
|
+
|
|
231
|
+
- **必需性**: 可选,需要说明时使用
|
|
232
|
+
- **用途**: 帮助用户理解字段用途
|
|
233
|
+
- **位置**: 表单提示、API文档
|
|
234
|
+
- **格式**: 完整的句子或短语
|
|
235
|
+
|
|
236
|
+
### 推荐组合
|
|
237
|
+
|
|
238
|
+
```javascript
|
|
239
|
+
// 最小配置(简单字段)
|
|
240
|
+
name: 'string:1-50!'.label('姓名')
|
|
241
|
+
|
|
242
|
+
// 标准配置(常规字段)
|
|
243
|
+
email: 'email!'
|
|
244
|
+
.label('邮箱地址')
|
|
245
|
+
.messages({ 'format': '请输入有效的{{#label}}' })
|
|
246
|
+
|
|
247
|
+
// 完整配置(复杂字段)
|
|
248
|
+
apiKey: 'string:32!'
|
|
249
|
+
.label('API密钥')
|
|
250
|
+
.description('用于调用第三方API,请妥善保管')
|
|
251
|
+
.pattern(/^[A-Za-z0-9]{32}$/)
|
|
252
|
+
.messages({
|
|
253
|
+
'required': '{{#label}}不能为空',
|
|
254
|
+
'pattern': '{{#label}}格式不正确'
|
|
255
|
+
})
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
---
|
|
259
|
+
|
|
260
|
+
**记住**: label用于错误消息,description用于帮助说明!
|
|
261
|
+
|
|
262
|
+
|
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
# Markdown 导出器
|
|
2
|
+
|
|
3
|
+
> **功能**: 将 JSON Schema 导出为人类可读的 Markdown 文档
|
|
4
|
+
> **版本**: v2.2.0
|
|
5
|
+
> **语言支持**: 中文、英文、日文
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 📑 目录
|
|
10
|
+
|
|
11
|
+
- [快速开始](#快速开始)
|
|
12
|
+
- [API 参考](#api-参考)
|
|
13
|
+
- [使用示例](#使用示例)
|
|
14
|
+
- [多语言支持](#多语言支持)
|
|
15
|
+
- [自定义选项](#自定义选项)
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## 快速开始
|
|
20
|
+
|
|
21
|
+
### 安装
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install schema-dsl
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### 基本用法
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
const { dsl, exporters } = require('schema-dsl');
|
|
31
|
+
|
|
32
|
+
// 定义 Schema
|
|
33
|
+
const schema = dsl({
|
|
34
|
+
username: 'string:3-32!',
|
|
35
|
+
email: 'email!',
|
|
36
|
+
age: 'number:18-120'
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// 导出为 Markdown
|
|
40
|
+
const markdown = exporters.MarkdownExporter.export(schema, {
|
|
41
|
+
title: '用户注册 API',
|
|
42
|
+
locale: 'zh-CN'
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
console.log(markdown);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
**生成的 Markdown**:
|
|
49
|
+
|
|
50
|
+
```markdown
|
|
51
|
+
# 用户注册 API
|
|
52
|
+
|
|
53
|
+
## 字段列表
|
|
54
|
+
|
|
55
|
+
| 字段名 | 类型 | 必填 | 约束 | 说明 |
|
|
56
|
+
|--------|------|------|------|------|
|
|
57
|
+
| username | 字符串 | ✅ | 长度: 3-32 | - |
|
|
58
|
+
| email | 邮箱 | ✅ | - | - |
|
|
59
|
+
| age | 数字 | ❌ | 范围: 18-120 | - |
|
|
60
|
+
|
|
61
|
+
## 示例数据
|
|
62
|
+
|
|
63
|
+
\```json
|
|
64
|
+
{
|
|
65
|
+
"username": "example",
|
|
66
|
+
"email": "user@example.com"
|
|
67
|
+
}
|
|
68
|
+
\```
|
|
69
|
+
|
|
70
|
+
## 约束规则
|
|
71
|
+
|
|
72
|
+
**必填字段**: `username`, `email`
|
|
73
|
+
|
|
74
|
+
**可选字段**: `age`
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## API 参考
|
|
80
|
+
|
|
81
|
+
### MarkdownExporter.export(schema, options)
|
|
82
|
+
|
|
83
|
+
导出 JSON Schema 为 Markdown 文档。
|
|
84
|
+
|
|
85
|
+
**参数**:
|
|
86
|
+
|
|
87
|
+
- `schema` (Object) - JSON Schema 对象
|
|
88
|
+
- `options` (Object) - 导出选项
|
|
89
|
+
- `title` (String) - 文档标题,默认: `'Schema 文档'`
|
|
90
|
+
- `locale` (String) - 语言代码,默认: `'zh-CN'`
|
|
91
|
+
- 支持: `'zh-CN'` (中文), `'en-US'` (英文), `'ja-JP'` (日文)
|
|
92
|
+
- `includeExample` (Boolean) - 是否包含示例数据,默认: `true`
|
|
93
|
+
- `includeDescription` (Boolean) - 是否包含描述,默认: `true`
|
|
94
|
+
|
|
95
|
+
**返回值**: `String` - Markdown 文本
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## 使用示例
|
|
100
|
+
|
|
101
|
+
### 示例 1: 基础用法
|
|
102
|
+
|
|
103
|
+
```javascript
|
|
104
|
+
const schema = dsl({
|
|
105
|
+
name: 'string:1-50!',
|
|
106
|
+
email: 'email!'
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const markdown = exporters.MarkdownExporter.export(schema, {
|
|
110
|
+
title: '用户信息'
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
console.log(markdown);
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### 示例 2: 使用标签
|
|
117
|
+
|
|
118
|
+
```javascript
|
|
119
|
+
const schema = dsl({
|
|
120
|
+
name: 'string:1-50!'.label('姓名'),
|
|
121
|
+
email: 'email!'.label('邮箱地址'),
|
|
122
|
+
age: 'number:18-120'.label('年龄')
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const markdown = exporters.MarkdownExporter.export(schema, {
|
|
126
|
+
title: '用户注册表单',
|
|
127
|
+
locale: 'zh-CN'
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
**生成结果**:
|
|
132
|
+
|
|
133
|
+
| 字段名 | 类型 | 必填 | 约束 | 说明 |
|
|
134
|
+
|--------|------|------|------|------|
|
|
135
|
+
| name | 字符串 | ✅ | 长度: 1-50 | 姓名 |
|
|
136
|
+
| email | 邮箱 | ✅ | - | 邮箱地址 |
|
|
137
|
+
| age | 数字 | ❌ | 范围: 18-120 | 年龄 |
|
|
138
|
+
|
|
139
|
+
### 示例 3: 复杂 Schema
|
|
140
|
+
|
|
141
|
+
```javascript
|
|
142
|
+
const productSchema = dsl({
|
|
143
|
+
'id': 'string:24!'.label('产品ID'),
|
|
144
|
+
'name': 'string:1-100!'.label('产品名称'),
|
|
145
|
+
'price': 'number:0.01-!'.label('价格 (USD)'),
|
|
146
|
+
'stock': 'integer:0-!'.label('库存数量'),
|
|
147
|
+
'category': 'electronics|clothing|books|other!'.label('类别'),
|
|
148
|
+
'tags': 'array:1-10<string:1-20>'.label('标签'),
|
|
149
|
+
'description': 'string:500'.label('产品描述'),
|
|
150
|
+
'active': 'boolean'.label('是否上架')
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
const markdown = exporters.MarkdownExporter.export(productSchema, {
|
|
154
|
+
title: '产品信息 Schema',
|
|
155
|
+
locale: 'zh-CN',
|
|
156
|
+
includeExample: true
|
|
157
|
+
});
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### 示例 4: 不包含示例
|
|
161
|
+
|
|
162
|
+
```javascript
|
|
163
|
+
const markdown = exporters.MarkdownExporter.export(schema, {
|
|
164
|
+
title: 'API 文档',
|
|
165
|
+
includeExample: false // 不生成示例数据
|
|
166
|
+
});
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## 多语言支持
|
|
172
|
+
|
|
173
|
+
### 支持的语言
|
|
174
|
+
|
|
175
|
+
| 语言代码 | 语言名称 | 示例 |
|
|
176
|
+
|---------|---------|------|
|
|
177
|
+
| `zh-CN` | 简体中文 | 字符串、数字、必填 |
|
|
178
|
+
| `en-US` | 英文 | String, Number, Required |
|
|
179
|
+
| `ja-JP` | 日文 | 文字列、数値、必須 |
|
|
180
|
+
|
|
181
|
+
### 中文示例
|
|
182
|
+
|
|
183
|
+
```javascript
|
|
184
|
+
const markdown = exporters.MarkdownExporter.export(schema, {
|
|
185
|
+
title: '用户注册 API',
|
|
186
|
+
locale: 'zh-CN'
|
|
187
|
+
});
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
**输出**:
|
|
191
|
+
- 字段列表 (Fields)
|
|
192
|
+
- 类型: 字符串、数字、布尔值
|
|
193
|
+
- 必填: ✅ / ❌
|
|
194
|
+
|
|
195
|
+
### 英文示例
|
|
196
|
+
|
|
197
|
+
```javascript
|
|
198
|
+
const markdown = exporters.MarkdownExporter.export(schema, {
|
|
199
|
+
title: 'User Registration API',
|
|
200
|
+
locale: 'en-US'
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
**输出**:
|
|
205
|
+
- Fields
|
|
206
|
+
- Type: String, Number, Boolean
|
|
207
|
+
- Required: ✅ / ❌
|
|
208
|
+
|
|
209
|
+
### 日文示例
|
|
210
|
+
|
|
211
|
+
```javascript
|
|
212
|
+
const markdown = exporters.MarkdownExporter.export(schema, {
|
|
213
|
+
title: 'ユーザー登録 API',
|
|
214
|
+
locale: 'ja-JP'
|
|
215
|
+
});
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
**输出**:
|
|
219
|
+
- フィールド一覧
|
|
220
|
+
- タイプ: 文字列、数値、ブール値
|
|
221
|
+
- 必須: ✅ / ❌
|
|
222
|
+
|
|
223
|
+
---
|
|
224
|
+
|
|
225
|
+
## 自定义选项
|
|
226
|
+
|
|
227
|
+
### 完整配置示例
|
|
228
|
+
|
|
229
|
+
```javascript
|
|
230
|
+
const markdown = exporters.MarkdownExporter.export(schema, {
|
|
231
|
+
// 文档标题
|
|
232
|
+
title: 'API 文档 - 用户模块',
|
|
233
|
+
|
|
234
|
+
// 语言设置
|
|
235
|
+
locale: 'zh-CN',
|
|
236
|
+
|
|
237
|
+
// 是否包含示例数据
|
|
238
|
+
includeExample: true,
|
|
239
|
+
|
|
240
|
+
// 是否包含 Schema 描述
|
|
241
|
+
includeDescription: true
|
|
242
|
+
});
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### 保存为文件
|
|
246
|
+
|
|
247
|
+
```javascript
|
|
248
|
+
const fs = require('fs');
|
|
249
|
+
|
|
250
|
+
const markdown = exporters.MarkdownExporter.export(schema, {
|
|
251
|
+
title: 'API Documentation',
|
|
252
|
+
locale: 'en-US'
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// 保存为 Markdown 文件
|
|
256
|
+
fs.writeFileSync('./API.md', markdown, 'utf-8');
|
|
257
|
+
console.log('Markdown 文档已生成: API.md');
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
## 类型映射表
|
|
263
|
+
|
|
264
|
+
### 基本类型
|
|
265
|
+
|
|
266
|
+
| Schema 类型 | 中文 | 英文 | 日文 |
|
|
267
|
+
|------------|------|------|------|
|
|
268
|
+
| string | 字符串 | String | 文字列 |
|
|
269
|
+
| number | 数字 | Number | 数値 |
|
|
270
|
+
| integer | 整数 | Integer | 整数 |
|
|
271
|
+
| boolean | 布尔值 | Boolean | ブール値 |
|
|
272
|
+
| array | 数组 | Array | 配列 |
|
|
273
|
+
| object | 对象 | Object | オブジェクト |
|
|
274
|
+
|
|
275
|
+
### 格式类型
|
|
276
|
+
|
|
277
|
+
| 格式 | 中文 | 英文 | 日文 |
|
|
278
|
+
|------|------|------|------|
|
|
279
|
+
| email | 邮箱 | Email | メールアドレス |
|
|
280
|
+
| url | 网址 | URL | URL |
|
|
281
|
+
| date | 日期 | Date | 日付 |
|
|
282
|
+
| uuid | UUID | UUID | UUID |
|
|
283
|
+
|
|
284
|
+
---
|
|
285
|
+
|
|
286
|
+
## 高级用法
|
|
287
|
+
|
|
288
|
+
### 与其他导出器结合
|
|
289
|
+
|
|
290
|
+
```javascript
|
|
291
|
+
const { dsl, exporters } = require('schema-dsl');
|
|
292
|
+
|
|
293
|
+
const schema = dsl({
|
|
294
|
+
username: 'string:3-32!',
|
|
295
|
+
email: 'email!'
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
// 导出为 Markdown (人类可读)
|
|
299
|
+
const markdown = exporters.MarkdownExporter.export(schema, {
|
|
300
|
+
title: '用户 Schema',
|
|
301
|
+
locale: 'zh-CN'
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
// 导出为 MongoDB Schema
|
|
305
|
+
const mongoSchema = exporters.MongoDBExporter.export(schema, {
|
|
306
|
+
collectionName: 'users'
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
// 导出为 MySQL DDL
|
|
310
|
+
const mysqlDDL = exporters.MySQLExporter.export(schema, {
|
|
311
|
+
tableName: 'users'
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
console.log('Markdown 文档:\n', markdown);
|
|
315
|
+
console.log('\nMongoDB Schema:\n', mongoSchema);
|
|
316
|
+
console.log('\nMySQL DDL:\n', mysqlDDL);
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
---
|
|
320
|
+
|
|
321
|
+
## 常见问题
|
|
322
|
+
|
|
323
|
+
### Q: 如何自定义字段说明?
|
|
324
|
+
|
|
325
|
+
A: 使用 `.label()` 方法:
|
|
326
|
+
|
|
327
|
+
```javascript
|
|
328
|
+
const schema = dsl({
|
|
329
|
+
email: 'email!'.label('用户邮箱地址')
|
|
330
|
+
});
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
### Q: 生成的示例数据是什么?
|
|
334
|
+
|
|
335
|
+
A: 示例数据包含所有**必填字段**,值根据类型和约束自动生成:
|
|
336
|
+
- 邮箱: `user@example.com`
|
|
337
|
+
- 数字: 使用 `minimum` 或 `0`
|
|
338
|
+
- 字符串: `'example'`
|
|
339
|
+
- 布尔值: `true`
|
|
340
|
+
|
|
341
|
+
### Q: 如何隐藏某些字段?
|
|
342
|
+
|
|
343
|
+
A: Markdown 导出器会导出所有字段。如需隐藏,请在生成前移除字段。
|
|
344
|
+
|
|
345
|
+
### Q: 支持嵌套对象吗?
|
|
346
|
+
|
|
347
|
+
A: 当前版本主要支持扁平结构。嵌套对象会显示为 "对象" 类型。
|
|
348
|
+
|
|
349
|
+
---
|
|
350
|
+
|
|
351
|
+
## 完整示例
|
|
352
|
+
|
|
353
|
+
```javascript
|
|
354
|
+
const { dsl, exporters } = require('schema-dsl');
|
|
355
|
+
const fs = require('fs');
|
|
356
|
+
|
|
357
|
+
// 定义用户注册 Schema
|
|
358
|
+
const userRegistrationSchema = dsl({
|
|
359
|
+
// 基本信息
|
|
360
|
+
'username': 'string:3-32!'.label('用户名'),
|
|
361
|
+
'email': 'email!'.label('邮箱地址'),
|
|
362
|
+
'password': 'string:8-32!'.label('密码'),
|
|
363
|
+
|
|
364
|
+
// 个人信息
|
|
365
|
+
'realName': 'string:1-50'.label('真实姓名'),
|
|
366
|
+
'age': 'integer:18-120'.label('年龄'),
|
|
367
|
+
'gender': 'male|female|other'.label('性别'),
|
|
368
|
+
|
|
369
|
+
// 其他
|
|
370
|
+
'acceptTerms': 'boolean!'.label('同意条款')
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
// 生成中文文档
|
|
374
|
+
const zhDoc = exporters.MarkdownExporter.export(userRegistrationSchema, {
|
|
375
|
+
title: '用户注册 API 文档',
|
|
376
|
+
locale: 'zh-CN',
|
|
377
|
+
includeExample: true
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
// 生成英文文档
|
|
381
|
+
const enDoc = exporters.MarkdownExporter.export(userRegistrationSchema, {
|
|
382
|
+
title: 'User Registration API Documentation',
|
|
383
|
+
locale: 'en-US',
|
|
384
|
+
includeExample: true
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
// 保存文档
|
|
388
|
+
fs.writeFileSync('./docs/USER_REGISTRATION_ZH.md', zhDoc);
|
|
389
|
+
fs.writeFileSync('./docs/USER_REGISTRATION_EN.md', enDoc);
|
|
390
|
+
|
|
391
|
+
console.log('✅ 文档已生成');
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
---
|
|
395
|
+
|
|
396
|
+
**文档更新日期**: 2025-12-29
|
|
397
|
+
**版本**: v2.2.0
|
|
398
|
+
|