schema-dsl 1.2.4 → 2.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 (242) hide show
  1. package/CHANGELOG.md +87 -210
  2. package/README.md +391 -2249
  3. package/dist/DslBuilder-DQDN0ZxZ.d.cts +341 -0
  4. package/dist/DslBuilder-DkLaOo9Q.d.ts +341 -0
  5. package/dist/Validator-C7GsVQOH.d.cts +192 -0
  6. package/dist/Validator-hFWKGxir.d.ts +192 -0
  7. package/dist/index.cjs +6594 -0
  8. package/dist/index.d.cts +1145 -0
  9. package/dist/index.d.ts +1145 -0
  10. package/dist/index.js +6528 -0
  11. package/dist/plugin-CIKtTMtS.d.cts +246 -0
  12. package/dist/plugin-CIKtTMtS.d.ts +246 -0
  13. package/dist/plugins/custom-format.cjs +3802 -0
  14. package/dist/plugins/custom-format.d.cts +12 -0
  15. package/dist/plugins/custom-format.d.ts +12 -0
  16. package/dist/plugins/custom-format.js +3772 -0
  17. package/dist/plugins/custom-type-example.cjs +3795 -0
  18. package/dist/plugins/custom-type-example.d.cts +8 -0
  19. package/dist/plugins/custom-type-example.d.ts +8 -0
  20. package/dist/plugins/custom-type-example.js +3765 -0
  21. package/dist/plugins/custom-validator.cjs +146 -0
  22. package/dist/plugins/custom-validator.d.cts +10 -0
  23. package/dist/plugins/custom-validator.d.ts +10 -0
  24. package/dist/plugins/custom-validator.js +121 -0
  25. package/docs/FEATURE-INDEX.md +102 -68
  26. package/docs/add-custom-locale.md +48 -35
  27. package/docs/add-keyword.md +24 -0
  28. package/docs/api-reference.md +396 -154
  29. package/docs/api.md +13 -0
  30. package/docs/best-practices-project-structure.md +19 -10
  31. package/docs/best-practices.md +93 -53
  32. package/docs/cache-manager.md +23 -15
  33. package/docs/compile.md +45 -0
  34. package/docs/conditional-api.md +40 -11
  35. package/docs/custom-extensions-guide.md +80 -152
  36. package/docs/design-philosophy.md +76 -71
  37. package/docs/doc-index.md +324 -0
  38. package/docs/dsl-syntax.md +69 -19
  39. package/docs/dynamic-locale.md +24 -14
  40. package/docs/enum.md +12 -5
  41. package/docs/error-handling.md +53 -44
  42. package/docs/export-guide.md +47 -8
  43. package/docs/export-limitations.md +27 -11
  44. package/docs/faq.md +86 -67
  45. package/docs/frontend-i18n-guide.md +26 -12
  46. package/docs/i18n-user-guide.md +60 -47
  47. package/docs/i18n.md +51 -32
  48. package/docs/index.md +48 -0
  49. package/docs/json-schema-basics.md +40 -0
  50. package/docs/label-vs-description.md +12 -3
  51. package/docs/markdown-exporter.md +15 -6
  52. package/docs/mongodb-exporter.md +11 -4
  53. package/docs/multi-language.md +26 -0
  54. package/docs/multi-type-support.md +26 -33
  55. package/docs/mysql-exporter.md +9 -2
  56. package/docs/number-operators.md +12 -5
  57. package/docs/optional-marker-guide.md +28 -23
  58. package/docs/performance-guide.md +49 -0
  59. package/docs/plugin-system.md +205 -366
  60. package/docs/plugin-type-registration.md +34 -0
  61. package/docs/postgresql-exporter.md +9 -2
  62. package/docs/public/favicon.svg +5 -0
  63. package/docs/quick-start.md +37 -363
  64. package/docs/runtime-locale-support.md +20 -9
  65. package/docs/schema-helper.md +10 -5
  66. package/docs/schema-utils-advanced-issues.md +23 -0
  67. package/docs/schema-utils-best-practices.md +20 -0
  68. package/docs/schema-utils-chaining.md +7 -0
  69. package/docs/schema-utils.md +76 -42
  70. package/docs/security-checklist.md +20 -0
  71. package/docs/string-extensions.md +17 -9
  72. package/docs/troubleshooting.md +36 -21
  73. package/docs/type-converter.md +41 -50
  74. package/docs/type-reference.md +38 -15
  75. package/docs/typescript-guide.md +53 -42
  76. package/docs/union-type-guide.md +11 -1
  77. package/docs/union-types.md +10 -3
  78. package/docs/validate-async.md +36 -25
  79. package/docs/validate-batch.md +49 -0
  80. package/docs/validate-dsl-object-support.md +33 -28
  81. package/docs/validate.md +36 -16
  82. package/docs/validation-guide.md +25 -7
  83. package/docs/validator.md +39 -0
  84. package/package.json +85 -27
  85. package/plugins/custom-format.cjs +8 -0
  86. package/plugins/custom-type-example.cjs +8 -0
  87. package/plugins/custom-validator.cjs +8 -0
  88. package/src/adapters/DslAdapter.ts +111 -0
  89. package/src/adapters/index.ts +1 -0
  90. package/src/config/constants.ts +83 -0
  91. package/src/config/index.ts +2 -0
  92. package/src/config/patterns.ts +77 -0
  93. package/src/core/CacheManager.ts +159 -0
  94. package/src/core/ConditionalBuilder.ts +382 -0
  95. package/src/core/ConditionalRuntime.ts +28 -0
  96. package/src/core/ConditionalValidator.ts +255 -0
  97. package/src/core/DslBuilder.ts +677 -0
  98. package/src/core/ErrorCodes.ts +38 -0
  99. package/src/core/ErrorFormatter.ts +271 -0
  100. package/src/core/JSONSchemaCore.ts +65 -0
  101. package/src/core/Locale.ts +187 -0
  102. package/src/core/MessageTemplate.ts +42 -0
  103. package/src/core/ObjectDslBuilder.ts +64 -0
  104. package/src/core/PluginManager.ts +326 -0
  105. package/src/core/StringExtensions.ts +140 -0
  106. package/src/core/TemplateEngine.ts +44 -0
  107. package/src/core/Validator.ts +448 -0
  108. package/src/errors/I18nError.ts +159 -0
  109. package/src/errors/ValidationError.ts +105 -0
  110. package/src/exporters/BaseExporter.ts +60 -0
  111. package/src/exporters/MarkdownExporter.ts +305 -0
  112. package/src/exporters/MongoDBExporter.ts +126 -0
  113. package/src/exporters/MySQLExporter.ts +155 -0
  114. package/src/exporters/PostgreSQLExporter.ts +222 -0
  115. package/src/exporters/index.ts +18 -0
  116. package/src/index.ts +633 -0
  117. package/{lib/locales/en-US.js → src/locales/en-US.ts} +21 -37
  118. package/{lib/locales/es-ES.js → src/locales/es-ES.ts} +63 -16
  119. package/{lib/locales/fr-FR.js → src/locales/fr-FR.ts} +74 -27
  120. package/src/locales/index.ts +103 -0
  121. package/{lib/locales/ja-JP.js → src/locales/ja-JP.ts} +59 -17
  122. package/src/locales/types.ts +156 -0
  123. package/{lib/locales/zh-CN.js → src/locales/zh-CN.ts} +21 -38
  124. package/src/parser/ConstraintParser.ts +101 -0
  125. package/src/parser/DslParser.ts +470 -0
  126. package/src/parser/SchemaCompiler.ts +66 -0
  127. package/src/parser/TypeRegistry.ts +250 -0
  128. package/src/parser/index.ts +6 -0
  129. package/src/plugins/custom-format.ts +126 -0
  130. package/src/plugins/custom-type-example.ts +108 -0
  131. package/src/plugins/custom-validator.ts +140 -0
  132. package/src/types/conditional.ts +28 -0
  133. package/src/types/config.ts +59 -0
  134. package/src/types/dsl.ts +131 -0
  135. package/src/types/error.ts +60 -0
  136. package/src/types/index.ts +17 -0
  137. package/src/types/infer.ts +128 -0
  138. package/src/types/plugin.ts +58 -0
  139. package/src/types/safe-regex.d.ts +9 -0
  140. package/src/types/schema.ts +66 -0
  141. package/src/types/validate.ts +71 -0
  142. package/src/utils/SchemaHelper.ts +196 -0
  143. package/src/utils/SchemaUtils.ts +346 -0
  144. package/src/utils/TypeConverter.ts +215 -0
  145. package/src/utils/index.ts +10 -0
  146. package/src/validators/CustomKeywords.ts +477 -0
  147. package/.eslintignore +0 -11
  148. package/.eslintrc.json +0 -27
  149. package/CONTRIBUTING.md +0 -368
  150. package/STATUS.md +0 -491
  151. package/changelogs/v1.0.0.md +0 -328
  152. package/changelogs/v1.0.9.md +0 -367
  153. package/changelogs/v1.1.0.md +0 -389
  154. package/changelogs/v1.1.1.md +0 -308
  155. package/changelogs/v1.1.2.md +0 -183
  156. package/changelogs/v1.1.3.md +0 -161
  157. package/changelogs/v1.1.4.md +0 -432
  158. package/changelogs/v1.1.5.md +0 -493
  159. package/changelogs/v1.1.6.md +0 -211
  160. package/changelogs/v1.1.8.md +0 -376
  161. package/changelogs/v1.2.3.md +0 -124
  162. package/docs/INDEX.md +0 -252
  163. package/docs/issues-resolved-summary.md +0 -196
  164. package/docs/performance-benchmark-report.md +0 -179
  165. package/docs/performance-quick-reference.md +0 -123
  166. package/docs/user-questions-answered.md +0 -353
  167. package/docs/validation-rules-v1.0.2.md +0 -1608
  168. package/examples/README.md +0 -81
  169. package/examples/array-dsl-example.js +0 -227
  170. package/examples/conditional-example.js +0 -288
  171. package/examples/conditional-non-object.js +0 -129
  172. package/examples/conditional-validate-example.js +0 -321
  173. package/examples/custom-extension.js +0 -85
  174. package/examples/dsl-match-example.js +0 -74
  175. package/examples/dsl-style.js +0 -118
  176. package/examples/dynamic-locale-configuration.js +0 -348
  177. package/examples/dynamic-locale-example.js +0 -287
  178. package/examples/enum.examples.js +0 -324
  179. package/examples/export-demo.js +0 -130
  180. package/examples/express-integration.js +0 -376
  181. package/examples/i18n-error-handling-complete.js +0 -381
  182. package/examples/i18n-error-handling-quickstart.md +0 -0
  183. package/examples/i18n-error.examples.js +0 -181
  184. package/examples/i18n-full-demo.js +0 -301
  185. package/examples/i18n-memory-safety.examples.js +0 -268
  186. package/examples/markdown-export.js +0 -71
  187. package/examples/middleware-usage.js +0 -93
  188. package/examples/new-features-comparison.js +0 -315
  189. package/examples/password-reset/README.md +0 -153
  190. package/examples/password-reset/schema.js +0 -26
  191. package/examples/password-reset/test.js +0 -101
  192. package/examples/plugin-system.examples.js +0 -205
  193. package/examples/schema-utils-chaining.examples.js +0 -250
  194. package/examples/simple-example.js +0 -122
  195. package/examples/slug.examples.js +0 -179
  196. package/examples/string-extensions.js +0 -297
  197. package/examples/union-type-example.js +0 -127
  198. package/examples/union-types-example.js +0 -77
  199. package/examples/user-registration/README.md +0 -156
  200. package/examples/user-registration/routes.js +0 -92
  201. package/examples/user-registration/schema.js +0 -150
  202. package/examples/user-registration/server.js +0 -74
  203. package/index.d.ts +0 -3540
  204. package/index.js +0 -457
  205. package/index.mjs +0 -60
  206. package/lib/adapters/DslAdapter.js +0 -871
  207. package/lib/adapters/index.js +0 -20
  208. package/lib/config/constants.js +0 -286
  209. package/lib/config/patterns/common.js +0 -47
  210. package/lib/config/patterns/creditCard.js +0 -9
  211. package/lib/config/patterns/idCard.js +0 -9
  212. package/lib/config/patterns/index.js +0 -9
  213. package/lib/config/patterns/licensePlate.js +0 -4
  214. package/lib/config/patterns/passport.js +0 -4
  215. package/lib/config/patterns/phone.js +0 -9
  216. package/lib/config/patterns/postalCode.js +0 -5
  217. package/lib/core/CacheManager.js +0 -376
  218. package/lib/core/ConditionalBuilder.js +0 -503
  219. package/lib/core/DslBuilder.js +0 -1400
  220. package/lib/core/ErrorCodes.js +0 -233
  221. package/lib/core/ErrorFormatter.js +0 -445
  222. package/lib/core/JSONSchemaCore.js +0 -347
  223. package/lib/core/Locale.js +0 -130
  224. package/lib/core/MessageTemplate.js +0 -98
  225. package/lib/core/PluginManager.js +0 -448
  226. package/lib/core/StringExtensions.js +0 -240
  227. package/lib/core/Validator.js +0 -654
  228. package/lib/errors/I18nError.js +0 -328
  229. package/lib/errors/ValidationError.js +0 -191
  230. package/lib/exporters/MarkdownExporter.js +0 -420
  231. package/lib/exporters/MongoDBExporter.js +0 -162
  232. package/lib/exporters/MySQLExporter.js +0 -212
  233. package/lib/exporters/PostgreSQLExporter.js +0 -289
  234. package/lib/exporters/index.js +0 -24
  235. package/lib/locales/index.js +0 -8
  236. package/lib/utils/LRUCache.js +0 -174
  237. package/lib/utils/SchemaHelper.js +0 -240
  238. package/lib/utils/SchemaUtils.js +0 -445
  239. package/lib/utils/TypeConverter.js +0 -245
  240. package/lib/utils/index.js +0 -13
  241. package/lib/validators/CustomKeywords.js +0 -616
  242. package/lib/validators/index.js +0 -11
