schema-dsl 1.0.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.
Files changed (116) hide show
  1. package/.eslintignore +10 -0
  2. package/.eslintrc.json +27 -0
  3. package/.github/CODE_OF_CONDUCT.md +45 -0
  4. package/.github/ISSUE_TEMPLATE/bug_report.md +57 -0
  5. package/.github/ISSUE_TEMPLATE/config.yml +11 -0
  6. package/.github/ISSUE_TEMPLATE/feature_request.md +45 -0
  7. package/.github/ISSUE_TEMPLATE/question.md +31 -0
  8. package/.github/PULL_REQUEST_TEMPLATE.md +70 -0
  9. package/.github/SECURITY.md +184 -0
  10. package/.github/workflows/ci.yml +33 -0
  11. package/CHANGELOG.md +633 -0
  12. package/CONTRIBUTING.md +368 -0
  13. package/LICENSE +21 -0
  14. package/README.md +1184 -0
  15. package/STATUS.md +101 -0
  16. package/docs/FEATURE-INDEX.md +519 -0
  17. package/docs/INDEX.md +253 -0
  18. package/docs/api-reference.md +1096 -0
  19. package/docs/best-practices.md +672 -0
  20. package/docs/cache-manager.md +336 -0
  21. package/docs/design-philosophy.md +601 -0
  22. package/docs/dsl-syntax.md +653 -0
  23. package/docs/dynamic-locale.md +552 -0
  24. package/docs/error-handling.md +703 -0
  25. package/docs/export-guide.md +462 -0
  26. package/docs/export-limitations.md +551 -0
  27. package/docs/faq.md +577 -0
  28. package/docs/frontend-i18n-guide.md +290 -0
  29. package/docs/i18n-user-guide.md +476 -0
  30. package/docs/label-vs-description.md +262 -0
  31. package/docs/markdown-exporter.md +397 -0
  32. package/docs/mongodb-exporter.md +295 -0
  33. package/docs/multi-type-support.md +319 -0
  34. package/docs/mysql-exporter.md +273 -0
  35. package/docs/plugin-system.md +542 -0
  36. package/docs/postgresql-exporter.md +304 -0
  37. package/docs/quick-start.md +761 -0
  38. package/docs/schema-helper.md +340 -0
  39. package/docs/schema-utils-chaining.md +143 -0
  40. package/docs/schema-utils.md +490 -0
  41. package/docs/string-extensions.md +480 -0
  42. package/docs/troubleshooting.md +471 -0
  43. package/docs/type-converter.md +319 -0
  44. package/docs/type-reference.md +219 -0
  45. package/docs/validate-async.md +480 -0
  46. package/docs/validate.md +486 -0
  47. package/docs/validation-guide.md +484 -0
  48. package/examples/array-dsl-example.js +227 -0
  49. package/examples/custom-extension.js +85 -0
  50. package/examples/dsl-match-example.js +74 -0
  51. package/examples/dsl-style.js +118 -0
  52. package/examples/dynamic-locale-configuration.js +348 -0
  53. package/examples/dynamic-locale-example.js +287 -0
  54. package/examples/export-demo.js +130 -0
  55. package/examples/express-integration.js +376 -0
  56. package/examples/i18n-full-demo.js +310 -0
  57. package/examples/i18n-memory-safety.examples.js +268 -0
  58. package/examples/markdown-export.js +71 -0
  59. package/examples/middleware-usage.js +93 -0
  60. package/examples/new-features-comparison.js +315 -0
  61. package/examples/password-reset/README.md +153 -0
  62. package/examples/password-reset/schema.js +26 -0
  63. package/examples/password-reset/test.js +101 -0
  64. package/examples/plugin-system.examples.js +205 -0
  65. package/examples/schema-utils-chaining.examples.js +250 -0
  66. package/examples/simple-example.js +122 -0
  67. package/examples/string-extensions.js +297 -0
  68. package/examples/user-registration/README.md +156 -0
  69. package/examples/user-registration/routes.js +92 -0
  70. package/examples/user-registration/schema.js +150 -0
  71. package/examples/user-registration/server.js +74 -0
  72. package/index.d.ts +1999 -0
  73. package/index.js +282 -0
  74. package/index.mjs +30 -0
  75. package/lib/adapters/DslAdapter.js +699 -0
  76. package/lib/adapters/index.js +20 -0
  77. package/lib/config/constants.js +286 -0
  78. package/lib/config/patterns/creditCard.js +9 -0
  79. package/lib/config/patterns/idCard.js +9 -0
  80. package/lib/config/patterns/index.js +8 -0
  81. package/lib/config/patterns/licensePlate.js +4 -0
  82. package/lib/config/patterns/passport.js +4 -0
  83. package/lib/config/patterns/phone.js +9 -0
  84. package/lib/config/patterns/postalCode.js +5 -0
  85. package/lib/core/CacheManager.js +376 -0
  86. package/lib/core/DslBuilder.js +740 -0
  87. package/lib/core/ErrorCodes.js +233 -0
  88. package/lib/core/ErrorFormatter.js +342 -0
  89. package/lib/core/JSONSchemaCore.js +347 -0
  90. package/lib/core/Locale.js +119 -0
  91. package/lib/core/MessageTemplate.js +89 -0
  92. package/lib/core/PluginManager.js +448 -0
  93. package/lib/core/StringExtensions.js +209 -0
  94. package/lib/core/Validator.js +376 -0
  95. package/lib/errors/ValidationError.js +191 -0
  96. package/lib/exporters/MarkdownExporter.js +420 -0
  97. package/lib/exporters/MongoDBExporter.js +162 -0
  98. package/lib/exporters/MySQLExporter.js +212 -0
  99. package/lib/exporters/PostgreSQLExporter.js +289 -0
  100. package/lib/exporters/index.js +24 -0
  101. package/lib/locales/en-US.js +65 -0
  102. package/lib/locales/es-ES.js +66 -0
  103. package/lib/locales/fr-FR.js +66 -0
  104. package/lib/locales/index.js +8 -0
  105. package/lib/locales/ja-JP.js +66 -0
  106. package/lib/locales/zh-CN.js +93 -0
  107. package/lib/utils/LRUCache.js +174 -0
  108. package/lib/utils/SchemaHelper.js +240 -0
  109. package/lib/utils/SchemaUtils.js +445 -0
  110. package/lib/utils/TypeConverter.js +245 -0
  111. package/lib/utils/index.js +13 -0
  112. package/lib/validators/CustomKeywords.js +203 -0
  113. package/lib/validators/index.js +11 -0
  114. package/package.json +70 -0
  115. package/plugins/custom-format.js +101 -0
  116. package/plugins/custom-validator.js +200 -0
