schema-dsl 2.0.0 → 2.0.1
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/CHANGELOG.md +130 -113
- package/LICENSE +21 -21
- package/README.md +628 -628
- package/dist/{DslBuilder-DkLaOo9Q.d.ts → DslBuilder-BIgQOAXp.d.ts} +2 -0
- package/dist/{DslBuilder-DQDN0ZxZ.d.cts → DslBuilder-CjHTucNQ.d.cts} +2 -0
- package/dist/{Validator-hFWKGxir.d.ts → Validator-CllRdrY0.d.ts} +1 -1
- package/dist/{Validator-C7GsVQOH.d.cts → Validator-D6okG9tr.d.cts} +1 -1
- package/dist/index.cjs +75 -29
- package/dist/index.d.cts +10 -4
- package/dist/index.d.ts +10 -4
- package/dist/index.js +75 -29
- package/dist/plugins/custom-format.cjs +33 -17
- package/dist/plugins/custom-format.d.cts +1 -1
- package/dist/plugins/custom-format.d.ts +1 -1
- package/dist/plugins/custom-format.js +33 -17
- package/dist/plugins/custom-type-example.cjs +33 -17
- package/dist/plugins/custom-type-example.d.cts +1 -1
- package/dist/plugins/custom-type-example.d.ts +1 -1
- package/dist/plugins/custom-type-example.js +33 -17
- package/dist/plugins/custom-validator.cjs +0 -2
- package/dist/plugins/custom-validator.d.cts +1 -1
- package/dist/plugins/custom-validator.d.ts +1 -1
- package/dist/plugins/custom-validator.js +0 -2
- package/docs/FEATURE-INDEX.md +553 -553
- package/docs/add-custom-locale.md +496 -496
- package/docs/add-keyword.md +24 -24
- package/docs/api-reference.md +1047 -1047
- package/docs/api.md +13 -13
- package/docs/best-practices-project-structure.md +417 -417
- package/docs/best-practices.md +712 -712
- package/docs/cache-manager.md +344 -344
- package/docs/compile.md +45 -45
- package/docs/conditional-api.md +1307 -1307
- package/docs/custom-extensions-guide.md +339 -339
- package/docs/design-philosophy.md +606 -606
- package/docs/doc-index.md +324 -324
- package/docs/dsl-syntax.md +714 -714
- package/docs/dynamic-locale.md +608 -608
- package/docs/enum.md +482 -482
- package/docs/error-handling.md +1975 -1975
- package/docs/export-guide.md +501 -501
- package/docs/export-limitations.md +567 -567
- package/docs/faq.md +596 -596
- package/docs/frontend-i18n-guide.md +307 -307
- package/docs/i18n-user-guide.md +487 -487
- package/docs/i18n.md +476 -476
- package/docs/index.md +48 -48
- package/docs/json-schema-basics.md +40 -40
- package/docs/label-vs-description.md +271 -271
- package/docs/markdown-exporter.md +406 -406
- package/docs/mongodb-exporter.md +302 -302
- package/docs/multi-language.md +26 -26
- package/docs/multi-type-support.md +322 -322
- package/docs/mysql-exporter.md +280 -280
- package/docs/number-operators.md +449 -449
- package/docs/optional-marker-guide.md +326 -326
- package/docs/performance-guide.md +49 -49
- package/docs/plugin-system.md +381 -381
- package/docs/plugin-type-registration.md +34 -34
- package/docs/postgresql-exporter.md +311 -311
- package/docs/public/favicon.svg +4 -4
- package/docs/quick-start.md +435 -435
- package/docs/runtime-locale-support.md +532 -532
- package/docs/schema-helper.md +345 -345
- package/docs/schema-utils-advanced-issues.md +23 -23
- package/docs/schema-utils-best-practices.md +20 -20
- package/docs/schema-utils-chaining.md +150 -150
- package/docs/schema-utils.md +524 -524
- package/docs/security-checklist.md +20 -20
- package/docs/string-extensions.md +488 -488
- package/docs/troubleshooting.md +486 -486
- package/docs/type-converter.md +310 -310
- package/docs/type-reference.md +242 -242
- package/docs/typescript-guide.md +584 -584
- package/docs/union-type-guide.md +157 -157
- package/docs/union-types.md +284 -284
- package/docs/validate-async.md +491 -491
- package/docs/validate-batch.md +49 -49
- package/docs/validate-dsl-object-support.md +578 -578
- package/docs/validate.md +506 -506
- package/docs/validation-guide.md +502 -502
- package/docs/validator.md +39 -39
- package/package.json +131 -131
- package/plugins/custom-format.cjs +8 -8
- package/plugins/custom-type-example.cjs +8 -8
- package/plugins/custom-validator.cjs +8 -8
- package/src/adapters/DslAdapter.ts +111 -111
- package/src/adapters/index.ts +1 -1
- package/src/config/constants.ts +83 -83
- package/src/config/index.ts +2 -2
- package/src/config/patterns.ts +77 -77
- package/src/core/CacheManager.ts +169 -159
- package/src/core/ConditionalBuilder.ts +382 -382
- package/src/core/ConditionalRuntime.ts +27 -27
- package/src/core/ConditionalValidator.ts +254 -254
- package/src/core/DslBuilder.ts +687 -677
- package/src/core/ErrorCodes.ts +38 -38
- package/src/core/ErrorFormatter.ts +271 -271
- package/src/core/JSONSchemaCore.ts +65 -65
- package/src/core/Locale.ts +187 -187
- package/src/core/MessageTemplate.ts +42 -42
- package/src/core/ObjectDslBuilder.ts +64 -64
- package/src/core/PluginManager.ts +326 -326
- package/src/core/StringExtensions.ts +140 -140
- package/src/core/TemplateEngine.ts +44 -44
- package/src/core/Validator.ts +448 -448
- package/src/errors/I18nError.ts +159 -159
- package/src/errors/ValidationError.ts +105 -105
- package/src/exporters/BaseExporter.ts +60 -60
- package/src/exporters/MarkdownExporter.ts +305 -305
- package/src/exporters/MongoDBExporter.ts +126 -126
- package/src/exporters/MySQLExporter.ts +156 -155
- package/src/exporters/PostgreSQLExporter.ts +222 -222
- package/src/exporters/index.ts +18 -18
- package/src/index.ts +651 -633
- package/src/locales/en-US.ts +160 -160
- package/src/locales/es-ES.ts +160 -160
- package/src/locales/fr-FR.ts +160 -160
- package/src/locales/index.ts +103 -103
- package/src/locales/ja-JP.ts +160 -160
- package/src/locales/types.ts +156 -156
- package/src/locales/zh-CN.ts +160 -160
- package/src/parser/ConstraintParser.ts +101 -101
- package/src/parser/DslParser.ts +470 -470
- package/src/parser/SchemaCompiler.ts +66 -66
- package/src/parser/TypeRegistry.ts +250 -250
- package/src/parser/index.ts +6 -6
- package/src/plugins/custom-format.ts +124 -126
- package/src/plugins/custom-type-example.ts +106 -108
- package/src/plugins/custom-validator.ts +138 -140
- package/src/types/conditional.ts +28 -28
- package/src/types/config.ts +59 -59
- package/src/types/dsl.ts +131 -131
- package/src/types/error.ts +60 -60
- package/src/types/index.ts +17 -17
- package/src/types/infer.ts +127 -127
- package/src/types/plugin.ts +58 -58
- package/src/types/safe-regex.d.ts +9 -9
- package/src/types/schema.ts +66 -66
- package/src/types/validate.ts +71 -71
- package/src/utils/SchemaHelper.ts +196 -196
- package/src/utils/SchemaUtils.ts +365 -346
- package/src/utils/TypeConverter.ts +215 -215
- package/src/utils/index.ts +10 -10
- package/src/validators/CustomKeywords.ts +477 -477
package/docs/FEATURE-INDEX.md
CHANGED
|
@@ -1,553 +1,553 @@
|
|
|
1
|
-
# schema-dsl 功能索引
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
> **更新时间**: 2026-04-30
|
|
5
|
-
> **用途**: 快速查找所有功能及其文档位置
|
|
6
|
-
|
|
7
|
-
---
|
|
8
|
-
|
|
9
|
-
## 📑 目录
|
|
10
|
-
|
|
11
|
-
- [核心API](#核心api)
|
|
12
|
-
- [验证功能](#验证功能)
|
|
13
|
-
- [导出器](#导出器)
|
|
14
|
-
- [工具函数](#工具函数)
|
|
15
|
-
- [错误处理](#错误处理)
|
|
16
|
-
- [配置管理](#配置管理)
|
|
17
|
-
- [示例代码](#示例代码)
|
|
18
|
-
|
|
19
|
-
---
|
|
20
|
-
|
|
21
|
-
## 核心API
|
|
22
|
-
|
|
23
|
-
### dsl() 函数
|
|
24
|
-
|
|
25
|
-
**功能**: DSL主入口,支持字符串和对象定义
|
|
26
|
-
|
|
27
|
-
**使用示例**:
|
|
28
|
-
```javascript
|
|
29
|
-
const { dsl } = require('schema-dsl');
|
|
30
|
-
|
|
31
|
-
// 字符串定义
|
|
32
|
-
const builder = dsl('email!');
|
|
33
|
-
|
|
34
|
-
// 对象定义
|
|
35
|
-
const schema = dsl({
|
|
36
|
-
username: 'string:3-32!',
|
|
37
|
-
email: 'email!'
|
|
38
|
-
});
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
**文档位置**:
|
|
42
|
-
- 📖 [API参考 - dsl()函数](./api-reference.md#dsl-函数)
|
|
43
|
-
- 📖 [快速开始](./quick-start.md)
|
|
44
|
-
- 📖 [DSL语法指南](./dsl-syntax.md)
|
|
45
|
-
|
|
46
|
-
**代码位置**: `src/index.ts` / `src/adapters/DslAdapter.ts`
|
|
47
|
-
|
|
48
|
-
---
|
|
49
|
-
|
|
50
|
-
### DslBuilder 类
|
|
51
|
-
|
|
52
|
-
**功能**: Schema构建器,支持链式调用
|
|
53
|
-
|
|
54
|
-
**可用方法**:
|
|
55
|
-
- ✅ `pattern(regex, message?)` - 正则验证
|
|
56
|
-
- ✅ `label(text)` - 字段标签
|
|
57
|
-
- ✅ `messages(obj)` - 自定义错误消息
|
|
58
|
-
- ✅ `description(text)` - 字段描述
|
|
59
|
-
- ✅ `custom(fn)` - 自定义验证器
|
|
60
|
-
- ✅ `when(field, opts)` - 条件验证
|
|
61
|
-
- ✅ `default(value)` - 默认值
|
|
62
|
-
- ✅ `toSchema()` - 转为JSON Schema(含内部标记字段)
|
|
63
|
-
- ✅ `toJsonSchema()` - 转为纯净 JSON Schema(自动清理 `_required`/`_customMessages` 等内部标记,适用于 OpenAPI / 外部系统)🆕 v1.2.5
|
|
64
|
-
- ✅ `validate(data)` - 验证数据
|
|
65
|
-
- ✅ `validateNestingDepth(schema, maxDepth)` - 检测嵌套深度(静态方法)
|
|
66
|
-
|
|
67
|
-
**默认验证器方法**:
|
|
68
|
-
- ✅ `username(preset?)` - 用户名验证(preset: 'short'|'medium'|'long'|'5-20')
|
|
69
|
-
- ✅ `password(strength?)` - 密码强度验证(strength: 'weak'|'medium'|'strong'|'veryStrong')
|
|
70
|
-
- ✅ `phone(country?)` - 手机号验证(country: 'cn'|'us'|'uk'|'hk'|'tw'|'international')
|
|
71
|
-
|
|
72
|
-
**使用示例**:
|
|
73
|
-
```javascript
|
|
74
|
-
// 基础链式调用
|
|
75
|
-
const schema = dsl('string:3-32!')
|
|
76
|
-
.pattern(/^[a-zA-Z0-9_]+$/)
|
|
77
|
-
.label('用户名')
|
|
78
|
-
.messages({ 'pattern': '只能包含字母、数字和下划线' });
|
|
79
|
-
|
|
80
|
-
// 使用默认验证器
|
|
81
|
-
const userSchema = dsl({
|
|
82
|
-
username: dsl('string!').username(), // 自动设置3-32长度+正则
|
|
83
|
-
password: dsl('string!').password('strong'), // 强密码验证
|
|
84
|
-
phone: dsl('string!').phone('cn') // 中国手机号验证
|
|
85
|
-
});
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
**文档位置**:
|
|
89
|
-
- 📖 [API参考 - DslBuilder类](./api-reference.md#dslbuilder-类)
|
|
90
|
-
- 📖 [String扩展文档](./string-extensions.md)
|
|
91
|
-
|
|
92
|
-
**代码位置**: `src/core/DslBuilder.ts`
|
|
93
|
-
|
|
94
|
-
---
|
|
95
|
-
|
|
96
|
-
### String 扩展
|
|
97
|
-
|
|
98
|
-
**功能**: 字符串直接链式调用,无需 dsl() 包裹
|
|
99
|
-
|
|
100
|
-
**可用方法**: 与 DslBuilder 相同
|
|
101
|
-
|
|
102
|
-
**使用示例**:
|
|
103
|
-
```javascript
|
|
104
|
-
const schema = dsl({
|
|
105
|
-
email: 'email!'.pattern(/custom/).label('邮箱'),
|
|
106
|
-
username: 'string:3-32!'.pattern(/^\w+$/).label('用户名')
|
|
107
|
-
});
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
**文档位置**:
|
|
111
|
-
- 📖 [String扩展完整文档](./string-extensions.md)
|
|
112
|
-
- 📖 [README](https://github.com/vextjs/schema-dsl/blob/main/README.md)
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
**代码位置**: `src/core/StringExtensions.ts`
|
|
116
|
-
|
|
117
|
-
---
|
|
118
|
-
|
|
119
|
-
## 验证功能
|
|
120
|
-
|
|
121
|
-
### Validator 类
|
|
122
|
-
|
|
123
|
-
**功能**: JSON Schema验证器(基于ajv)
|
|
124
|
-
|
|
125
|
-
**可用方法**:
|
|
126
|
-
- ✅ `validate(schema, data, options)` - 验证数据
|
|
127
|
-
- ✅ `compile(schema, cacheKey)` - 编译Schema
|
|
128
|
-
- ✅ `validateBatch(schema, dataArray)` - 批量验证
|
|
129
|
-
- ✅ `addKeyword(name, definition)` - 添加自定义关键字
|
|
130
|
-
- ✅ `addFormat(name, validator)` - 添加自定义格式
|
|
131
|
-
- ✅ `clearCache()` - 清空缓存
|
|
132
|
-
- ✅ `Validator.create(options)` - 创建实例(静态方法)
|
|
133
|
-
- ✅ `Validator.quickValidate(schema, data)` - 快速验证(静态方法)
|
|
134
|
-
|
|
135
|
-
**使用示例**:
|
|
136
|
-
```javascript
|
|
137
|
-
const { Validator } = require('schema-dsl');
|
|
138
|
-
|
|
139
|
-
const validator = new Validator();
|
|
140
|
-
const result = validator.validate(schema, data);
|
|
141
|
-
|
|
142
|
-
console.log(result.valid); // true/false
|
|
143
|
-
console.log(result.errors); // 错误列表
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
**文档位置**:
|
|
147
|
-
- 📖 [API参考 - Validator类](./api-reference.md#validator-类)
|
|
148
|
-
- 📖 [validate方法详解](./validate.md)
|
|
149
|
-
- 📖 [快速开始](./quick-start.md)
|
|
150
|
-
|
|
151
|
-
**代码位置**: `src/core/Validator.ts`
|
|
152
|
-
|
|
153
|
-
---
|
|
154
|
-
|
|
155
|
-
### validate() 便捷函数
|
|
156
|
-
|
|
157
|
-
**功能**: 单例验证,无需 new Validator()
|
|
158
|
-
|
|
159
|
-
**使用示例**:
|
|
160
|
-
```javascript
|
|
161
|
-
const { dsl, validate } = require('schema-dsl');
|
|
162
|
-
|
|
163
|
-
const schema = dsl({ email: 'email!' });
|
|
164
|
-
const result = validate(schema, { email: 'test@example.com' });
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
**文档位置**:
|
|
168
|
-
- 📖 [API参考 - validate()函数](./api-reference.md)
|
|
169
|
-
- 📖 [快速开始](./quick-start.md#1-hello-world30秒)
|
|
170
|
-
|
|
171
|
-
**代码位置**: `src/index.ts`(默认单例导出)
|
|
172
|
-
|
|
173
|
-
---
|
|
174
|
-
|
|
175
|
-
## 导出器
|
|
176
|
-
|
|
177
|
-
### MongoDBExporter
|
|
178
|
-
|
|
179
|
-
**功能**: 导出MongoDB $jsonSchema格式
|
|
180
|
-
|
|
181
|
-
**可用方法**:
|
|
182
|
-
- ✅ `export(schema)` - 导出Schema
|
|
183
|
-
- ✅ `generateCreateCommand(collectionName, schema)` - 生成createCollection命令
|
|
184
|
-
- ✅ `generateCommand(collectionName, schema)` - 生成可执行命令字符串
|
|
185
|
-
- ✅ `MongoDBExporter.export(schema)` - 快速导出(静态方法)
|
|
186
|
-
|
|
187
|
-
**使用示例**:
|
|
188
|
-
```javascript
|
|
189
|
-
const { exporters } = require('schema-dsl');
|
|
190
|
-
|
|
191
|
-
const exporter = new exporters.MongoDBExporter();
|
|
192
|
-
const mongoSchema = exporter.export(jsonSchema);
|
|
193
|
-
|
|
194
|
-
// 生成命令
|
|
195
|
-
const command = exporter.generateCommand('users', jsonSchema);
|
|
196
|
-
console.log(command);
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
**文档位置**:
|
|
200
|
-
- 📖 [数据库导出指南](./export-guide.md)
|
|
201
|
-
- 📖 [示例代码](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/feature-index.ts)
|
|
202
|
-
|
|
203
|
-
**代码位置**: `src/exporters/MongoDBExporter.ts`
|
|
204
|
-
|
|
205
|
-
---
|
|
206
|
-
|
|
207
|
-
### MySQLExporter
|
|
208
|
-
|
|
209
|
-
**功能**: 导出MySQL CREATE TABLE DDL
|
|
210
|
-
|
|
211
|
-
**可用方法**:
|
|
212
|
-
- ✅ `export(tableName, schema, options)` - 导出DDL
|
|
213
|
-
- ✅ `MySQLExporter.export(tableName, schema)` - 快速导出(静态方法)
|
|
214
|
-
|
|
215
|
-
**使用示例**:
|
|
216
|
-
```javascript
|
|
217
|
-
const { exporters } = require('schema-dsl');
|
|
218
|
-
|
|
219
|
-
const exporter = new exporters.MySQLExporter();
|
|
220
|
-
const ddl = exporter.export('users', jsonSchema);
|
|
221
|
-
|
|
222
|
-
console.log(ddl);
|
|
223
|
-
// CREATE TABLE `users` (
|
|
224
|
-
// `username` VARCHAR(32) NOT NULL,
|
|
225
|
-
// ...
|
|
226
|
-
// );
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
**文档位置**:
|
|
230
|
-
- 📖 [数据库导出指南](./export-guide.md)
|
|
231
|
-
- 📖 [示例代码](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/feature-index.ts)
|
|
232
|
-
|
|
233
|
-
**代码位置**: `src/exporters/MySQLExporter.ts`
|
|
234
|
-
|
|
235
|
-
---
|
|
236
|
-
|
|
237
|
-
### PostgreSQLExporter
|
|
238
|
-
|
|
239
|
-
**功能**: 导出PostgreSQL CREATE TABLE DDL
|
|
240
|
-
|
|
241
|
-
**可用方法**:
|
|
242
|
-
- ✅ `export(tableName, schema, options)` - 导出DDL
|
|
243
|
-
- ✅ `PostgreSQLExporter.export(tableName, schema)` - 快速导出(静态方法)
|
|
244
|
-
|
|
245
|
-
**使用示例**:
|
|
246
|
-
```javascript
|
|
247
|
-
const { exporters } = require('schema-dsl');
|
|
248
|
-
|
|
249
|
-
const exporter = new exporters.PostgreSQLExporter();
|
|
250
|
-
const ddl = exporter.export('users', jsonSchema);
|
|
251
|
-
|
|
252
|
-
console.log(ddl);
|
|
253
|
-
// CREATE TABLE public.users (
|
|
254
|
-
// username VARCHAR(32) NOT NULL,
|
|
255
|
-
// ...
|
|
256
|
-
// );
|
|
257
|
-
```
|
|
258
|
-
|
|
259
|
-
**文档位置**:
|
|
260
|
-
- 📖 [数据库导出指南](./export-guide.md)
|
|
261
|
-
- 📖 [示例代码](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/feature-index.ts)
|
|
262
|
-
|
|
263
|
-
**代码位置**: `src/exporters/PostgreSQLExporter.ts`
|
|
264
|
-
|
|
265
|
-
---
|
|
266
|
-
|
|
267
|
-
## 工具函数
|
|
268
|
-
|
|
269
|
-
### SchemaUtils
|
|
270
|
-
|
|
271
|
-
**功能**: Schema复用、合并、操作工具
|
|
272
|
-
|
|
273
|
-
**可用方法**:
|
|
274
|
-
- ✅ `reusable(factory)` - 创建可复用片段
|
|
275
|
-
- ✅ `createLibrary(fragments)` - 创建片段库
|
|
276
|
-
- ✅ `extend(baseSchema, extensions)` - 扩展Schema
|
|
277
|
-
- ✅ `pick(schema, fields)` - 筛选字段
|
|
278
|
-
- ✅ `omit(schema, fields)` - 排除字段
|
|
279
|
-
- ✅ `partial(schema, fields?)` - 将字段改为可选
|
|
280
|
-
- ✅ `validateBatch(schema, dataArray, ajvInstance)` - 带汇总统计的批量验证
|
|
281
|
-
- ✅ `withPerformance(validator)` - 为 Validator 包装性能信息
|
|
282
|
-
- ✅ `toMarkdown(schema)` - 导出为Markdown
|
|
283
|
-
- ✅ `toHTML(schema)` - 导出为HTML
|
|
284
|
-
- ✅ `clone(schema)` - 深度克隆
|
|
285
|
-
|
|
286
|
-
**使用示例**:
|
|
287
|
-
```javascript
|
|
288
|
-
const { SchemaUtils, dsl } = require('schema-dsl');
|
|
289
|
-
|
|
290
|
-
// Schema复用
|
|
291
|
-
const emailField = SchemaUtils.reusable(() => dsl('email!'));
|
|
292
|
-
|
|
293
|
-
const schema1 = dsl({ email: emailField() });
|
|
294
|
-
const schema2 = dsl({ contactEmail: emailField() });
|
|
295
|
-
|
|
296
|
-
// Schema合并
|
|
297
|
-
const extended = SchemaUtils.extend(schema1, { age: 'number' });
|
|
298
|
-
```
|
|
299
|
-
|
|
300
|
-
**文档位置**:
|
|
301
|
-
- 📖 [API参考 - SchemaUtils](./api-reference.md#工具函数)
|
|
302
|
-
|
|
303
|
-
**代码位置**: `src/utils/SchemaUtils.ts`
|
|
304
|
-
|
|
305
|
-
---
|
|
306
|
-
|
|
307
|
-
### TypeConverter
|
|
308
|
-
|
|
309
|
-
**功能**: 类型转换工具(JSON Schema ↔ 数据库类型)
|
|
310
|
-
|
|
311
|
-
**可用方法**:
|
|
312
|
-
- ✅ `toJSONSchemaType(nativeType)` - 转为 JSON Schema `type` 字符串
|
|
313
|
-
- ✅ `toMongoDBType(jsonSchemaType)` - 转为 MongoDB BSON 类型
|
|
314
|
-
- ✅ `toMySQLType(jsonSchemaType, schema?)` - 转为 MySQL 数据类型
|
|
315
|
-
- ✅ `toPostgreSQLType(jsonSchemaType, schema?)` - 转为 PostgreSQL 数据类型
|
|
316
|
-
- ✅ `normalizePropertyName(name)` - 规范化属性名
|
|
317
|
-
- ✅ `formatToRegex(format)` - 格式验证转正则
|
|
318
|
-
|
|
319
|
-
**文档位置**:
|
|
320
|
-
- 📖 [TypeConverter 文档](./type-converter.md)
|
|
321
|
-
|
|
322
|
-
**代码位置**: `src/utils/TypeConverter.ts`
|
|
323
|
-
|
|
324
|
-
---
|
|
325
|
-
|
|
326
|
-
### SchemaHelper
|
|
327
|
-
|
|
328
|
-
**功能**: Schema分析和辅助工具
|
|
329
|
-
|
|
330
|
-
**可用方法**:
|
|
331
|
-
- ✅ `isValidSchema(schema)` - 验证 Schema 有效性
|
|
332
|
-
- ✅ `generateSchemaId(schema)` - 生成基于内容的 Schema ID
|
|
333
|
-
- ✅ `getFieldPaths(schema)` - 提取字段路径
|
|
334
|
-
- ✅ `flattenSchema(schema)` - 扁平化 Schema
|
|
335
|
-
- ✅ `cloneSchema(schema)` - 克隆 Schema
|
|
336
|
-
- ✅ `extractRequiredFields(schema)` - 提取 required 字段
|
|
337
|
-
- ✅ `compareSchemas(schema1, schema2)` - 比较 Schema
|
|
338
|
-
- ✅ `simplifySchema(schema)` - 精简 Schema
|
|
339
|
-
- ✅ `isValidPropertyName(name)` - 校验属性名
|
|
340
|
-
- ✅ `getSchemaComplexity(schema)` - 评估复杂度
|
|
341
|
-
- ✅ `summarizeSchema(schema)` - 生成摘要
|
|
342
|
-
|
|
343
|
-
**文档位置**:
|
|
344
|
-
- 📖 [SchemaHelper 文档](./schema-helper.md)
|
|
345
|
-
|
|
346
|
-
**代码位置**: `src/utils/SchemaHelper.ts`
|
|
347
|
-
|
|
348
|
-
---
|
|
349
|
-
|
|
350
|
-
## 错误处理
|
|
351
|
-
|
|
352
|
-
### ErrorFormatter
|
|
353
|
-
|
|
354
|
-
**功能**: 格式化验证错误信息
|
|
355
|
-
|
|
356
|
-
**可用方法**:
|
|
357
|
-
- ✅ `new ErrorFormatter(locale?, messages?)` - 创建格式化器
|
|
358
|
-
- ✅ `format(error, locale?)` - 格式化单个错误为消息字符串
|
|
359
|
-
- ✅ `formatDetailed(errors, locale?, customMessages?, alreadyMerged?)` - 格式化错误数组为标准错误项
|
|
360
|
-
|
|
361
|
-
**文档位置**:
|
|
362
|
-
- 📖 [API参考 - ErrorFormatter / MessageTemplate / 底层解析工具](./api-reference.md)
|
|
363
|
-
- 📖 [错误处理文档](./error-handling.md)
|
|
364
|
-
|
|
365
|
-
**代码位置**: `src/core/ErrorFormatter.ts`
|
|
366
|
-
|
|
367
|
-
---
|
|
368
|
-
|
|
369
|
-
### ErrorCodes
|
|
370
|
-
|
|
371
|
-
**功能**: 错误码定义
|
|
372
|
-
|
|
373
|
-
**代码位置**: `src/core/ErrorCodes.ts`
|
|
374
|
-
|
|
375
|
-
---
|
|
376
|
-
|
|
377
|
-
### MessageTemplate
|
|
378
|
-
|
|
379
|
-
**功能**: 错误消息模板
|
|
380
|
-
|
|
381
|
-
**可用方法**:
|
|
382
|
-
- ✅ `render(template, vars)` - 渲染模板
|
|
383
|
-
- ✅ `MessageTemplate.render(template, vars)` - 快速渲染(静态方法)
|
|
384
|
-
- ✅ `MessageTemplate.renderBatch(templates, vars)` - 批量渲染(静态方法)
|
|
385
|
-
|
|
386
|
-
**文档位置**:
|
|
387
|
-
- 📖 [API参考 - MessageTemplate](./api-reference.md#messagetemplate)
|
|
388
|
-
|
|
389
|
-
**代码位置**: `src/core/MessageTemplate.ts`
|
|
390
|
-
|
|
391
|
-
---
|
|
392
|
-
|
|
393
|
-
### 类型注册与模板工具
|
|
394
|
-
|
|
395
|
-
**功能**: 面向进阶集成的模板渲染、JSON Schema 外观与自定义类型注册能力
|
|
396
|
-
|
|
397
|
-
**可用导出**:
|
|
398
|
-
- ✅ `renderTemplate(template, params)`
|
|
399
|
-
- ✅ `JSONSchemaCore`
|
|
400
|
-
- ✅ `TypeRegistry`
|
|
401
|
-
|
|
402
|
-
**文档位置**:
|
|
403
|
-
- 📖 [API参考 - renderTemplate / JSONSchemaCore / 类型注册与内部解析边界](./api-reference.md)
|
|
404
|
-
|
|
405
|
-
**代码位置**: `src/core/TemplateEngine.ts` / `src/core/JSONSchemaCore.ts` / `src/parser/TypeRegistry.ts`
|
|
406
|
-
|
|
407
|
-
---
|
|
408
|
-
|
|
409
|
-
### Locale
|
|
410
|
-
|
|
411
|
-
**功能**: 国际化支持
|
|
412
|
-
|
|
413
|
-
**可用方法**:
|
|
414
|
-
- ✅ `setLocale(locale)` - 设置语言
|
|
415
|
-
- ✅ `getLocale()` - 获取当前语言
|
|
416
|
-
- ✅ `addLocale(locale, messages)` - 添加语言包
|
|
417
|
-
- ✅ `setMessages(messages)` - 设置全局消息
|
|
418
|
-
- ✅ `getMessage(code, customMessages)` - 获取消息
|
|
419
|
-
- ✅ `getAvailableLocales()` - 获取可用语言
|
|
420
|
-
- ✅ `reset()` - 重置
|
|
421
|
-
|
|
422
|
-
**支持语言**:
|
|
423
|
-
- ✅ en-US(英语)
|
|
424
|
-
- ✅ zh-CN(中文)
|
|
425
|
-
- ✅ ja-JP(日语)
|
|
426
|
-
- ✅ es-ES(西班牙语)
|
|
427
|
-
- ✅ fr-FR(法语)
|
|
428
|
-
|
|
429
|
-
**文档位置**:
|
|
430
|
-
- 📖 [API参考 - Locale](./api-reference.md)
|
|
431
|
-
|
|
432
|
-
**代码位置**: `src/core/Locale.ts`
|
|
433
|
-
|
|
434
|
-
---
|
|
435
|
-
|
|
436
|
-
## 配置管理
|
|
437
|
-
|
|
438
|
-
### CacheManager
|
|
439
|
-
|
|
440
|
-
**功能**: Schema编译缓存管理
|
|
441
|
-
|
|
442
|
-
**可用方法**:
|
|
443
|
-
- ✅ `get(key)` - 获取缓存
|
|
444
|
-
- ✅ `set(key, value)` - 设置缓存
|
|
445
|
-
- ✅ `has(key)` - 检查缓存
|
|
446
|
-
- ✅ `delete(key)` - 删除缓存
|
|
447
|
-
- ✅ `clear()` - 清空缓存
|
|
448
|
-
- ✅ `size()` - 缓存大小
|
|
449
|
-
|
|
450
|
-
**文档位置**:
|
|
451
|
-
- 📖 [CacheManager 文档](./cache-manager.md)
|
|
452
|
-
|
|
453
|
-
**代码位置**: `src/core/CacheManager.ts`
|
|
454
|
-
|
|
455
|
-
---
|
|
456
|
-
|
|
457
|
-
### CustomKeywords
|
|
458
|
-
|
|
459
|
-
**功能**: 自定义验证关键字
|
|
460
|
-
|
|
461
|
-
**可用关键字**:
|
|
462
|
-
- ✅ `regex` - 正则验证
|
|
463
|
-
- ✅ `validate` - 自定义函数验证
|
|
464
|
-
|
|
465
|
-
**使用示例**:
|
|
466
|
-
```javascript
|
|
467
|
-
const { Validator, CustomKeywords } = require('schema-dsl');
|
|
468
|
-
|
|
469
|
-
const validator = new Validator();
|
|
470
|
-
CustomKeywords.registerAll(validator.getAjv());
|
|
471
|
-
|
|
472
|
-
const schema = {
|
|
473
|
-
type: 'string',
|
|
474
|
-
regex: '^[a-z]+$'
|
|
475
|
-
};
|
|
476
|
-
```
|
|
477
|
-
|
|
478
|
-
**文档位置**:
|
|
479
|
-
- 📖 [错误处理文档](./error-handling.md)
|
|
480
|
-
|
|
481
|
-
**代码位置**: `src/validators/CustomKeywords.ts`
|
|
482
|
-
|
|
483
|
-
---
|
|
484
|
-
|
|
485
|
-
## 示例代码
|
|
486
|
-
|
|
487
|
-
### 完整示例目录
|
|
488
|
-
|
|
489
|
-
**基础示例**:
|
|
490
|
-
- 📄 [dsl-syntax.ts](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/dsl-syntax.ts) - DSL基础用法
|
|
491
|
-
- 📄 [string-extensions.ts](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/string-extensions.ts) - String扩展示例
|
|
492
|
-
|
|
493
|
-
**场景示例**:
|
|
494
|
-
- 📄 [quick-start.ts](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/quick-start.ts) - 用户注册式的基础表单验证起点
|
|
495
|
-
- 📄 [validation-guide.ts](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/validation-guide.ts) - 失败路径、错误处理与规则组合示例
|
|
496
|
-
|
|
497
|
-
**导出示例**:
|
|
498
|
-
- 📄 [feature-index.ts](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/feature-index.ts) - 功能索引代表性示例
|
|
499
|
-
|
|
500
|
-
---
|
|
501
|
-
|
|
502
|
-
## 功能覆盖检查
|
|
503
|
-
|
|
504
|
-
### ✅ 已完整文档化
|
|
505
|
-
|
|
506
|
-
1. ✅ DSL语法 - `docs/dsl-syntax.md` (2815行)
|
|
507
|
-
2. ✅ String扩展 - `docs/string-extensions.md`
|
|
508
|
-
3. ✅ Validator类 - `docs/validate.md`
|
|
509
|
-
4. ✅ API参考 - `docs/api-reference.md`
|
|
510
|
-
5. ✅ 快速开始 - `docs/quick-start.md`
|
|
511
|
-
6. ✅ 数据库导出 - `README.md` + `docs/export-guide.md`
|
|
512
|
-
7. ✅ 自定义验证 - `README.md`
|
|
513
|
-
8. ✅ Schema工具 - `docs/schema-utils.md` + `docs/schema-helper.md`
|
|
514
|
-
|
|
515
|
-
### ⚠️ 文档需要补充
|
|
516
|
-
|
|
517
|
-
1. ⚠️ ErrorFormatter - 当前已在 API 参考与错误处理文档中覆盖;如需更聚焦入口,可后续增补专项文档
|
|
518
|
-
2. ⚠️ PluginManager - 可补一份更聚焦的 API/Hook 速查
|
|
519
|
-
3. ⚠️ 性能与基准测试 - 可继续补充独立诊断手册
|
|
520
|
-
4. ⚠️ 示例运行方式 - 可补充统一的 TypeScript 示例编译说明
|
|
521
|
-
5. ⚠️ 错误处理 - 可继续补充更完整的框架集成案例
|
|
522
|
-
|
|
523
|
-
### 📝 计划补充
|
|
524
|
-
|
|
525
|
-
- [ ] 增补 `ErrorFormatter` 专项文档
|
|
526
|
-
- [ ] 增补 Plugin Hook 速查文档
|
|
527
|
-
- [ ] 增补 TypeScript 示例统一运行说明
|
|
528
|
-
- [ ] 增补性能调优/基准解读手册
|
|
529
|
-
|
|
530
|
-
---
|
|
531
|
-
|
|
532
|
-
## 相关文档
|
|
533
|
-
|
|
534
|
-
- 📖 [README.md](https://github.com/vextjs/schema-dsl/blob/main/README.md) - 项目介绍
|
|
535
|
-
- 📖 [快速开始](./quick-start.md) - 5分钟入门
|
|
536
|
-
- 📖 [DSL语法指南](./dsl-syntax.md) - 完整语法
|
|
537
|
-
- 📖 [String扩展](./string-extensions.md) - String扩展特性
|
|
538
|
-
- 📖 [API参考](./api-reference.md) - 完整API
|
|
539
|
-
- 📖 [CHANGELOG](https://github.com/vextjs/schema-dsl/blob/main/CHANGELOG.md) - 更新日志
|
|
540
|
-
|
|
541
|
-
---
|
|
542
|
-
|
|
543
|
-
## 对应示例文件
|
|
544
|
-
|
|
545
|
-
**示例入口**: [feature-index.ts](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/feature-index.ts)
|
|
546
|
-
**说明**: 以单文件串联 DSL、String 扩展和导出器三个代表性能力,作为功能索引页的快速落地入口。
|
|
547
|
-
|
|
548
|
-
---
|
|
549
|
-
|
|
550
|
-
**最后更新**: 2026-05-08
|
|
551
|
-
**维护者**: schema-dsl Team
|
|
552
|
-
|
|
553
|
-
|
|
1
|
+
# schema-dsl 功能索引
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
> **更新时间**: 2026-04-30
|
|
5
|
+
> **用途**: 快速查找所有功能及其文档位置
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 📑 目录
|
|
10
|
+
|
|
11
|
+
- [核心API](#核心api)
|
|
12
|
+
- [验证功能](#验证功能)
|
|
13
|
+
- [导出器](#导出器)
|
|
14
|
+
- [工具函数](#工具函数)
|
|
15
|
+
- [错误处理](#错误处理)
|
|
16
|
+
- [配置管理](#配置管理)
|
|
17
|
+
- [示例代码](#示例代码)
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## 核心API
|
|
22
|
+
|
|
23
|
+
### dsl() 函数
|
|
24
|
+
|
|
25
|
+
**功能**: DSL主入口,支持字符串和对象定义
|
|
26
|
+
|
|
27
|
+
**使用示例**:
|
|
28
|
+
```javascript
|
|
29
|
+
const { dsl } = require('schema-dsl');
|
|
30
|
+
|
|
31
|
+
// 字符串定义
|
|
32
|
+
const builder = dsl('email!');
|
|
33
|
+
|
|
34
|
+
// 对象定义
|
|
35
|
+
const schema = dsl({
|
|
36
|
+
username: 'string:3-32!',
|
|
37
|
+
email: 'email!'
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
**文档位置**:
|
|
42
|
+
- 📖 [API参考 - dsl()函数](./api-reference.md#dsl-函数)
|
|
43
|
+
- 📖 [快速开始](./quick-start.md)
|
|
44
|
+
- 📖 [DSL语法指南](./dsl-syntax.md)
|
|
45
|
+
|
|
46
|
+
**代码位置**: `src/index.ts` / `src/adapters/DslAdapter.ts`
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
### DslBuilder 类
|
|
51
|
+
|
|
52
|
+
**功能**: Schema构建器,支持链式调用
|
|
53
|
+
|
|
54
|
+
**可用方法**:
|
|
55
|
+
- ✅ `pattern(regex, message?)` - 正则验证
|
|
56
|
+
- ✅ `label(text)` - 字段标签
|
|
57
|
+
- ✅ `messages(obj)` - 自定义错误消息
|
|
58
|
+
- ✅ `description(text)` - 字段描述
|
|
59
|
+
- ✅ `custom(fn)` - 自定义验证器
|
|
60
|
+
- ✅ `when(field, opts)` - 条件验证
|
|
61
|
+
- ✅ `default(value)` - 默认值
|
|
62
|
+
- ✅ `toSchema()` - 转为JSON Schema(含内部标记字段)
|
|
63
|
+
- ✅ `toJsonSchema()` - 转为纯净 JSON Schema(自动清理 `_required`/`_customMessages` 等内部标记,适用于 OpenAPI / 外部系统)🆕 v1.2.5
|
|
64
|
+
- ✅ `validate(data)` - 验证数据
|
|
65
|
+
- ✅ `validateNestingDepth(schema, maxDepth)` - 检测嵌套深度(静态方法)
|
|
66
|
+
|
|
67
|
+
**默认验证器方法**:
|
|
68
|
+
- ✅ `username(preset?)` - 用户名验证(preset: 'short'|'medium'|'long'|'5-20')
|
|
69
|
+
- ✅ `password(strength?)` - 密码强度验证(strength: 'weak'|'medium'|'strong'|'veryStrong')
|
|
70
|
+
- ✅ `phone(country?)` - 手机号验证(country: 'cn'|'us'|'uk'|'hk'|'tw'|'international')
|
|
71
|
+
|
|
72
|
+
**使用示例**:
|
|
73
|
+
```javascript
|
|
74
|
+
// 基础链式调用
|
|
75
|
+
const schema = dsl('string:3-32!')
|
|
76
|
+
.pattern(/^[a-zA-Z0-9_]+$/)
|
|
77
|
+
.label('用户名')
|
|
78
|
+
.messages({ 'pattern': '只能包含字母、数字和下划线' });
|
|
79
|
+
|
|
80
|
+
// 使用默认验证器
|
|
81
|
+
const userSchema = dsl({
|
|
82
|
+
username: dsl('string!').username(), // 自动设置3-32长度+正则
|
|
83
|
+
password: dsl('string!').password('strong'), // 强密码验证
|
|
84
|
+
phone: dsl('string!').phone('cn') // 中国手机号验证
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**文档位置**:
|
|
89
|
+
- 📖 [API参考 - DslBuilder类](./api-reference.md#dslbuilder-类)
|
|
90
|
+
- 📖 [String扩展文档](./string-extensions.md)
|
|
91
|
+
|
|
92
|
+
**代码位置**: `src/core/DslBuilder.ts`
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
### String 扩展
|
|
97
|
+
|
|
98
|
+
**功能**: 字符串直接链式调用,无需 dsl() 包裹
|
|
99
|
+
|
|
100
|
+
**可用方法**: 与 DslBuilder 相同
|
|
101
|
+
|
|
102
|
+
**使用示例**:
|
|
103
|
+
```javascript
|
|
104
|
+
const schema = dsl({
|
|
105
|
+
email: 'email!'.pattern(/custom/).label('邮箱'),
|
|
106
|
+
username: 'string:3-32!'.pattern(/^\w+$/).label('用户名')
|
|
107
|
+
});
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
**文档位置**:
|
|
111
|
+
- 📖 [String扩展完整文档](./string-extensions.md)
|
|
112
|
+
- 📖 [README](https://github.com/vextjs/schema-dsl/blob/main/README.md)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
**代码位置**: `src/core/StringExtensions.ts`
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## 验证功能
|
|
120
|
+
|
|
121
|
+
### Validator 类
|
|
122
|
+
|
|
123
|
+
**功能**: JSON Schema验证器(基于ajv)
|
|
124
|
+
|
|
125
|
+
**可用方法**:
|
|
126
|
+
- ✅ `validate(schema, data, options)` - 验证数据
|
|
127
|
+
- ✅ `compile(schema, cacheKey)` - 编译Schema
|
|
128
|
+
- ✅ `validateBatch(schema, dataArray)` - 批量验证
|
|
129
|
+
- ✅ `addKeyword(name, definition)` - 添加自定义关键字
|
|
130
|
+
- ✅ `addFormat(name, validator)` - 添加自定义格式
|
|
131
|
+
- ✅ `clearCache()` - 清空缓存
|
|
132
|
+
- ✅ `Validator.create(options)` - 创建实例(静态方法)
|
|
133
|
+
- ✅ `Validator.quickValidate(schema, data)` - 快速验证(静态方法)
|
|
134
|
+
|
|
135
|
+
**使用示例**:
|
|
136
|
+
```javascript
|
|
137
|
+
const { Validator } = require('schema-dsl');
|
|
138
|
+
|
|
139
|
+
const validator = new Validator();
|
|
140
|
+
const result = validator.validate(schema, data);
|
|
141
|
+
|
|
142
|
+
console.log(result.valid); // true/false
|
|
143
|
+
console.log(result.errors); // 错误列表
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
**文档位置**:
|
|
147
|
+
- 📖 [API参考 - Validator类](./api-reference.md#validator-类)
|
|
148
|
+
- 📖 [validate方法详解](./validate.md)
|
|
149
|
+
- 📖 [快速开始](./quick-start.md)
|
|
150
|
+
|
|
151
|
+
**代码位置**: `src/core/Validator.ts`
|
|
152
|
+
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
### validate() 便捷函数
|
|
156
|
+
|
|
157
|
+
**功能**: 单例验证,无需 new Validator()
|
|
158
|
+
|
|
159
|
+
**使用示例**:
|
|
160
|
+
```javascript
|
|
161
|
+
const { dsl, validate } = require('schema-dsl');
|
|
162
|
+
|
|
163
|
+
const schema = dsl({ email: 'email!' });
|
|
164
|
+
const result = validate(schema, { email: 'test@example.com' });
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
**文档位置**:
|
|
168
|
+
- 📖 [API参考 - validate()函数](./api-reference.md)
|
|
169
|
+
- 📖 [快速开始](./quick-start.md#1-hello-world30秒)
|
|
170
|
+
|
|
171
|
+
**代码位置**: `src/index.ts`(默认单例导出)
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
## 导出器
|
|
176
|
+
|
|
177
|
+
### MongoDBExporter
|
|
178
|
+
|
|
179
|
+
**功能**: 导出MongoDB $jsonSchema格式
|
|
180
|
+
|
|
181
|
+
**可用方法**:
|
|
182
|
+
- ✅ `export(schema)` - 导出Schema
|
|
183
|
+
- ✅ `generateCreateCommand(collectionName, schema)` - 生成createCollection命令
|
|
184
|
+
- ✅ `generateCommand(collectionName, schema)` - 生成可执行命令字符串
|
|
185
|
+
- ✅ `MongoDBExporter.export(schema)` - 快速导出(静态方法)
|
|
186
|
+
|
|
187
|
+
**使用示例**:
|
|
188
|
+
```javascript
|
|
189
|
+
const { exporters } = require('schema-dsl');
|
|
190
|
+
|
|
191
|
+
const exporter = new exporters.MongoDBExporter();
|
|
192
|
+
const mongoSchema = exporter.export(jsonSchema);
|
|
193
|
+
|
|
194
|
+
// 生成命令
|
|
195
|
+
const command = exporter.generateCommand('users', jsonSchema);
|
|
196
|
+
console.log(command);
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
**文档位置**:
|
|
200
|
+
- 📖 [数据库导出指南](./export-guide.md)
|
|
201
|
+
- 📖 [示例代码](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/feature-index.ts)
|
|
202
|
+
|
|
203
|
+
**代码位置**: `src/exporters/MongoDBExporter.ts`
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
### MySQLExporter
|
|
208
|
+
|
|
209
|
+
**功能**: 导出MySQL CREATE TABLE DDL
|
|
210
|
+
|
|
211
|
+
**可用方法**:
|
|
212
|
+
- ✅ `export(tableName, schema, options)` - 导出DDL
|
|
213
|
+
- ✅ `MySQLExporter.export(tableName, schema)` - 快速导出(静态方法)
|
|
214
|
+
|
|
215
|
+
**使用示例**:
|
|
216
|
+
```javascript
|
|
217
|
+
const { exporters } = require('schema-dsl');
|
|
218
|
+
|
|
219
|
+
const exporter = new exporters.MySQLExporter();
|
|
220
|
+
const ddl = exporter.export('users', jsonSchema);
|
|
221
|
+
|
|
222
|
+
console.log(ddl);
|
|
223
|
+
// CREATE TABLE `users` (
|
|
224
|
+
// `username` VARCHAR(32) NOT NULL,
|
|
225
|
+
// ...
|
|
226
|
+
// );
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
**文档位置**:
|
|
230
|
+
- 📖 [数据库导出指南](./export-guide.md)
|
|
231
|
+
- 📖 [示例代码](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/feature-index.ts)
|
|
232
|
+
|
|
233
|
+
**代码位置**: `src/exporters/MySQLExporter.ts`
|
|
234
|
+
|
|
235
|
+
---
|
|
236
|
+
|
|
237
|
+
### PostgreSQLExporter
|
|
238
|
+
|
|
239
|
+
**功能**: 导出PostgreSQL CREATE TABLE DDL
|
|
240
|
+
|
|
241
|
+
**可用方法**:
|
|
242
|
+
- ✅ `export(tableName, schema, options)` - 导出DDL
|
|
243
|
+
- ✅ `PostgreSQLExporter.export(tableName, schema)` - 快速导出(静态方法)
|
|
244
|
+
|
|
245
|
+
**使用示例**:
|
|
246
|
+
```javascript
|
|
247
|
+
const { exporters } = require('schema-dsl');
|
|
248
|
+
|
|
249
|
+
const exporter = new exporters.PostgreSQLExporter();
|
|
250
|
+
const ddl = exporter.export('users', jsonSchema);
|
|
251
|
+
|
|
252
|
+
console.log(ddl);
|
|
253
|
+
// CREATE TABLE public.users (
|
|
254
|
+
// username VARCHAR(32) NOT NULL,
|
|
255
|
+
// ...
|
|
256
|
+
// );
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
**文档位置**:
|
|
260
|
+
- 📖 [数据库导出指南](./export-guide.md)
|
|
261
|
+
- 📖 [示例代码](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/feature-index.ts)
|
|
262
|
+
|
|
263
|
+
**代码位置**: `src/exporters/PostgreSQLExporter.ts`
|
|
264
|
+
|
|
265
|
+
---
|
|
266
|
+
|
|
267
|
+
## 工具函数
|
|
268
|
+
|
|
269
|
+
### SchemaUtils
|
|
270
|
+
|
|
271
|
+
**功能**: Schema复用、合并、操作工具
|
|
272
|
+
|
|
273
|
+
**可用方法**:
|
|
274
|
+
- ✅ `reusable(factory)` - 创建可复用片段
|
|
275
|
+
- ✅ `createLibrary(fragments)` - 创建片段库
|
|
276
|
+
- ✅ `extend(baseSchema, extensions)` - 扩展Schema
|
|
277
|
+
- ✅ `pick(schema, fields)` - 筛选字段
|
|
278
|
+
- ✅ `omit(schema, fields)` - 排除字段
|
|
279
|
+
- ✅ `partial(schema, fields?)` - 将字段改为可选
|
|
280
|
+
- ✅ `validateBatch(schema, dataArray, ajvInstance)` - 带汇总统计的批量验证
|
|
281
|
+
- ✅ `withPerformance(validator)` - 为 Validator 包装性能信息
|
|
282
|
+
- ✅ `toMarkdown(schema)` - 导出为Markdown
|
|
283
|
+
- ✅ `toHTML(schema)` - 导出为HTML
|
|
284
|
+
- ✅ `clone(schema)` - 深度克隆
|
|
285
|
+
|
|
286
|
+
**使用示例**:
|
|
287
|
+
```javascript
|
|
288
|
+
const { SchemaUtils, dsl } = require('schema-dsl');
|
|
289
|
+
|
|
290
|
+
// Schema复用
|
|
291
|
+
const emailField = SchemaUtils.reusable(() => dsl('email!'));
|
|
292
|
+
|
|
293
|
+
const schema1 = dsl({ email: emailField() });
|
|
294
|
+
const schema2 = dsl({ contactEmail: emailField() });
|
|
295
|
+
|
|
296
|
+
// Schema合并
|
|
297
|
+
const extended = SchemaUtils.extend(schema1, { age: 'number' });
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
**文档位置**:
|
|
301
|
+
- 📖 [API参考 - SchemaUtils](./api-reference.md#工具函数)
|
|
302
|
+
|
|
303
|
+
**代码位置**: `src/utils/SchemaUtils.ts`
|
|
304
|
+
|
|
305
|
+
---
|
|
306
|
+
|
|
307
|
+
### TypeConverter
|
|
308
|
+
|
|
309
|
+
**功能**: 类型转换工具(JSON Schema ↔ 数据库类型)
|
|
310
|
+
|
|
311
|
+
**可用方法**:
|
|
312
|
+
- ✅ `toJSONSchemaType(nativeType)` - 转为 JSON Schema `type` 字符串
|
|
313
|
+
- ✅ `toMongoDBType(jsonSchemaType)` - 转为 MongoDB BSON 类型
|
|
314
|
+
- ✅ `toMySQLType(jsonSchemaType, schema?)` - 转为 MySQL 数据类型
|
|
315
|
+
- ✅ `toPostgreSQLType(jsonSchemaType, schema?)` - 转为 PostgreSQL 数据类型
|
|
316
|
+
- ✅ `normalizePropertyName(name)` - 规范化属性名
|
|
317
|
+
- ✅ `formatToRegex(format)` - 格式验证转正则
|
|
318
|
+
|
|
319
|
+
**文档位置**:
|
|
320
|
+
- 📖 [TypeConverter 文档](./type-converter.md)
|
|
321
|
+
|
|
322
|
+
**代码位置**: `src/utils/TypeConverter.ts`
|
|
323
|
+
|
|
324
|
+
---
|
|
325
|
+
|
|
326
|
+
### SchemaHelper
|
|
327
|
+
|
|
328
|
+
**功能**: Schema分析和辅助工具
|
|
329
|
+
|
|
330
|
+
**可用方法**:
|
|
331
|
+
- ✅ `isValidSchema(schema)` - 验证 Schema 有效性
|
|
332
|
+
- ✅ `generateSchemaId(schema)` - 生成基于内容的 Schema ID
|
|
333
|
+
- ✅ `getFieldPaths(schema)` - 提取字段路径
|
|
334
|
+
- ✅ `flattenSchema(schema)` - 扁平化 Schema
|
|
335
|
+
- ✅ `cloneSchema(schema)` - 克隆 Schema
|
|
336
|
+
- ✅ `extractRequiredFields(schema)` - 提取 required 字段
|
|
337
|
+
- ✅ `compareSchemas(schema1, schema2)` - 比较 Schema
|
|
338
|
+
- ✅ `simplifySchema(schema)` - 精简 Schema
|
|
339
|
+
- ✅ `isValidPropertyName(name)` - 校验属性名
|
|
340
|
+
- ✅ `getSchemaComplexity(schema)` - 评估复杂度
|
|
341
|
+
- ✅ `summarizeSchema(schema)` - 生成摘要
|
|
342
|
+
|
|
343
|
+
**文档位置**:
|
|
344
|
+
- 📖 [SchemaHelper 文档](./schema-helper.md)
|
|
345
|
+
|
|
346
|
+
**代码位置**: `src/utils/SchemaHelper.ts`
|
|
347
|
+
|
|
348
|
+
---
|
|
349
|
+
|
|
350
|
+
## 错误处理
|
|
351
|
+
|
|
352
|
+
### ErrorFormatter
|
|
353
|
+
|
|
354
|
+
**功能**: 格式化验证错误信息
|
|
355
|
+
|
|
356
|
+
**可用方法**:
|
|
357
|
+
- ✅ `new ErrorFormatter(locale?, messages?)` - 创建格式化器
|
|
358
|
+
- ✅ `format(error, locale?)` - 格式化单个错误为消息字符串
|
|
359
|
+
- ✅ `formatDetailed(errors, locale?, customMessages?, alreadyMerged?)` - 格式化错误数组为标准错误项
|
|
360
|
+
|
|
361
|
+
**文档位置**:
|
|
362
|
+
- 📖 [API参考 - ErrorFormatter / MessageTemplate / 底层解析工具](./api-reference.md)
|
|
363
|
+
- 📖 [错误处理文档](./error-handling.md)
|
|
364
|
+
|
|
365
|
+
**代码位置**: `src/core/ErrorFormatter.ts`
|
|
366
|
+
|
|
367
|
+
---
|
|
368
|
+
|
|
369
|
+
### ErrorCodes
|
|
370
|
+
|
|
371
|
+
**功能**: 错误码定义
|
|
372
|
+
|
|
373
|
+
**代码位置**: `src/core/ErrorCodes.ts`
|
|
374
|
+
|
|
375
|
+
---
|
|
376
|
+
|
|
377
|
+
### MessageTemplate
|
|
378
|
+
|
|
379
|
+
**功能**: 错误消息模板
|
|
380
|
+
|
|
381
|
+
**可用方法**:
|
|
382
|
+
- ✅ `render(template, vars)` - 渲染模板
|
|
383
|
+
- ✅ `MessageTemplate.render(template, vars)` - 快速渲染(静态方法)
|
|
384
|
+
- ✅ `MessageTemplate.renderBatch(templates, vars)` - 批量渲染(静态方法)
|
|
385
|
+
|
|
386
|
+
**文档位置**:
|
|
387
|
+
- 📖 [API参考 - MessageTemplate](./api-reference.md#messagetemplate)
|
|
388
|
+
|
|
389
|
+
**代码位置**: `src/core/MessageTemplate.ts`
|
|
390
|
+
|
|
391
|
+
---
|
|
392
|
+
|
|
393
|
+
### 类型注册与模板工具
|
|
394
|
+
|
|
395
|
+
**功能**: 面向进阶集成的模板渲染、JSON Schema 外观与自定义类型注册能力
|
|
396
|
+
|
|
397
|
+
**可用导出**:
|
|
398
|
+
- ✅ `renderTemplate(template, params)`
|
|
399
|
+
- ✅ `JSONSchemaCore`
|
|
400
|
+
- ✅ `TypeRegistry`
|
|
401
|
+
|
|
402
|
+
**文档位置**:
|
|
403
|
+
- 📖 [API参考 - renderTemplate / JSONSchemaCore / 类型注册与内部解析边界](./api-reference.md)
|
|
404
|
+
|
|
405
|
+
**代码位置**: `src/core/TemplateEngine.ts` / `src/core/JSONSchemaCore.ts` / `src/parser/TypeRegistry.ts`
|
|
406
|
+
|
|
407
|
+
---
|
|
408
|
+
|
|
409
|
+
### Locale
|
|
410
|
+
|
|
411
|
+
**功能**: 国际化支持
|
|
412
|
+
|
|
413
|
+
**可用方法**:
|
|
414
|
+
- ✅ `setLocale(locale)` - 设置语言
|
|
415
|
+
- ✅ `getLocale()` - 获取当前语言
|
|
416
|
+
- ✅ `addLocale(locale, messages)` - 添加语言包
|
|
417
|
+
- ✅ `setMessages(messages)` - 设置全局消息
|
|
418
|
+
- ✅ `getMessage(code, customMessages)` - 获取消息
|
|
419
|
+
- ✅ `getAvailableLocales()` - 获取可用语言
|
|
420
|
+
- ✅ `reset()` - 重置
|
|
421
|
+
|
|
422
|
+
**支持语言**:
|
|
423
|
+
- ✅ en-US(英语)
|
|
424
|
+
- ✅ zh-CN(中文)
|
|
425
|
+
- ✅ ja-JP(日语)
|
|
426
|
+
- ✅ es-ES(西班牙语)
|
|
427
|
+
- ✅ fr-FR(法语)
|
|
428
|
+
|
|
429
|
+
**文档位置**:
|
|
430
|
+
- 📖 [API参考 - Locale](./api-reference.md)
|
|
431
|
+
|
|
432
|
+
**代码位置**: `src/core/Locale.ts`
|
|
433
|
+
|
|
434
|
+
---
|
|
435
|
+
|
|
436
|
+
## 配置管理
|
|
437
|
+
|
|
438
|
+
### CacheManager
|
|
439
|
+
|
|
440
|
+
**功能**: Schema编译缓存管理
|
|
441
|
+
|
|
442
|
+
**可用方法**:
|
|
443
|
+
- ✅ `get(key)` - 获取缓存
|
|
444
|
+
- ✅ `set(key, value)` - 设置缓存
|
|
445
|
+
- ✅ `has(key)` - 检查缓存
|
|
446
|
+
- ✅ `delete(key)` - 删除缓存
|
|
447
|
+
- ✅ `clear()` - 清空缓存
|
|
448
|
+
- ✅ `size()` - 缓存大小
|
|
449
|
+
|
|
450
|
+
**文档位置**:
|
|
451
|
+
- 📖 [CacheManager 文档](./cache-manager.md)
|
|
452
|
+
|
|
453
|
+
**代码位置**: `src/core/CacheManager.ts`
|
|
454
|
+
|
|
455
|
+
---
|
|
456
|
+
|
|
457
|
+
### CustomKeywords
|
|
458
|
+
|
|
459
|
+
**功能**: 自定义验证关键字
|
|
460
|
+
|
|
461
|
+
**可用关键字**:
|
|
462
|
+
- ✅ `regex` - 正则验证
|
|
463
|
+
- ✅ `validate` - 自定义函数验证
|
|
464
|
+
|
|
465
|
+
**使用示例**:
|
|
466
|
+
```javascript
|
|
467
|
+
const { Validator, CustomKeywords } = require('schema-dsl');
|
|
468
|
+
|
|
469
|
+
const validator = new Validator();
|
|
470
|
+
CustomKeywords.registerAll(validator.getAjv());
|
|
471
|
+
|
|
472
|
+
const schema = {
|
|
473
|
+
type: 'string',
|
|
474
|
+
regex: '^[a-z]+$'
|
|
475
|
+
};
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
**文档位置**:
|
|
479
|
+
- 📖 [错误处理文档](./error-handling.md)
|
|
480
|
+
|
|
481
|
+
**代码位置**: `src/validators/CustomKeywords.ts`
|
|
482
|
+
|
|
483
|
+
---
|
|
484
|
+
|
|
485
|
+
## 示例代码
|
|
486
|
+
|
|
487
|
+
### 完整示例目录
|
|
488
|
+
|
|
489
|
+
**基础示例**:
|
|
490
|
+
- 📄 [dsl-syntax.ts](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/dsl-syntax.ts) - DSL基础用法
|
|
491
|
+
- 📄 [string-extensions.ts](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/string-extensions.ts) - String扩展示例
|
|
492
|
+
|
|
493
|
+
**场景示例**:
|
|
494
|
+
- 📄 [quick-start.ts](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/quick-start.ts) - 用户注册式的基础表单验证起点
|
|
495
|
+
- 📄 [validation-guide.ts](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/validation-guide.ts) - 失败路径、错误处理与规则组合示例
|
|
496
|
+
|
|
497
|
+
**导出示例**:
|
|
498
|
+
- 📄 [feature-index.ts](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/feature-index.ts) - 功能索引代表性示例
|
|
499
|
+
|
|
500
|
+
---
|
|
501
|
+
|
|
502
|
+
## 功能覆盖检查
|
|
503
|
+
|
|
504
|
+
### ✅ 已完整文档化
|
|
505
|
+
|
|
506
|
+
1. ✅ DSL语法 - `docs/dsl-syntax.md` (2815行)
|
|
507
|
+
2. ✅ String扩展 - `docs/string-extensions.md`
|
|
508
|
+
3. ✅ Validator类 - `docs/validate.md`
|
|
509
|
+
4. ✅ API参考 - `docs/api-reference.md`
|
|
510
|
+
5. ✅ 快速开始 - `docs/quick-start.md`
|
|
511
|
+
6. ✅ 数据库导出 - `README.md` + `docs/export-guide.md`
|
|
512
|
+
7. ✅ 自定义验证 - `README.md`
|
|
513
|
+
8. ✅ Schema工具 - `docs/schema-utils.md` + `docs/schema-helper.md`
|
|
514
|
+
|
|
515
|
+
### ⚠️ 文档需要补充
|
|
516
|
+
|
|
517
|
+
1. ⚠️ ErrorFormatter - 当前已在 API 参考与错误处理文档中覆盖;如需更聚焦入口,可后续增补专项文档
|
|
518
|
+
2. ⚠️ PluginManager - 可补一份更聚焦的 API/Hook 速查
|
|
519
|
+
3. ⚠️ 性能与基准测试 - 可继续补充独立诊断手册
|
|
520
|
+
4. ⚠️ 示例运行方式 - 可补充统一的 TypeScript 示例编译说明
|
|
521
|
+
5. ⚠️ 错误处理 - 可继续补充更完整的框架集成案例
|
|
522
|
+
|
|
523
|
+
### 📝 计划补充
|
|
524
|
+
|
|
525
|
+
- [ ] 增补 `ErrorFormatter` 专项文档
|
|
526
|
+
- [ ] 增补 Plugin Hook 速查文档
|
|
527
|
+
- [ ] 增补 TypeScript 示例统一运行说明
|
|
528
|
+
- [ ] 增补性能调优/基准解读手册
|
|
529
|
+
|
|
530
|
+
---
|
|
531
|
+
|
|
532
|
+
## 相关文档
|
|
533
|
+
|
|
534
|
+
- 📖 [README.md](https://github.com/vextjs/schema-dsl/blob/main/README.md) - 项目介绍
|
|
535
|
+
- 📖 [快速开始](./quick-start.md) - 5分钟入门
|
|
536
|
+
- 📖 [DSL语法指南](./dsl-syntax.md) - 完整语法
|
|
537
|
+
- 📖 [String扩展](./string-extensions.md) - String扩展特性
|
|
538
|
+
- 📖 [API参考](./api-reference.md) - 完整API
|
|
539
|
+
- 📖 [CHANGELOG](https://github.com/vextjs/schema-dsl/blob/main/CHANGELOG.md) - 更新日志
|
|
540
|
+
|
|
541
|
+
---
|
|
542
|
+
|
|
543
|
+
## 对应示例文件
|
|
544
|
+
|
|
545
|
+
**示例入口**: [feature-index.ts](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/feature-index.ts)
|
|
546
|
+
**说明**: 以单文件串联 DSL、String 扩展和导出器三个代表性能力,作为功能索引页的快速落地入口。
|
|
547
|
+
|
|
548
|
+
---
|
|
549
|
+
|
|
550
|
+
**最后更新**: 2026-05-08
|
|
551
|
+
**维护者**: schema-dsl Team
|
|
552
|
+
|
|
553
|
+
|