@@ -1,7 +1,7 @@
1
1
  # schema-dsl API 参考文档
2
2
 
3
3
 
4
- > **更新时间**: 2025-12-25
4
+ > **更新时间**: 2026-05-22
5
5
 
6
6
  ---
7
7
 
@@ -150,21 +150,20 @@ dsl('url').description('个人主页链接')
150
150
 
151
151
  **参数**:
152
152
  - `validator` (**Function**) - 验证函数
153
- - 签名:`(value) => boolean | Promise<boolean> | { error, message }`
153
+ - 签名:`(value) => boolean | string | { error, message } | void`
154
154
  - 返回 `true` 表示通过
155
- - 返回 `false` 或错误对象表示失败
155
+ - 返回 `false`、错误消息字符串或错误对象表示失败
156
+ - 同步验证器由 `validate()` 和 `validateAsync()` 均执行;异步验证器(返回 `Promise`)仅由 `validateAsync()` 执行,`validate()` 调用时会跳过异步 validator
156
157
 
157
158
  **返回**: **DslBuilder**
158
159
 
159
160
  **示例**:
160
161
  ```javascript
161
162
  dsl('string:3-32!')
162
- .custom(async (value) => {
163
- const exists = await checkUsernameExists(value);
164
- if (exists) {
163
+ .custom((value) => {
164
+ if (value === 'admin') {
165
165
  return { error: 'username.exists', message: '用户名已存在' };
166
166
  }
167
- return true;
168
167
  })
169
168
  ```