@@ -0,0 +1,1096 @@
1
+ # schema-dsl API 参考文档
2
+
3
+
4
+ > **更新时间**: 2025-12-25
5
+
6
+ ---
7
+
8
+ ## 📑 目录
9
+
10
+ - [dsl() 函数](#dsl-函数)
11
+ - [DslBuilder 类](#dslbuilder-类)
12
+ - [String 扩展](#string-扩展)
13
+ - [Validator 类](#validator-类)
14
+ - [导出器](#导出器)
15
+ - [工具函数](#工具函数)
16
+
17
+ ---
18
+
19
+ ## dsl() 函数
20
+
21
+ ### 描述
22
+
23
+ DSL 主入口函数,支持字符串和对象两种定义方式。
24
+
25
+ ### 语法
26
+
27
+ ```javascript
28
+ dsl(definition: string | object): DslBuilder | JSONSchema
29
+ ```
30
+
31
+ ### 参数
32
+
33
+ - `definition` (**string** | **object**) - DSL定义
34
+ - 字符串:返回 DslBuilder 实例(可链式调用)
35
+ - 对象:返回 JSON Schema 对象
36
+
37
+ ### 返回值
38
+
39
+ - **DslBuilder** - 当参数为字符串时
40
+ - **Object** - 当参数为对象时(JSON Schema)
41
+
42
+ ### 示例
43
+
44
+ ```javascript
45
+ // 字符串:返回 DslBuilder
46
+ const builder = dsl('email!');
47
+ builder.pattern(/custom/).label('邮箱');
48
+
49
+ // 对象:返回 JSON Schema
50
+ const schema = dsl({
51
+ username: 'string:3-32!',
52
+ email: 'email!'
53
+ });
54
+ ```
55
+
56
+ ---
57
+
58
+ ## DslBuilder 类
59
+
60
+ ### 描述
61
+
62
+ Schema 构建器类,支持链式调用添加验证规则。
63
+
64
+ ### 构造函数
65
+
66
+ ```javascript
67
+ new DslBuilder(dslString: string)
68
+ ```
69
+
70
+ **参数**:
71
+ - `dslString` (**string**) - DSL字符串,如 `'string:3-32!'`
72
+
73
+ ### 方法
74
+
75
+ #### `.pattern(regex, message?)`
76
+
77
+ 添加正则表达式验证。
78
+
79
+ **参数**:
80
+ - `regex` (**RegExp** | **string**) - 正则表达式
81
+ - `message` (**string**, 可选) - 自定义错误消息
82
+
83
+ **返回**: **DslBuilder**
84
+
85
+ **示例**:
86
+ ```javascript
87
+ dsl('string:3-32!')
88
+ .pattern(/^[a-zA-Z0-9_]+$/, '只能包含字母、数字和下划线')
89
+ ```
90
+
91
+ ---
92
+
93
+ #### `.label(text)`
94
+
95
+ 设置字段标签(用于错误消息)。
96
+
97
+ **参数**:
98
+ - `text` (**string**) - 标签文本
99
+
100
+ **返回**: **DslBuilder**
101
+
102
+ **示例**:
103
+ ```javascript
104
+ dsl('email!').label('邮箱地址')
105
+ ```
106
+
107
+ ---
108
+
109
+ #### `.messages(messages)`
110
+
111
+ 自定义错误消息。
112
+
113
+ **参数**:
114
+ - `messages` (**Object**) - 错误消息对象
115
+ - 键:错误代码(如 `'string.min'`)
116
+ - 值:错误消息模板
117
+
118
+ **返回**: **DslBuilder**
119
+
120
+ **示例**:
121
+ ```javascript
122
+ dsl('string:3-32!')
123
+ .messages({
124
+ 'min': '至少{{#limit}}个字符',
125
+ 'max': '最多{{#limit}}个字符'
126
+ })
127
+ ```
128
+
129
+ ---
130
+
131
+ #### `.description(text)`
132
+
133
+ 设置字段描述。
134
+
135
+ **参数**:
136
+ - `text` (**string**) - 描述文本
137
+
138
+ **返回**: **DslBuilder**
139
+
140
+ **示例**:
141
+ ```javascript
142
+ dsl('url').description('个人主页链接')
143
+ ```
144
+
145
+ ---
146
+
147
+ #### `.custom(validator)`
148
+
149
+ 添加自定义验证器。
150
+
151
+ **参数**:
152
+ - `validator` (**Function**) - 验证函数
153
+ - 签名:`(value) => boolean | Promise<boolean> | { error, message }`
154
+ - 返回 `true` 表示通过
155
+ - 返回 `false` 或错误对象表示失败
156
+
157
+ **返回**: **DslBuilder**
158
+
159
+ **示例**:
160
+ ```javascript
161
+ dsl('string:3-32!')
162
+ .custom(async (value) => {
163
+ const exists = await checkUsernameExists(value);
164
+ if (exists) {
165
+ return { error: 'username.exists', message: '用户名已存在' };
166
+ }
167
+ return true;
168
+ })
169
+ ```
170
+
171
+
172
+ ---
173
+
174
+ #### `.default(value)`
175
+
176
+ 设置默认值。
177
+
178
+ **参数**:
179
+ - `value` (**any**) - 默认值
180
+
181
+ **返回**: **DslBuilder**
182
+
183
+ **示例**:
184
+ ```javascript
185
+ dsl('string').default('guest')
186
+ ```
187
+
188
+ ---
189
+
190
+ #### `.username(preset?)`
191
+
192
+ 用户名验证(自动设置长度和正则)。
193
+
194
+ **参数**:
195
+ - `preset` (**string** | **Object**, 可选) - 预设配置
196
+ - 字符串:`'short'` | `'medium'` | `'long'` | `'5-20'`
197
+ - 对象:`{ minLength, maxLength, allowUnderscore, allowNumber }`
198
+ - 默认值:`'medium'` (3-32位)
199
+
200
+ **返回**: **DslBuilder**
201
+
202
+ **示例**:
203
+ ```javascript
204
+ // 默认 medium (3-32位)
205
+ dsl('string!').username()
206
+
207
+ // 自定义范围
208
+ dsl('string!').username('5-20')
209
+
210
+ // 使用预设
211
+ dsl('string!').username('short') // 3-16位
212
+ ```
213
+
214
+ ---
215
+
216
+ #### `.password(strength?)`
217
+
218
+ 密码强度验证(自动设置长度和正则)。
219
+
220
+ **参数**:
221
+ - `strength` (**string**, 可选) - 强度级别
222
+ - `'weak'` - 最少6位
223
+ - `'medium'` - 8位,字母+数字(默认)
224
+ - `'strong'` - 8位,大小写+数字
225
+ - `'veryStrong'` - 10位,大小写+数字+特殊字符
226
+
227
+ **返回**: **DslBuilder**
228
+
229
+ **示例**:
230
+ ```javascript
231
+ dsl('string!').password('strong')
232
+ ```
233
+
234
+ ---
235
+
236
+ #### `.phone(country?)`
237
+
238
+ 手机号验证(自动设置长度和正则)。
239
+
240
+ **参数**:
241
+ - `country` (**string**, 可选) - 国家代码
242
+ - `'cn'` - 中国(默认)
243
+ - `'us'` - 美国
244
+ - `'uk'` - 英国
245
+ - `'hk'` - 香港
246
+ - `'tw'` - 台湾
247
+ - `'international'` - 国际格式
248
+
249
+ **返回**: **DslBuilder**
250
+
251
+ **注意**: 自动将类型纠正为 `string`(即使写成 `number` 也会自动修正)
252
+
253
+ **示例**:
254
+ ```javascript
255
+ // 推荐写法
256
+ dsl('string!').phone('cn')
257
+
258
+ // 自动纠正:number → string
259
+ dsl('number!').phone('cn') // 自动纠正为 string
260
+ ```
261
+
262
+ ---
263
+
264
+ #### `.toSchema()`
265
+
266
+ 转换为 JSON Schema 对象。
267
+
268
+ **返回**: **Object** - JSON Schema对象
269
+
270
+ **示例**:
271
+ ```javascript
272
+ const schema = dsl('email!').label('邮箱').toSchema();
273
+ // { type: 'string', format: 'email', _label: '邮箱', _required: true }
274
+ ```
275
+
276
+ ---
277
+
278
+ #### `.validate(data, context?)`
279
+
280
+ 验证数据(便捷方法)。
281
+
282
+ **参数**:
283
+ - `data` (**any**) - 待验证数据
284
+ - `context` (**Object**, 可选) - 验证上下文
285
+
286
+ **返回**: **Promise<Object>** - 验证结果
287
+ - `valid` (**boolean**) - 是否通过
288
+ - `errors` (**Array**, 可选) - 错误列表
289
+ - `data` (**any**, 可选) - 验证通过的数据
290
+
291
+ **示例**:
292
+ ```javascript
293
+ const result = await dsl('email!').validate('user@example.com');
294
+ console.log(result.valid); // true
295
+ ```
296
+
297
+ ---
298
+
299
+ ### 静态方法
300
+
301
+ #### `dsl.match(field, map)```
302
+
303
+ 创建条件验证规则(类似 switch-case)。
304
+
305
+ **参数**:
306
+ - `field` (**string**) - 依赖的字段名
307
+ - `map` (**Object**) - 值与Schema的映射
308
+ - `[value: string]`: 对应的Schema
309
+ - `_default` (**optional**): 默认Schema
310
+
311
+ **返回**: **Object** - 内部Match结构
312
+
313
+ **示例**:
314
+ ```javascript
315
+ dsl.match('type', {
316
+ email: 'email!',
317
+ phone: 'string:11!',
318
+ _default: 'string'
319
+ })
320
+ ```
321
+
322
+ #### `dsl.if(condition, thenSchema, elseSchema)`
323
+
324
+ 创建简单的条件验证规则。
325
+
326
+ **参数**:
327
+ - `condition` (**string**) - 条件字段名
328
+ - `thenSchema` (**string|Object**) - 满足条件时的Schema
329
+ - `elseSchema` (**string|Object**, 可选) - 不满足条件时的Schema
330
+
331
+ **返回**: **Object** - 内部If结构
332
+
333
+ **示例**:
334
+ ```javascript
335
+ dsl.if('isVip', 'number:0-50', 'number:0-10')
336
+ ```
337
+
338
+ ---
339
+
340
+ ## DslBuilder 类
341
+
342
+ ### 描述
343
+
344
+ Schema 构建器类,支持链式调用添加验证规则。
345
+
346
+ ### 构造函数
347
+
348
+ ```javascript
349
+ new DslBuilder(dslString: string)
350
+ ```
351
+
352
+ **参数**:
353
+ - `dslString` (**string**) - DSL字符串,如 `'string:3-32!'`
354
+
355
+ ### 方法
356
+
357
+ #### `.pattern(regex, message?)`
358
+
359
+ 添加正则表达式验证。
360
+
361
+ **参数**:
362
+ - `regex` (**RegExp** | **string**) - 正则表达式
363
+ - `message` (**string**, 可选) - 自定义错误消息
364
+
365
+ **返回**: **DslBuilder**
366
+
367
+ **示例**:
368
+ ```javascript
369
+ dsl('string:3-32!')
370
+ .pattern(/^[a-zA-Z0-9_]+$/, '只能包含字母、数字和下划线')
371
+ ```
372
+
373
+ ---
374
+
375
+ #### `.label(text)`
376
+
377
+ 设置字段标签(用于错误消息)。
378
+
379
+ **参数**:
380
+ - `text` (**string**) - 标签文本
381
+
382
+ **返回**: **DslBuilder**
383
+
384
+ **示例**:
385
+ ```javascript
386
+ dsl('email!').label('邮箱地址')
387
+ ```
388
+
389
+ ---
390
+
391
+ #### `.messages(messages)`
392
+
393
+ 自定义错误消息。
394
+
395
+ **参数**:
396
+ - `messages` (**Object**) - 错误消息对象
397
+ - 键:错误代码(如 `'string.min'`)
398
+ - 值:错误消息模板
399
+
400
+ **返回**: **DslBuilder**
401
+
402
+ **示例**:
403
+ ```javascript
404
+ dsl('string:3-32!')
405
+ .messages({
406
+ 'string.min': '至少{{#limit}}个字符',
407
+ 'string.max': '最多{{#limit}}个字符'
408
+ })
409
+ ```
410
+
411
+ ---
412
+
413
+ #### `.description(text)`
414
+
415
+ 设置字段描述。
416
+
417
+ **参数**:
418
+ - `text` (**string**) - 描述文本
419
+
420
+ **返回**: **DslBuilder**
421
+
422
+ **示例**:
423
+ ```javascript
424
+ dsl('url').description('个人主页链接')
425
+ ```
426
+
427
+ ---
428
+
429
+ #### `.custom(validator)`
430
+
431
+ 添加自定义验证器。
432
+
433
+ **参数**:
434
+ - `validator` (**Function**) - 验证函数
435
+ - 签名:`(value) => boolean | Promise<boolean> | { error, message }`
436
+ - 返回 `true` 表示通过
437
+ - 返回 `false` 或错误对象表示失败
438
+
439
+ **返回**: **DslBuilder**
440
+
441
+ **示例**:
442
+ ```javascript
443
+ dsl('string:3-32!')
444
+ .custom(async (value) => {
445
+ const exists = await checkUsernameExists(value);
446
+ if (exists) {
447
+ return { error: 'username.exists', message: '用户名已存在' };
448
+ }
449
+ return true;
450
+ })
451
+ ```
452
+
453
+ ---
454
+
455
+ #### `.default(value)`
456
+
457
+ 设置默认值。
458
+
459
+ **参数**:
460
+ - `value` (**any**) - 默认值
461
+
462
+ **返回**: **DslBuilder**
463
+
464
+ **示例**:
465
+ ```javascript
466
+ dsl('string').default('guest')
467
+ ```
468
+
469
+ ---
470
+
471
+ #### `.username(preset?)`
472
+
473
+ 用户名验证(自动设置长度和正则)。
474
+
475
+ **参数**:
476
+ - `preset` (**string** | **Object**, 可选) - 预设配置
477
+ - 字符串:`'short'` | `'medium'` | `'long'` | `'5-20'`
478
+ - 对象:`{ minLength, maxLength, allowUnderscore, allowNumber }`
479
+ - 默认值:`'medium'` (3-32位)
480
+
481
+ **返回**: **DslBuilder**
482
+
483
+ **示例**:
484
+ ```javascript
485
+ // 默认 medium (3-32位)
486
+ dsl('string!').username()
487
+
488
+ // 自定义范围
489
+ dsl('string!').username('5-20')
490
+
491
+ // 使用预设
492
+ dsl('string!').username('short') // 3-16位
493
+ ```
494
+
495
+ ---
496
+
497
+ #### `.password(strength?)`
498
+
499
+ 密码强度验证(自动设置长度和正则)。
500
+
501
+ **参数**:
502
+ - `strength` (**string**, 可选) - 强度级别
503
+ - `'weak'` - 最少6位
504
+ - `'medium'` - 8位,字母+数字(默认)
505
+ - `'strong'` - 8位,大小写+数字
506
+ - `'veryStrong'` - 10位,大小写+数字+特殊字符
507
+
508
+ **返回**: **DslBuilder**
509
+
510
+ **示例**:
511
+ ```javascript
512
+ dsl('string!').password('strong')
513
+ ```
514
+
515
+ ---
516
+
517
+ #### `.phone(country?)`
518
+
519
+ 手机号验证(自动设置长度和正则)。
520
+
521
+ **参数**:
522
+ - `country` (**string**, 可选) - 国家代码
523
+ - `'cn'` - 中国(默认)
524
+ - `'us'` - 美国
525
+ - `'uk'` - 英国
526
+ - `'hk'` - 香港
527
+ - `'tw'` - 台湾
528
+ - `'international'` - 国际格式
529
+
530
+ **返回**: **DslBuilder**
531
+
532
+ **注意**: 自动将类型纠正为 `string`(即使写成 `number` 也会自动修正)
533
+
534
+ **示例**:
535
+ ```javascript
536
+ // 推荐写法
537
+ dsl('string!').phone('cn')
538
+
539
+ // 自动纠正:number → string
540
+ dsl('number!').phone('cn') // 自动纠正为 string
541
+ ```
542
+
543
+ ---
544
+
545
+ #### `.toSchema()`
546
+
547
+ 转换为 JSON Schema 对象。
548
+
549
+ **返回**: **Object** - JSON Schema对象
550
+
551
+ **示例**:
552
+ ```javascript
553
+ const schema = dsl('email!').label('邮箱').toSchema();
554
+ // { type: 'string', format: 'email', _label: '邮箱', _required: true }
555
+ ```
556
+
557
+ ---
558
+
559
+ #### `.validate(data, context?)`
560
+
561
+ 验证数据(便捷方法)。
562
+
563
+ **参数**:
564
+ - `data` (**any**) - 待验证数据
565
+ - `context` (**Object**, 可选) - 验证上下文
566
+
567
+ **返回**: **Promise<Object>** - 验证结果
568
+ - `valid` (**boolean**) - 是否通过
569
+ - `errors` (**Array**, 可选) - 错误列表
570
+ - `data` (**any**, 可选) - 验证通过的数据
571
+
572
+ **示例**:
573
+ ```javascript
574
+ const result = await dsl('email!').validate('user@example.com');
575
+ console.log(result.valid); // true
576
+ ```
577
+
578
+ ---
579
+
580
+ ### 静态方法
581
+
582
+ #### `dsl.match(field, map)```
583
+
584
+ 创建条件验证规则(类似 switch-case)。
585
+
586
+ **参数**:
587
+ - `field` (**string**) - 依赖的字段名
588
+ - `map` (**Object**) - 值与Schema的映射
589
+ - `[value: string]`: 对应的Schema
590
+ - `_default` (**optional**): 默认Schema
591
+
592
+ **返回**: **Object** - 内部Match结构
593
+
594
+ **示例**:
595
+ ```javascript
596
+ dsl.match('type', {
597
+ email: 'email!',
598
+ phone: 'string:11!',
599
+ _default: 'string'
600
+ })
601
+ ```
602
+
603
+ #### `dsl.if(condition, thenSchema, elseSchema)`
604
+
605
+ 创建简单的条件验证规则。
606
+
607
+ **参数**:
608
+ - `condition` (**string**) - 条件字段名
609
+ - `thenSchema` (**string|Object**) - 满足条件时的Schema
610
+ - `elseSchema` (**string|Object**, 可选) - 不满足条件时的Schema
611
+
612
+ **返回**: **Object** - 内部If结构
613
+
614
+ **示例**:
615
+ ```javascript
616
+ dsl.if('isVip', 'number:0-50', 'number:0-10')
617
+ ```
618
+
619
+ ---
620
+
621
+ ## DslBuilder 类
622
+
623
+ ### 描述
624
+
625
+ Schema 构建器类,支持链式调用添加验证规则。
626
+
627
+ ### 构造函数
628
+
629
+ ```javascript
630
+ new DslBuilder(dslString: string)
631
+ ```
632
+
633
+ **参数**:
634
+ - `dslString` (**string**) - DSL字符串,如 `'string:3-32!'`
635
+
636
+ ### 方法
637
+
638
+ #### `.pattern(regex, message?)`
639
+
640
+ 添加正则表达式验证。
641
+
642
+ **参数**:
643
+ - `regex` (**RegExp** | **string**) - 正则表达式
644
+ - `message` (**string**, 可选) - 自定义错误消息
645
+
646
+ **返回**: **DslBuilder**
647
+
648
+ **示例**:
649
+ ```javascript
650
+ dsl('string:3-32!')
651
+ .pattern(/^[a-zA-Z0-9_]+$/, '只能包含字母、数字和下划线')
652
+ ```
653
+
654
+ ---
655
+
656
+ #### `.label(text)`
657
+
658
+ 设置字段标签(用于错误消息)。
659
+
660
+ **参数**:
661
+ - `text` (**string**) - 标签文本
662
+
663
+ **返回**: **DslBuilder**
664
+
665
+ **示例**:
666
+ ```javascript
667
+ dsl('email!').label('邮箱地址')
668
+ ```
669
+
670
+ ---
671
+
672
+ #### `.messages(messages)`
673
+
674
+ 自定义错误消息。
675
+
676
+ **参数**:
677
+ - `messages` (**Object**) - 错误消息对象
678
+ - 键:错误代码(如 `'string.min'`)
679
+ - 值:错误消息模板
680
+
681
+ **返回**: **DslBuilder**
682
+
683
+ **示例**:
684
+ ```javascript
685
+ dsl('string:3-32!')
686
+ .messages({
687
+ 'string.min': '至少{{#limit}}个字符',
688
+ 'string.max': '最多{{#limit}}个字符'
689
+ })
690
+ ```
691
+
692
+ ---
693
+
694
+ #### `.description(text)`
695
+
696
+ 设置字段描述。
697
+
698
+ **参数**:
699
+ - `text` (**string**) - 描述文本
700
+
701
+ **返回**: **DslBuilder**
702
+
703
+ **示例**:
704
+ ```javascript
705
+ dsl('url').description('个人主页链接')
706
+ ```
707
+
708
+ ---
709
+
710
+ #### `.custom(validator)`
711
+
712
+ 添加自定义验证器。
713
+
714
+ **参数**:
715
+ - `validator` (**Function**) - 验证函数
716
+ - 签名:`(value) => boolean | Promise<boolean> | { error, message }`
717
+ - 返回 `true` 表示通过
718
+ - 返回 `false` 或错误对象表示失败
719
+
720
+ **返回**: **DslBuilder**
721
+
722
+ **示例**:
723
+ ```javascript
724
+ dsl('string:3-32!')
725
+ .custom(async (value) => {
726
+ const exists = await checkUsernameExists(value);
727
+ if (exists) {
728
+ return { error: 'username.exists', message: '用户名已存在' };
729
+ }
730
+ return true;
731
+ })
732
+ ```
733
+
734
+ ---
735
+
736
+ #### `.default(value)`
737
+
738
+ 设置默认值。
739
+
740
+ **参数**:
741
+ - `value` (**any**) - 默认值
742
+
743
+ **返回**: **DslBuilder**
744
+
745
+ **示例**:
746
+ ```javascript
747
+ dsl('string').default('guest')
748
+ ```
749
+
750
+ ---
751
+
752
+ #### `.username(preset?)`
753
+
754
+ 用户名验证(自动设置长度和正则)。
755
+
756
+ **参数**:
757
+ - `preset` (**string** | **Object**, 可选) - 预设配置
758
+ - 字符串:`'short'` | `'medium'` | `'long'` | `'5-20'`
759
+ - 对象:`{ minLength, maxLength, allowUnderscore, allowNumber }`
760
+ - 默认值:`'medium'` (3-32位)
761
+
762
+ **返回**: **DslBuilder**
763
+
764
+ **示例**:
765
+ ```javascript
766
+ // 默认 medium (3-32位)
767
+ dsl('string!').username()
768
+
769
+ // 自定义范围
770
+ dsl('string!').username('5-20')
771
+
772
+ // 使用预设
773
+ dsl('string!').username('short') // 3-16位
774
+ ```
775
+
776
+ ---
777
+
778
+ #### `.password(strength?)`
779
+
780
+ 密码强度验证(自动设置长度和正则)。
781
+
782
+ **参数**:
783
+ - `strength` (**string**, 可选) - 强度级别
784
+ - `'weak'` - 最少6位
785
+ - `'medium'` - 8位,字母+数字(默认)
786
+ - `'strong'` - 8位,大小写+数字
787
+ - `'veryStrong'` - 10位,大小写+数字+特殊字符
788
+
789
+ **返回**: **DslBuilder**
790
+
791
+ **示例**:
792
+ ```javascript
793
+ dsl('string!').password('strong')
794
+ ```
795
+
796
+ ---
797
+
798
+ #### `.phone(country?)`
799
+
800
+ 手机号验证(自动设置长度和正则)。
801
+
802
+ **参数**:
803
+ - `country` (**string**, 可选) - 国家代码
804
+ - `'cn'` - 中国(默认)
805
+ - `'us'` - 美国
806
+ - `'uk'` - 英国
807
+ - `'hk'` - 香港
808
+ - `'tw'` - 台湾
809
+ - `'international'` - 国际格式
810
+
811
+ **返回**: **DslBuilder**
812
+
813
+ **注意**: 自动将类型纠正为 `string`(即使写成 `number` 也会自动修正)
814
+
815
+ **示例**:
816
+ ```javascript
817
+ // 推荐写法
818
+ dsl('string!').phone('cn')
819
+
820
+ // 自动纠正:number → string
821
+ dsl('number!').phone('cn') // 自动纠正为 string
822
+ ```
823
+
824
+ ---
825
+
826
+ #### `.toSchema()`
827
+
828
+ 转换为 JSON Schema 对象。
829
+
830
+ **返回**: **Object** - JSON Schema对象
831
+
832
+ **示例**:
833
+ ```javascript
834
+ const schema = dsl('email!').label('邮箱').toSchema();
835
+ // { type: 'string', format: 'email', _label: '邮箱', _required: true }
836
+ ```
837
+
838
+ ---
839
+
840
+ #### `.validate(data, context?)`
841
+
842
+ 验证数据(便捷方法)。
843
+
844
+ **参数**:
845
+ - `data` (**any**) - 待验证数据
846
+ - `context` (**Object**, 可选) - 验证上下文
847
+
848
+ **返回**: **Promise<Object>** - 验证结果
849
+ - `valid` (**boolean**) - 是否通过
850
+ - `errors` (**Array**, 可选) - 错误列表
851
+ - `data` (**any**, 可选) - 验证通过的数据
852
+
853
+ **示例**:
854
+ ```javascript
855
+ const result = await dsl('email!').validate('user@example.com');
856
+ console.log(result.valid); // true
857
+ ```
858
+
859
+ ---
860
+
861
+ ### 静态方法
862
+
863
+ #### `dsl.match(field, map)```
864
+
865
+ 创建条件验证规则(类似 switch-case)。
866
+
867
+ **参数**:
868
+ - `field` (**string**) - 依赖的字段名
869
+ - `map` (**Object**) - 值与Schema的映射
870
+ - `[value: string]`: 对应的Schema
871
+ - `_default` (**optional**): 默认Schema
872
+
873
+ **返回**: **Object** - 内部Match结构
874
+
875
+ **示例**:
876
+ ```javascript
877
+ dsl.match('type', {
878
+ email: 'email!',
879
+ phone: 'string:11!',
880
+ _default: 'string'
881
+ })
882
+ ```
883
+
884
+ #### `dsl.if(condition, thenSchema, elseSchema)`
885
+
886
+ 创建简单的条件验证规则。
887
+
888
+ **参数**:
889
+ - `condition` (**string**) - 条件字段名
890
+ - `thenSchema` (**string|Object**) - 满足条件时的Schema
891
+ - `elseSchema` (**string|Object**, 可选) - 不满足条件时的Schema
892
+
893
+ **返回**: **Object** - 内部If结构
894
+
895
+ **示例**:
896
+ ```javascript
897
+ dsl.if('isVip', 'number:0-50', 'number:0-10')
898
+ ```
899
+
900
+ ---
901
+
902
+ ## 导出器
903
+
904
+ ### MongoDBExporter
905
+
906
+ 导出为 MongoDB 验证Schema。
907
+
908
+ ```javascript
909
+ const { MongoDBExporter } = require('schema-dsl');
910
+
911
+ const exporter = new MongoDBExporter({ strict: true });
912
+ const mongoSchema = exporter.export(jsonSchema);
913
+ const command = exporter.generateCommand('users', jsonSchema);
914
+ ```
915
+
916
+ **方法**:
917
+ - `export(schema)` - 导出为MongoDB Schema
918
+ - `generateCommand(collection, schema)` - 生成 createCollection 命令
919
+
920
+ ---
921
+
922
+ ### MySQLExporter
923
+
924
+ 导出为 MySQL DDL。
925
+
926
+ ```javascript
927
+ const { MySQLExporter } = require('schema-dsl');
928
+
929
+ const exporter = new MySQLExporter();
930
+ const ddl = exporter.export(jsonSchema, { tableName: 'users' });
931
+ ```
932
+
933
+ **方法**:
934
+ - `export(schema, options)` - 导出为MySQL DDL
935
+
936
+ ---
937
+
938
+ ### PostgreSQLExporter
939
+
940
+ 导出为 PostgreSQL DDL。
941
+
942
+ ```javascript
943
+ const { PostgreSQLExporter } = require('schema-dsl');
944
+
945
+ const exporter = new PostgreSQLExporter();
946
+ const ddl = exporter.export(jsonSchema, { tableName: 'users' });
947
+ ```
948
+
949
+ **方法**:
950
+ - `export(schema, options)` - 导出为PostgreSQL DDL
951
+
952
+ ---
953
+
954
+ ## 工具函数
955
+
956
+ ### TypeConverter
957
+
958
+ 类型转换工具。
959
+
960
+ ```javascript
961
+ const { TypeConverter } = require('schema-dsl');
962
+
963
+ TypeConverter.toJSONSchema(dslSchema);
964
+ ```
965
+
966
+ ---
967
+
968
+ ### SchemaHelper
969
+
970
+ Schema辅助工具。
971
+
972
+ ```javascript
973
+ const { SchemaHelper } = require('schema-dsl');
974
+
975
+ SchemaHelper.merge(schema1, schema2);
976
+ SchemaHelper.clone(schema);
977
+ ```
978
+
979
+ ---
980
+
981
+ ## DSL 语法快速参考
982
+
983
+ ### 基本类型
984
+
985
+ ```
986
+ string, number, integer, boolean
987
+ email, url, uuid, date, datetime
988
+ ```
989
+
990
+ ### 约束
991
+
992
+ ```
993
+ string:min-max # 字符串长度
994
+ number:min-max # 数字范围
995
+ value1|value2 # 枚举
996
+ ! # 必填
997
+ ```
998
+
999
+ ### 数组
1000
+
1001
+ ```
1002
+ array<type> # 数组
1003
+ array<string:1-50> # 带约束的数组元素
1004
+ ```
1005
+
1006
+ ### 示例
1007
+
1008
+ ```javascript
1009
+ 'string:3-32!' // 必填字符串,长度3-32
1010
+ 'email!' // 必填邮箱
1011
+ 'number:18-120' // 可选数字,范围18-120
1012
+ 'active|inactive|pending' // 枚举
1013
+ 'array<string:1-20>' // 字符串数组
1014
+ ```
1015
+
1016
+ ---
1017
+
1018
+ ## 常量
1019
+
1020
+ ### ErrorCodes
1021
+
1022
+ 错误代码常量。
1023
+
1024
+ ```javascript
1025
+ const { ErrorCodes } = require('schema-dsl');
1026
+
1027
+ console.log(ErrorCodes.STRING_MIN); // 'string.min'
1028
+ console.log(ErrorCodes.NUMBER_RANGE); // 'number.range'
1029
+ ```
1030
+
1031
+ ---
1032
+
1033
+ ### Locale
1034
+
1035
+ 多语言支持。
1036
+
1037
+ ```javascript
1038
+ const { Locale } = require('schema-dsl');
1039
+
1040
+ Locale.setLocale('zh-CN'); // 设置中文
1041
+ Locale.setLocale('en-US'); // 设置英文
1042
+ ```
1043
+
1044
+ ---
1045
+
1046
+ ## 完整示例
1047
+
1048
+ ```javascript
1049
+ const { dsl, Validator } = require('schema-dsl');
1050
+
1051
+ // 定义Schema(使用String扩展)
1052
+ const userSchema = dsl({
1053
+ username: 'string:3-32!'
1054
+ .pattern(/^[a-zA-Z0-9_]+$/)
1055
+ .messages({
1056
+ 'string.pattern': '只能包含字母、数字和下划线'
1057
+ })
1058
+ .label('用户名'),
1059
+
1060
+ email: 'email!'
1061
+ .label('邮箱地址'),
1062
+
1063
+ password: 'string:8-64!'
1064
+ .pattern(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$/)
1065
+ .label('密码'),
1066
+
1067
+ age: 'number:18-120',
1068
+ role: 'user|admin|moderator'
1069
+ });
1070
+
1071
+ // 验证数据
1072
+ const validator = new Validator();
1073
+ const result = validator.validate(userSchema, {
1074
+ username: 'john_doe',
1075
+ email: 'john@example.com',
1076
+ password: 'Password123',
1077
+ age: 25,
1078
+ role: 'user'
1079
+ });
1080
+
1081
+ console.log(result.valid); // true
1082
+ ```
1083
+
1084
+ ---
1085
+
1086
+ ## 更多资源
1087
+
1088
+ - [DSL 语法完整指南](./dsl-syntax.md)
1089
+ - [错误处理](./error-handling.md)
1090
+ - [示例代码](../examples/)
1091
+ - [GitHub](https://github.com/yourname/schema-dsl)
1092
+
1093
+ ---
1094
+
1095
+ **最后更新**: 2025-12-29
1096
+