170
169
 
@@ -263,9 +262,9 @@ dsl('number!').phone('cn') // 自动纠正为 string
263
262
 
264
263
  #### `.toSchema()`
265
264
 
266
- 转换为 JSON Schema 对象。
265
+ 转换为 JSON Schema 对象(含内部标记)。
267
266
 
268
- **返回**: **Object** - JSON Schema对象
267
+ **返回**: **Object** - JSON Schema 对象(包含 `_required`、`_customMessages`、`_label` 等 schema-dsl 内部字段)
269
268
 
270
269
  **示例**:
271
270
  ```javascript
@@ -275,13 +274,68 @@ const schema = dsl('email!').label('邮箱').toSchema();
275
274
 
276
275
  ---
277
276
 
278
- #### `.validate(data, context?)`
277
+ #### `.toJsonSchema()` <sup>v1.2.5+</sup>
278
+
279
+ 转换为纯净的 JSON Schema 对象(无内部标记)。
280
+
281
+ 与 `toSchema()` 不同,`toJsonSchema()` 会自动清理所有 schema-dsl 内部标记:
282
+ - **下划线前缀字段**:`_required`、`_customMessages`、`_label`、`_customValidators`、`_whenConditions`
283
+ - **自定义验证关键字(直接清除)**:`alphanum`、`lowercase`、`uppercase`、`trim`、`jsonString`、`port`、`requiredAll`、`strictSchema`、`noSparse`、`includesRequired`、`dateFormat`、`dateGreater`、`dateLess`、`precision`
284
+ - **`exactLength` 特殊翻译**:不直接清除,而是转换为标准 JSON Schema `{ minLength: N, maxLength: N }`(与 v1 DslBuilder `string:N` 行为兼容)
285
+
286
+ > ⚠️ `multipleOf` 是标准 JSON Schema 字段,**不会**被清除(v2 修复了 v1 的错误行为)。
287
+
288
+ 返回的对象可直接嵌入 OpenAPI / JSON Schema 等标准文档中,无需下游再做清理。
289
+
290
+ **返回**: **Object** - 纯净的 JSON Schema 对象
291
+
292
+ **适用场景**:
293
+ - 生成 OpenAPI 文档
294
+ - 导出给外部系统消费
295
+ - 任何需要标准 JSON Schema 的场景
296
+
297
+ **示例**:
298
+ ```javascript
299
+ // 对比 toSchema() 与 toJsonSchema()
300
+ const builder = dsl('string:3-32!').label('用户名').messages({ min: '至少3个字符' });
301
+
302
+ builder.toSchema();
303
+ // { type: 'string', minLength: 3, maxLength: 32, _required: true, _label: '用户名', _customMessages: { min: '至少3个字符' } }
304
+
305
+ builder.toJsonSchema();
306
+ // { type: 'string', minLength: 3, maxLength: 32 }
307
+ // 注意:不含 _required、_label、_customMessages 等内部字段
308
+
309
+ // string:N 单值语法(exactLength → minLength + maxLength)
310
+ const exact = dsl('string:6!');
311
+ exact.toSchema();
312
+ // { type: 'string', exactLength: 6, _required: true }
313
+ exact.toJsonSchema();
314
+ // { type: 'string', minLength: 6, maxLength: 6 }
315
+ // 注:exactLength 自动翻译为标准 JSON Schema 的 minLength + maxLength(v1 兼容行为)
316
+
317
+ // enum 示例
318
+ const enumBuilder = dsl('enum:admin,user,guest!');
319
+ enumBuilder.toJsonSchema();
320
+ // { type: 'string', enum: ['admin', 'user', 'guest'] }
321
+
322
+ // 用于 OpenAPI 文档生成
323
+ const schema = dsl({
324
+ username: 'string:3-32!',
325
+ email: 'email!',
326
+ age: 'number:0-120'
327
+ });
328
+ // 遍历各字段调用 toJsonSchema() 即可获得标准 JSON Schema
329
+ ```
330
+
331
+ ---
332
+
333
+ #### `.validate(data)`
279
334
 
280
335
  验证数据(便捷方法)。
281
336
 
282
337
  **参数**:
283
338
  - `data` (**any**) - 待验证数据
284
- - `context` (**Object**, 可选) - 验证上下文
285
339
 
286
340
  **返回**: **Promise<Object>** - 验证结果
287
341
  - `valid` (**boolean**) - 是否通过
@@ -337,273 +391,326 @@ dsl.if('isVip', 'number:0-50', 'number:0-10')
337
391
 
338
392
  ---
339
393
 
394
+ ## 运行时辅助函数
395
+
396
+ ### `dsl.config(options)`
397
+
398
+ 全局配置入口,在应用启动时调用一次,设置 i18n、缓存、自定义正则和严格类型解析模式。
399
+
400
+ ```javascript
401
+ const { dsl } = require('schema-dsl');
402
+
403
+ dsl.config({
404
+ // i18n:内置语言包路径 / 内联 locale bundle / { localesPath } / { locales }
405
+ i18n: './locales',
406
+
407
+ // 默认语言(默认 'en-US')
408
+ defaultLocale: 'zh-CN',
409
+
410
+ // 编译缓存配置
411
+ cache: { maxSize: 2000, ttl: 0, enabled: true },
412
+
413
+ // strict: true → 遇到未知类型直接抛出 Error(默认 false:warn + 回退 string)
414
+ strict: true,
415
+
416
+ // 追加自定义正则规则(phone / idCard / creditCard 等)
417
+ patterns: {
418
+ phone: { us: /^\+1\d{10}$/ }
419
+ }
420
+ });
421
+ ```
422
+
423
+ **参数** (`DslConfigOptions`):
424
+
425
+ | 字段 | 类型 | 默认 | 说明 |
426
+ |------|------|------|------|
427
+ | `i18n` | `string \| object` | — | 语言包路径或内联对象 |
428
+ | `defaultLocale` | `string` | `'en-US'` | 默认语言 |
429
+ | `cache` | `CacheOptions` | — | `maxSize` / `ttl` / `enabled` / `statsEnabled` |
430
+ | `strict` | `boolean` | `false` | `true` 时解析未知 DSL 类型抛出 Error |
431
+ | `patterns` | `object` | — | 追加自定义格式正则(phone/idCard/creditCard) |
432
+
433
+ ---
434
+
435
+ ### `getDefaultValidator()`
436
+
437
+ 获取顶层 `validate()` / `validateAsync()` 便捷函数内部复用的默认 `Validator` 单例。
438
+
439
+ ```javascript
440
+ const { getDefaultValidator } = require('schema-dsl');
441
+
442
+ const validator = getDefaultValidator();
443
+ console.log(validator.getCacheStats());
444
+ ```
445
+
446
+ ---
447
+
448
+ ### `resetDefaultValidator()`
449
+
450
+ 重置顶层 `validate()` / `validateAsync()` 使用的默认 `Validator` 单例。
451
+
452
+ ```javascript
453
+ const { resetDefaultValidator } = require('schema-dsl');
454
+
455
+ resetDefaultValidator();
456
+ ```
457
+
458
+ ---
459
+
460
+ ### `installStringExtensions(dslFunction?)`
461
+
462
+ 手动安装 String 扩展。模块导入时默认已自动安装;只有在调用过 `uninstallStringExtensions()` 或需要显式恢复扩展时才需要手动执行。
463
+
464
+ ```javascript
465
+ const { installStringExtensions } = require('schema-dsl');
466
+
467
+ installStringExtensions();
468
+ ```
469
+
470
+ ---
471
+
472
+ ### `uninstallStringExtensions()`
473
+
474
+ 卸载挂载到 `String.prototype` 上的扩展方法。
475
+
476
+ ```javascript
477
+ const { uninstallStringExtensions } = require('schema-dsl');
478
+
479
+ uninstallStringExtensions();
480
+ ```
481
+
482
+ ---
483
+
340
484
  ## Validator 类
341
485
 
342
486
  **参数**:
343
- - `dslString` (**string**) - DSL字符串,如 `'string:3-32!'`
487
+ - `options` (**Object**, 可选) - Validator 配置项
344
488
 
345
489
  ### 方法
346
490
 
347
- #### `.pattern(regex, message?)`
491
+ #### `.compile(schema, cacheKey?)`
348
492
 
349
- 添加正则表达式验证。
493
+ 编译 Schema 为 AJV 验证函数。
350
494
 
351
495
  **参数**:
352
- - `regex` (**RegExp** | **string**) - 正则表达式
353
- - `message` (**string**, 可选) - 自定义错误消息
496
+ - `schema` (**Object**) - JSON Schema
497
+ - `cacheKey` (**string** | **null**, 可选) - 缓存键
354
498
 
355
- **返回**: **DslBuilder**
499
+ **返回**: **Function** - AJV 验证函数
356
500
 
357
501
  **示例**:
358
502
  ```javascript
359
- dsl('string:3-32!')
360
- .pattern(/^[a-zA-Z0-9_]+$/, '只能包含字母、数字和下划线')
503
+ const validator = new Validator();
504
+ const validate = validator.compile(schema, 'user-schema');
505
+ const ok = validate(data);
361
506
  ```
362
507
 
363
508
  ---
364
509
 
365
- #### `.label(text)`
510
+ #### `.validate(schema, data, options?)`
366
511
 
367
- 设置字段标签(用于错误消息)。
512
+ 同步验证。
368
513
 
369
514
  **参数**:
370
- - `text` (**string**) - 标签文本
515
+ - `schema` (**Object** | **Function**) - JSON Schema 或已编译的验证函数
516
+ - `data` (**any**) - 待验证数据
517
+ - `options` (**Object**, 可选) - 验证选项
371
518
 
372
- **返回**: **DslBuilder**
519
+ **返回**: **Object**
520
+ - `valid` (**boolean**) - 是否通过
521
+ - `errors` (**Array**, 可选) - 错误列表
522
+ - `data` (**any**, 可选) - 经过处理后的数据
373
523
 
374
524
  **示例**:
375
525
  ```javascript
376
- dsl('email!').label('邮箱地址')
526
+ const validator = new Validator();
527
+ const result = validator.validate(schema, payload);
528
+ console.log(result.valid);
377
529
  ```
378
530
 
379
531
  ---
380
532
 
381
- #### `.messages(messages)`
533
+ #### `.validateAsync(schema, data, options?)`
382
534
 
383
- 自定义错误消息。
535
+ 异步验证。验证失败时抛出 `ValidationError`。
384
536
 
385
537
  **参数**:
386
- - `messages` (**Object**) - 错误消息对象
387
- - 键:错误代码(如 `'string.min'`)
388
- - 值:错误消息模板
538
+ - `schema` (**Object** | **Function**) - JSON Schema 或已编译的验证函数
539
+ - `data` (**any**) - 待验证数据
540
+ - `options` (**Object**, 可选) - 验证选项
389
541
 
390
- **返回**: **DslBuilder**
542
+ **返回**: **Promise<any>** - 验证通过后的数据
391
543
 
392
544
  **示例**:
393
545
  ```javascript
394
- dsl('string:3-32!')
395
- .messages({
396
- 'string.min': '至少{{#limit}}个字符',
397
- 'string.max': '最多{{#limit}}个字符'
398
- })
546
+ const validator = new Validator();
547
+ await validator.validateAsync(schema, payload);
399
548
  ```
400
549
 
401
550
  ---
402
551
 
403
- #### `.description(text)`
552
+ #### `.validateBatch(schema, dataArray)`
404
553
 
405
- 设置字段描述。
554
+ 批量验证。Schema 只编译一次,多次复用。
406
555
 
407
556
  **参数**:
408
- - `text` (**string**) - 描述文本
557
+ - `schema` (**Object**) - JSON Schema
558
+ - `dataArray` (**Array**) - 待验证数据数组
409
559
 
410
- **返回**: **DslBuilder**
560
+ **返回**: **Array<Object>** - 每项对应一个验证结果
411
561
 
412
562
  **示例**:
413
563
  ```javascript
414
- dsl('url').description('个人主页链接')
564
+ const validator = new Validator();
565
+ const results = validator.validateBatch(schema, records);
415
566
  ```
416
567
 
417
568
  ---
418
569
 
419
- #### `.custom(validator)`
570
+ #### `.addKeyword(keyword, definition)`
420
571
 
421
- 添加自定义验证器。
572
+ 添加 AJV 自定义关键字。
422
573
 
423
574
  **参数**:
424
- - `validator` (**Function**) - 验证函数
425
- - 签名:`(value) => boolean | Promise<boolean> | { error, message }`
426
- - 返回 `true` 表示通过
427
- - 返回 `false` 或错误对象表示失败
575
+ - `keyword` (**string**) - 关键字名称
576
+ - `definition` (**Object**) - AJV 关键字定义
428
577
 
429
- **返回**: **DslBuilder**
578
+ **返回**: **Validator**
430
579
 
431
580
  **示例**:
432
581
  ```javascript
433
- dsl('string:3-32!')
434
- .custom(async (value) => {
435
- const exists = await checkUsernameExists(value);
436
- if (exists) {
437
- return { error: 'username.exists', message: '用户名已存在' };
438
- }
439
- return true;
440
- })
582
+ const validator = new Validator();
583
+ validator.addKeyword('isEven', {
584
+ type: 'number',
585
+ validate: (_schema, data) => data % 2 === 0
586
+ });
441
587
  ```
442
588
 
443
589
  ---
444
590
 
445
- #### `.default(value)`
591
+ #### `.addFormat(name, validator)`
446
592
 
447
- 设置默认值。
593
+ 添加 AJV 自定义格式。
448
594
 
449
595
  **参数**:
450
- - `value` (**any**) - 默认值
596
+ - `name` (**string**) - 格式名称
597
+ - `validator` (**Function** | **Object**) - AJV format 定义
451
598
 
452
- **返回**: **DslBuilder**
599
+ **返回**: **Validator**
453
600
 
454
601
  **示例**:
455
602
  ```javascript
456
- dsl('string').default('guest')
603
+ const validator = new Validator();
604
+ validator.addFormat('phone-cn', /^1[3-9]\d{9}$/);
457
605
  ```
458
606
 
459
607
  ---
460
608
 
461
- #### `.username(preset?)`
609
+ #### `.addSchema(uri, schema)`
462
610
 
463
- 用户名验证(自动设置长度和正则)。
611
+ 添加 schema 引用。
464
612
 
465
613
  **参数**:
466
- - `preset` (**string** | **Object**, 可选) - 预设配置
467
- - 字符串:`'short'` | `'medium'` | `'long'` | `'5-20'`
468
- - 对象:`{ minLength, maxLength, allowUnderscore, allowNumber }`
469
- - 默认值:`'medium'` (3-32位)
614
+ - `uri` (**string**) - schema 标识
615
+ - `schema` (**Object**) - JSON Schema
470
616
 
471
- **返回**: **DslBuilder**
617
+ **返回**: **Validator**
472
618
 
473
619
  **示例**:
474
620
  ```javascript
475
- // 默认 medium (3-32位)
476
- dsl('string!').username()
477
-
478
- // 自定义范围
479
- dsl('string!').username('5-20')
480
-
481
- // 使用预设
482
- dsl('string!').username('short') // 3-16位
621
+ const validator = new Validator();
622
+ validator.addSchema('user.schema.json', schema);
483
623
  ```
484
624
 
485
625
  ---
486
626
 
487
- #### `.password(strength?)`
627
+ #### `.removeSchema(uri)`
488
628
 
489
- 密码强度验证(自动设置长度和正则)。
629
+ 删除 schema 引用。
490
630
 
491
631
  **参数**:
492
- - `strength` (**string**, 可选) - 强度级别
493
- - `'weak'` - 最少6位
494
- - `'medium'` - 8位,字母+数字(默认)
495
- - `'strong'` - 8位,大小写+数字
496
- - `'veryStrong'` - 10位,大小写+数字+特殊字符
632
+ - `uri` (**string**) - schema 标识
497
633
 
498
- **返回**: **DslBuilder**
634
+ **返回**: **Validator**
499
635
 
500
636
  **示例**:
501
637
  ```javascript
502
- dsl('string!').password('strong')
638
+ const validator = new Validator();
639
+ validator.removeSchema('user.schema.json');
503
640
  ```
504
641
 
505
642
  ---
506
643
 
507
- #### `.phone(country?)`
644
+ #### `.getAjv()`
508
645
 
509
- 手机号验证(自动设置长度和正则)。
646
+ 获取底层 AJV 实例。
510
647
 
511
- **参数**:
512
- - `country` (**string**, 可选) - 国家代码
513
- - `'cn'` - 中国(默认)
514
- - `'us'` - 美国
515
- - `'uk'` - 英国
516
- - `'hk'` - 香港
517
- - `'tw'` - 台湾
518
- - `'international'` - 国际格式
519
-
520
- **返回**: **DslBuilder**
521
-
522
- **注意**: 自动将类型纠正为 `string`(即使写成 `number` 也会自动修正)
648
+ **返回**: **Ajv**
523
649
 
524
650
  **示例**:
525
651
  ```javascript
526
- // 推荐写法
527
- dsl('string!').phone('cn')
528
-
529
- // 自动纠正:number → string
530
- dsl('number!').phone('cn') // 自动纠正为 string
652
+ const validator = new Validator();
653
+ const ajv = validator.getAjv();
531
654
  ```
532
655
 
533
656
  ---
534
657
 
535
- #### `.toSchema()`
658
+ #### `.clearCache()`
536
659
 
537
- 转换为 JSON Schema 对象。
660
+ 清空编译缓存。
538
661
 
539
- **返回**: **Object** - JSON Schema对象
662
+ **返回**: `void`
540
663
 
541
664
  **示例**:
542
665
  ```javascript
543
- const schema = dsl('email!').label('邮箱').toSchema();
544
- // { type: 'string', format: 'email', _label: '邮箱', _required: true }
666
+ const validator = new Validator();
667
+ validator.clearCache();
545
668
  ```
546
669
 
547
670
  ---
548
671
 
549
- #### `.validate(data, context?)`
550
-
551
- 验证数据(便捷方法)。
672
+ #### `.getCacheStats()`
552
673
 
553
- **参数**:
554
- - `data` (**any**) - 待验证数据
555
- - `context` (**Object**, 可选) - 验证上下文
674
+ 获取缓存统计信息。
556
675
 
557
- **返回**: **Promise<Object>** - 验证结果
558
- - `valid` (**boolean**) - 是否通过
559
- - `errors` (**Array**, 可选) - 错误列表
560
- - `data` (**any**, 可选) - 验证通过的数据
676
+ **返回**: **Object**
561
677
 
562
678
  **示例**:
563
679
  ```javascript
564
- const result = await dsl('email!').validate('user@example.com');
565
- console.log(result.valid); // true
680
+ const validator = new Validator();
681
+ console.log(validator.getCacheStats());
566
682
  ```
567
683
 
568
684
  ---
569
685
 
570
- ### 静态方法
571
-
572
- #### `dsl.match(field, map)```
686
+ ### 静态方法
573
687
 
574
- 创建条件验证规则(类似 switch-case)。
688
+ #### `Validator.create(options?)`
575
689
 
576
- **参数**:
577
- - `field` (**string**) - 依赖的字段名
578
- - `map` (**Object**) - 值与Schema的映射
579
- - `[value: string]`: 对应的Schema
580
- - `_default` (**optional**): 默认Schema
690
+ 创建 `Validator` 实例。
581
691
 
582
- **返回**: **Object** - 内部Match结构
692
+ **返回**: **Validator**
583
693
 
584
694
  **示例**:
585
695
  ```javascript
586
- dsl.match('type', {
587
- email: 'email!',
588
- phone: 'string:11!',
589
- _default: 'string'
590
- })
696
+ const validator = Validator.create();
591
697
  ```
592
698
 
593
- #### `dsl.if(condition, thenSchema, elseSchema)`
699
+ ---
594
700
 
595
- 创建简单的条件验证规则。
701
+ #### `Validator.quickValidate(schema, data)`
702
+
703
+ 快速验证。
596
704
 
597
705
  **参数**:
598
- - `condition` (**string**) - 条件字段名
599
- - `thenSchema` (**string|Object**) - 满足条件时的Schema
600
- - `elseSchema` (**string|Object**, 可选) - 不满足条件时的Schema
706
+ - `schema` (**Object**) - JSON Schema
707
+ - `data` (**any**) - 待验证数据
601
708
 
602
- **返回**: **Object** - 内部If结构
709
+ **返回**: **boolean**
603
710
 
604
711
  **示例**:
605
712
  ```javascript
606
- dsl.if('isVip', 'number:0-50', 'number:0-10')
713
+ const ok = Validator.quickValidate(schema, data);
607
714
  ```
608
715
 
609
716
  ---
@@ -636,11 +743,11 @@ const command = exporter.generateCommand('users', jsonSchema);
636
743
  const { MySQLExporter } = require('schema-dsl');
637
744
 
638
745
  const exporter = new MySQLExporter();
639
- const ddl = exporter.export(jsonSchema, { tableName: 'users' });
746
+ const ddl = exporter.export('users', jsonSchema);
640
747
  ```
641
748
 
642
749
  **方法**:
643
- - `export(schema, options)` - 导出为MySQL DDL
750
+ - `export(tableName, schema)` - 导出为MySQL DDL
644
751
 
645
752
  ---
646
753
 
@@ -652,11 +759,11 @@ const ddl = exporter.export(jsonSchema, { tableName: 'users' });
652
759
  const { PostgreSQLExporter } = require('schema-dsl');
653
760
 
654
761
  const exporter = new PostgreSQLExporter();
655
- const ddl = exporter.export(jsonSchema, { tableName: 'users' });
762
+ const ddl = exporter.export('users', jsonSchema);
656
763
  ```
657
764
 
658
765
  **方法**:
659
- - `export(schema, options)` - 导出为PostgreSQL DDL
766
+ - `export(tableName, schema)` - 导出为PostgreSQL DDL
660
767
 
661
768
  ---
662
769
 
@@ -669,7 +776,8 @@ const ddl = exporter.export(jsonSchema, { tableName: 'users' });
669
776
  ```javascript
670
777
  const { TypeConverter } = require('schema-dsl');
671
778
 
672
- TypeConverter.toJSONSchema(dslSchema);
779
+ TypeConverter.toJSONSchemaType('string');
780
+ TypeConverter.toMongoDBType('integer');
673
781
  ```
674
782
 
675
783
  ---
@@ -687,19 +795,123 @@ SchemaHelper.clone(schema);
687
795
 
688
796
  ---
689
797
 
798
+ ### ErrorFormatter
799
+
800
+ 验证错误格式化工具,用于把 Ajv 原始错误数组或简化错误对象转换成统一的错误项结构或消息文本。
801
+
802
+ ```javascript
803
+ const { ErrorFormatter } = require('schema-dsl');
804
+
805
+ const formatter = new ErrorFormatter('zh-CN');
806
+ const errors = formatter.formatDetailed(ajvValidate.errors);
807
+ console.log(errors[0].message);
808
+ ```
809
+
810
+ **方法**:
811
+ - `new ErrorFormatter(locale?, messages?)` - 创建错误格式化器
812
+ - `format(error, locale?)` - 格式化单个错误为消息字符串
813
+ - `formatDetailed(errors, locale?, customMessages?, alreadyMerged?)` - 格式化错误数组为标准错误项列表
814
+
815
+ ---
816
+
817
+ ### MessageTemplate
818
+
819
+ 错误消息模板封装类,内部委托 `renderTemplate()` 执行占位符替换。
820
+
821
+ ```javascript
822
+ const { MessageTemplate } = require('schema-dsl');
823
+
824
+ const template = new MessageTemplate('{{#label}} is required');
825
+ console.log(template.render({ label: 'Email' }));
826
+ ```
827
+
828
+ **方法**:
829
+ - `new MessageTemplate(template)` - 创建模板实例
830
+ - `render(context)` - 渲染当前模板
831
+ - `MessageTemplate.render(template, context)` - 静态快速渲染
832
+ - `MessageTemplate.renderBatch(templates, context)` - 批量渲染多个模板
833
+
834
+ ---
835
+
836
+ ### renderTemplate(template, params)
837
+
838
+ 底层模板渲染函数,同时兼容 `{{#key}}` 和 `{key}` 两种占位符格式。
839
+
840
+ ```javascript
841
+ const { renderTemplate } = require('schema-dsl');
842
+
843
+ const msg = renderTemplate('{field} must be {min}~{max}', {
844
+ field: 'age',
845
+ min: 18,
846
+ max: 65,
847
+ });
848
+
849
+ console.log(msg); // age must be 18~65
850
+ ```
851
+
852
+ ---
853
+
854
+ ### JSONSchemaCore
855
+
856
+ `JSONSchemaCore` 是 v1 兼容外观类,用于以链式方式构建 JSON Schema,并可直接调用 `validate()` 做快速校验。
857
+
858
+ ```javascript
859
+ const { JSONSchemaCore } = require('schema-dsl');
860
+
861
+ const schema = new JSONSchemaCore()
862
+ .type('object')
863
+ .property('email', { type: 'string', format: 'email' })
864
+ .required('email')
865
+ .getSchema();
866
+ ```
867
+
868
+ **常用方法**:
869
+ - `type(typeName)`
870
+ - `property(name, schema)`
871
+ - `properties(properties)`
872
+ - `required(fields)`
873
+ - `format(formatName)`
874
+ - `pattern(pattern)`
875
+ - `items(schema)`
876
+ - `toSchema()` / `getSchema()`
877
+ - `validate(data)`
878
+
879
+ ---
880
+
881
+ ### 类型注册与内部解析边界
882
+
883
+ `schema-dsl` 的 root API 只保留业务可依赖的稳定入口。DSL 解析器、约束解析器、Schema 编译器和 Adapter 属于内部实现细节,不再作为 root API 文档化导出;业务代码应优先使用 `dsl()`、`DslBuilder`、`Validator` 和 `validate()`。
884
+
885
+ #### TypeRegistry
886
+
887
+ 统一类型注册表。它是自定义类型扩展的公开入口;如果只需要注册 DSL 类型,也可以优先使用更高层的 `DslBuilder.registerType()`。
888
+
889
+ - `TypeRegistry.resolve(typeName)`
890
+ - `TypeRegistry.register(name, def)`
891
+ - `TypeRegistry.registerDynamic(name, factory)`
892
+ - `TypeRegistry.unregister(name)`
893
+ - `TypeRegistry.has(typeName)`
894
+ - `TypeRegistry.getInternalKeys()`
895
+ - `TypeRegistry.toJsonSchema(schema)`
896
+ - `TypeRegistry.clearCustomTypes()` — 清空所有自定义类型(含通过 `DslAdapter.registerType()` 注册的类型),适合测试后清理状态
897
+ - `TypeRegistry.setStrict(flag)` — 设置严格模式,等效于 `dsl.config({ strict: flag })`
898
+
899
+ ---
900
+
690
901
  ## DSL 语法快速参考
691
902
 
692
903
  ### 基本类型
693
904
 
694
- ```
905
+ ```text
695
906
  string, number, integer, boolean
696
907
  email, url, uuid, date, datetime
697
908
  ```
698
909
 
699
910
  ### 约束
700
911
 
701
- ```
702
- string:min-max # 字符串长度
912
+ ```text
913
+ string:N # 精确长度(exactLength = N,等同于 minLength: N, maxLength: N)
914
+ string:min-max # 字符串长度范围
703
915
  number:min-max # 数字范围
704
916
  value1|value2 # 枚举
705
917
  ! # 必填
@@ -707,7 +919,7 @@ value1|value2 # 枚举
707
919
 
708
920
  ### 数组
709
921
 
710
- ```
922
+ ```text
711
923
  array<type> # 数组
712
924
  array<string:1-50> # 带约束的数组元素
713
925
  ```
@@ -752,6 +964,29 @@ Locale.setLocale('en-US'); // 设置英文
752
964
 
753
965
  ---
754
966
 
967
+ ### ConditionalBuilder
968
+
969
+ `dsl.if(conditionFn)` 返回的链式条件构建器,适用于运行时动态条件校验。
970
+
971
+ **常用方法**:
972
+ - `if(condition)`
973
+ - `and(condition)`
974
+ - `or(condition)`
975
+ - `elseIf(condition)`
976
+ - `message(msg)`
977
+ - `then(schema)`
978
+ - `else(schema)`
979
+ - `toSchema()`
980
+ - `build()` - `toSchema()` 的别名
981
+ - `validate(data, options?)`
982
+ - `validateAsync(data, options?)`
983
+ - `assert(data, options?)`
984
+ - `check(data)`
985
+
986
+ 更完整的示例和行为说明请参考 [conditional-api.md](./conditional-api.md)。
987
+
988
+ ---
989
+
755
990
  ## 完整示例
756
991
 
757
992
  ```javascript
@@ -796,10 +1031,17 @@ console.log(result.valid); // true
796
1031
 
797
1032
  - [DSL 语法完整指南](./dsl-syntax.md)
798
1033
  - [错误处理](./error-handling.md)
799
- - [示例代码](../examples/)
800
- - [GitHub](https://github.com/yourname/schema-dsl)
1034
+ - [示例代码](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/api-reference.ts)
1035
+ - [GitHub](https://github.com/vextjs/schema-dsl)
1036
+
1037
+ ---
1038
+
1039
+ ## 对应示例文件
1040
+
1041
+ **示例入口**: [api-reference.ts](https://github.com/vextjs/schema-dsl/blob/main/examples/docs/api-reference.ts)
1042
+ **说明**: 覆盖 `dsl()`、`validate()`、`validateAsync()`、默认 `Validator` 单例、模板渲染、`JSONSchemaCore`、`ErrorFormatter` 以及底层解析 / 编译工具的可运行调用链。
801
1043
 
802
1044
  ---
803
1045
 
804
- **最后更新**: 2025-12-29
1046
+ **最后更新**: 2026-05-08
805
1047