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.
- package/CHANGELOG.md +87 -210
- package/README.md +391 -2249
- package/dist/DslBuilder-DQDN0ZxZ.d.cts +341 -0
- package/dist/DslBuilder-DkLaOo9Q.d.ts +341 -0
- package/dist/Validator-C7GsVQOH.d.cts +192 -0
- package/dist/Validator-hFWKGxir.d.ts +192 -0
- package/dist/index.cjs +6594 -0
- package/dist/index.d.cts +1145 -0
- package/dist/index.d.ts +1145 -0
- package/dist/index.js +6528 -0
- package/dist/plugin-CIKtTMtS.d.cts +246 -0
- package/dist/plugin-CIKtTMtS.d.ts +246 -0
- package/dist/plugins/custom-format.cjs +3802 -0
- package/dist/plugins/custom-format.d.cts +12 -0
- package/dist/plugins/custom-format.d.ts +12 -0
- package/dist/plugins/custom-format.js +3772 -0
- package/dist/plugins/custom-type-example.cjs +3795 -0
- package/dist/plugins/custom-type-example.d.cts +8 -0
- package/dist/plugins/custom-type-example.d.ts +8 -0
- package/dist/plugins/custom-type-example.js +3765 -0
- package/dist/plugins/custom-validator.cjs +146 -0
- package/dist/plugins/custom-validator.d.cts +10 -0
- package/dist/plugins/custom-validator.d.ts +10 -0
- package/dist/plugins/custom-validator.js +121 -0
- package/docs/FEATURE-INDEX.md +102 -68
- package/docs/add-custom-locale.md +48 -35
- package/docs/add-keyword.md +24 -0
- package/docs/api-reference.md +396 -154
- package/docs/api.md +13 -0
- package/docs/best-practices-project-structure.md +19 -10
- package/docs/best-practices.md +93 -53
- package/docs/cache-manager.md +23 -15
- package/docs/compile.md +45 -0
- package/docs/conditional-api.md +40 -11
- package/docs/custom-extensions-guide.md +80 -152
- package/docs/design-philosophy.md +76 -71
- package/docs/doc-index.md +324 -0
- package/docs/dsl-syntax.md +69 -19
- package/docs/dynamic-locale.md +24 -14
- package/docs/enum.md +12 -5
- package/docs/error-handling.md +53 -44
- package/docs/export-guide.md +47 -8
- package/docs/export-limitations.md +27 -11
- package/docs/faq.md +86 -67
- package/docs/frontend-i18n-guide.md +26 -12
- package/docs/i18n-user-guide.md +60 -47
- package/docs/i18n.md +51 -32
- package/docs/index.md +48 -0
- package/docs/json-schema-basics.md +40 -0
- package/docs/label-vs-description.md +12 -3
- package/docs/markdown-exporter.md +15 -6
- package/docs/mongodb-exporter.md +11 -4
- package/docs/multi-language.md +26 -0
- package/docs/multi-type-support.md +26 -33
- package/docs/mysql-exporter.md +9 -2
- package/docs/number-operators.md +12 -5
- package/docs/optional-marker-guide.md +28 -23
- package/docs/performance-guide.md +49 -0
- package/docs/plugin-system.md +205 -366
- package/docs/plugin-type-registration.md +34 -0
- package/docs/postgresql-exporter.md +9 -2
- package/docs/public/favicon.svg +5 -0
- package/docs/quick-start.md +37 -363
- package/docs/runtime-locale-support.md +20 -9
- package/docs/schema-helper.md +10 -5
- package/docs/schema-utils-advanced-issues.md +23 -0
- package/docs/schema-utils-best-practices.md +20 -0
- package/docs/schema-utils-chaining.md +7 -0
- package/docs/schema-utils.md +76 -42
- package/docs/security-checklist.md +20 -0
- package/docs/string-extensions.md +17 -9
- package/docs/troubleshooting.md +36 -21
- package/docs/type-converter.md +41 -50
- package/docs/type-reference.md +38 -15
- package/docs/typescript-guide.md +53 -42
- package/docs/union-type-guide.md +11 -1
- package/docs/union-types.md +10 -3
- package/docs/validate-async.md +36 -25
- package/docs/validate-batch.md +49 -0
- package/docs/validate-dsl-object-support.md +33 -28
- package/docs/validate.md +36 -16
- package/docs/validation-guide.md +25 -7
- package/docs/validator.md +39 -0
- package/package.json +85 -27
- package/plugins/custom-format.cjs +8 -0
- package/plugins/custom-type-example.cjs +8 -0
- package/plugins/custom-validator.cjs +8 -0
- package/src/adapters/DslAdapter.ts +111 -0
- package/src/adapters/index.ts +1 -0
- package/src/config/constants.ts +83 -0
- package/src/config/index.ts +2 -0
- package/src/config/patterns.ts +77 -0
- package/src/core/CacheManager.ts +159 -0
- package/src/core/ConditionalBuilder.ts +382 -0
- package/src/core/ConditionalRuntime.ts +28 -0
- package/src/core/ConditionalValidator.ts +255 -0
- package/src/core/DslBuilder.ts +677 -0
- package/src/core/ErrorCodes.ts +38 -0
- package/src/core/ErrorFormatter.ts +271 -0
- package/src/core/JSONSchemaCore.ts +65 -0
- package/src/core/Locale.ts +187 -0
- package/src/core/MessageTemplate.ts +42 -0
- package/src/core/ObjectDslBuilder.ts +64 -0
- package/src/core/PluginManager.ts +326 -0
- package/src/core/StringExtensions.ts +140 -0
- package/src/core/TemplateEngine.ts +44 -0
- package/src/core/Validator.ts +448 -0
- package/src/errors/I18nError.ts +159 -0
- package/src/errors/ValidationError.ts +105 -0
- package/src/exporters/BaseExporter.ts +60 -0
- package/src/exporters/MarkdownExporter.ts +305 -0
- package/src/exporters/MongoDBExporter.ts +126 -0
- package/src/exporters/MySQLExporter.ts +155 -0
- package/src/exporters/PostgreSQLExporter.ts +222 -0
- package/src/exporters/index.ts +18 -0
- package/src/index.ts +633 -0
- package/{lib/locales/en-US.js → src/locales/en-US.ts} +21 -37
- package/{lib/locales/es-ES.js → src/locales/es-ES.ts} +63 -16
- package/{lib/locales/fr-FR.js → src/locales/fr-FR.ts} +74 -27
- package/src/locales/index.ts +103 -0
- package/{lib/locales/ja-JP.js → src/locales/ja-JP.ts} +59 -17
- package/src/locales/types.ts +156 -0
- package/{lib/locales/zh-CN.js → src/locales/zh-CN.ts} +21 -38
- package/src/parser/ConstraintParser.ts +101 -0
- package/src/parser/DslParser.ts +470 -0
- package/src/parser/SchemaCompiler.ts +66 -0
- package/src/parser/TypeRegistry.ts +250 -0
- package/src/parser/index.ts +6 -0
- package/src/plugins/custom-format.ts +126 -0
- package/src/plugins/custom-type-example.ts +108 -0
- package/src/plugins/custom-validator.ts +140 -0
- package/src/types/conditional.ts +28 -0
- package/src/types/config.ts +59 -0
- package/src/types/dsl.ts +131 -0
- package/src/types/error.ts +60 -0
- package/src/types/index.ts +17 -0
- package/src/types/infer.ts +128 -0
- package/src/types/plugin.ts +58 -0
- package/src/types/safe-regex.d.ts +9 -0
- package/src/types/schema.ts +66 -0
- package/src/types/validate.ts +71 -0
- package/src/utils/SchemaHelper.ts +196 -0
- package/src/utils/SchemaUtils.ts +346 -0
- package/src/utils/TypeConverter.ts +215 -0
- package/src/utils/index.ts +10 -0
- package/src/validators/CustomKeywords.ts +477 -0
- package/.eslintignore +0 -11
- package/.eslintrc.json +0 -27
- package/CONTRIBUTING.md +0 -368
- package/STATUS.md +0 -491
- package/changelogs/v1.0.0.md +0 -328
- package/changelogs/v1.0.9.md +0 -367
- package/changelogs/v1.1.0.md +0 -389
- package/changelogs/v1.1.1.md +0 -308
- package/changelogs/v1.1.2.md +0 -183
- package/changelogs/v1.1.3.md +0 -161
- package/changelogs/v1.1.4.md +0 -432
- package/changelogs/v1.1.5.md +0 -493
- package/changelogs/v1.1.6.md +0 -211
- package/changelogs/v1.1.8.md +0 -376
- package/changelogs/v1.2.3.md +0 -124
- package/docs/INDEX.md +0 -252
- package/docs/issues-resolved-summary.md +0 -196
- package/docs/performance-benchmark-report.md +0 -179
- package/docs/performance-quick-reference.md +0 -123
- package/docs/user-questions-answered.md +0 -353
- package/docs/validation-rules-v1.0.2.md +0 -1608
- package/examples/README.md +0 -81
- package/examples/array-dsl-example.js +0 -227
- package/examples/conditional-example.js +0 -288
- package/examples/conditional-non-object.js +0 -129
- package/examples/conditional-validate-example.js +0 -321
- package/examples/custom-extension.js +0 -85
- package/examples/dsl-match-example.js +0 -74
- package/examples/dsl-style.js +0 -118
- package/examples/dynamic-locale-configuration.js +0 -348
- package/examples/dynamic-locale-example.js +0 -287
- package/examples/enum.examples.js +0 -324
- package/examples/export-demo.js +0 -130
- package/examples/express-integration.js +0 -376
- package/examples/i18n-error-handling-complete.js +0 -381
- package/examples/i18n-error-handling-quickstart.md +0 -0
- package/examples/i18n-error.examples.js +0 -181
- package/examples/i18n-full-demo.js +0 -301
- package/examples/i18n-memory-safety.examples.js +0 -268
- package/examples/markdown-export.js +0 -71
- package/examples/middleware-usage.js +0 -93
- package/examples/new-features-comparison.js +0 -315
- package/examples/password-reset/README.md +0 -153
- package/examples/password-reset/schema.js +0 -26
- package/examples/password-reset/test.js +0 -101
- package/examples/plugin-system.examples.js +0 -205
- package/examples/schema-utils-chaining.examples.js +0 -250
- package/examples/simple-example.js +0 -122
- package/examples/slug.examples.js +0 -179
- package/examples/string-extensions.js +0 -297
- package/examples/union-type-example.js +0 -127
- package/examples/union-types-example.js +0 -77
- package/examples/user-registration/README.md +0 -156
- package/examples/user-registration/routes.js +0 -92
- package/examples/user-registration/schema.js +0 -150
- package/examples/user-registration/server.js +0 -74
- package/index.d.ts +0 -3540
- package/index.js +0 -457
- package/index.mjs +0 -60
- package/lib/adapters/DslAdapter.js +0 -871
- package/lib/adapters/index.js +0 -20
- package/lib/config/constants.js +0 -286
- package/lib/config/patterns/common.js +0 -47
- package/lib/config/patterns/creditCard.js +0 -9
- package/lib/config/patterns/idCard.js +0 -9
- package/lib/config/patterns/index.js +0 -9
- package/lib/config/patterns/licensePlate.js +0 -4
- package/lib/config/patterns/passport.js +0 -4
- package/lib/config/patterns/phone.js +0 -9
- package/lib/config/patterns/postalCode.js +0 -5
- package/lib/core/CacheManager.js +0 -376
- package/lib/core/ConditionalBuilder.js +0 -503
- package/lib/core/DslBuilder.js +0 -1400
- package/lib/core/ErrorCodes.js +0 -233
- package/lib/core/ErrorFormatter.js +0 -445
- package/lib/core/JSONSchemaCore.js +0 -347
- package/lib/core/Locale.js +0 -130
- package/lib/core/MessageTemplate.js +0 -98
- package/lib/core/PluginManager.js +0 -448
- package/lib/core/StringExtensions.js +0 -240
- package/lib/core/Validator.js +0 -654
- package/lib/errors/I18nError.js +0 -328
- package/lib/errors/ValidationError.js +0 -191
- package/lib/exporters/MarkdownExporter.js +0 -420
- package/lib/exporters/MongoDBExporter.js +0 -162
- package/lib/exporters/MySQLExporter.js +0 -212
- package/lib/exporters/PostgreSQLExporter.js +0 -289
- package/lib/exporters/index.js +0 -24
- package/lib/locales/index.js +0 -8
- package/lib/utils/LRUCache.js +0 -174
- package/lib/utils/SchemaHelper.js +0 -240
- package/lib/utils/SchemaUtils.js +0 -445
- package/lib/utils/TypeConverter.js +0 -245
- package/lib/utils/index.js +0 -13
- package/lib/validators/CustomKeywords.js +0 -616
- package/lib/validators/index.js +0 -11
package/docs/api-reference.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# schema-dsl API 参考文档
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
> **更新时间**:
|
|
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 |
|
|
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(
|
|
163
|
-
|
|
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
|
-
#### `.
|
|
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
|
-
- `
|
|
487
|
+
- `options` (**Object**, 可选) - Validator 配置项
|
|
344
488
|
|
|
345
489
|
### 方法
|
|
346
490
|
|
|
347
|
-
#### `.
|
|
491
|
+
#### `.compile(schema, cacheKey?)`
|
|
348
492
|
|
|
349
|
-
|
|
493
|
+
编译 Schema 为 AJV 验证函数。
|
|
350
494
|
|
|
351
495
|
**参数**:
|
|
352
|
-
- `
|
|
353
|
-
- `
|
|
496
|
+
- `schema` (**Object**) - JSON Schema
|
|
497
|
+
- `cacheKey` (**string** | **null**, 可选) - 缓存键
|
|
354
498
|
|
|
355
|
-
**返回**: **
|
|
499
|
+
**返回**: **Function** - AJV 验证函数
|
|
356
500
|
|
|
357
501
|
**示例**:
|
|
358
502
|
```javascript
|
|
359
|
-
|
|
360
|
-
|
|
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
|
-
#### `.
|
|
510
|
+
#### `.validate(schema, data, options?)`
|
|
366
511
|
|
|
367
|
-
|
|
512
|
+
同步验证。
|
|
368
513
|
|
|
369
514
|
**参数**:
|
|
370
|
-
- `
|
|
515
|
+
- `schema` (**Object** | **Function**) - JSON Schema 或已编译的验证函数
|
|
516
|
+
- `data` (**any**) - 待验证数据
|
|
517
|
+
- `options` (**Object**, 可选) - 验证选项
|
|
371
518
|
|
|
372
|
-
**返回**: **
|
|
519
|
+
**返回**: **Object**
|
|
520
|
+
- `valid` (**boolean**) - 是否通过
|
|
521
|
+
- `errors` (**Array**, 可选) - 错误列表
|
|
522
|
+
- `data` (**any**, 可选) - 经过处理后的数据
|
|
373
523
|
|
|
374
524
|
**示例**:
|
|
375
525
|
```javascript
|
|
376
|
-
|
|
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
|
-
#### `.
|
|
533
|
+
#### `.validateAsync(schema, data, options?)`
|
|
382
534
|
|
|
383
|
-
|
|
535
|
+
异步验证。验证失败时抛出 `ValidationError`。
|
|
384
536
|
|
|
385
537
|
**参数**:
|
|
386
|
-
- `
|
|
387
|
-
|
|
388
|
-
|
|
538
|
+
- `schema` (**Object** | **Function**) - JSON Schema 或已编译的验证函数
|
|
539
|
+
- `data` (**any**) - 待验证数据
|
|
540
|
+
- `options` (**Object**, 可选) - 验证选项
|
|
389
541
|
|
|
390
|
-
**返回**: **
|
|
542
|
+
**返回**: **Promise<any>** - 验证通过后的数据
|
|
391
543
|
|
|
392
544
|
**示例**:
|
|
393
545
|
```javascript
|
|
394
|
-
|
|
395
|
-
|
|
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
|
-
#### `.
|
|
552
|
+
#### `.validateBatch(schema, dataArray)`
|
|
404
553
|
|
|
405
|
-
|
|
554
|
+
批量验证。Schema 只编译一次,多次复用。
|
|
406
555
|
|
|
407
556
|
**参数**:
|
|
408
|
-
- `
|
|
557
|
+
- `schema` (**Object**) - JSON Schema
|
|
558
|
+
- `dataArray` (**Array**) - 待验证数据数组
|
|
409
559
|
|
|
410
|
-
**返回**: **
|
|
560
|
+
**返回**: **Array<Object>** - 每项对应一个验证结果
|
|
411
561
|
|
|
412
562
|
**示例**:
|
|
413
563
|
```javascript
|
|
414
|
-
|
|
564
|
+
const validator = new Validator();
|
|
565
|
+
const results = validator.validateBatch(schema, records);
|
|
415
566
|
```
|
|
416
567
|
|
|
417
568
|
---
|
|
418
569
|
|
|
419
|
-
#### `.
|
|
570
|
+
#### `.addKeyword(keyword, definition)`
|
|
420
571
|
|
|
421
|
-
|
|
572
|
+
添加 AJV 自定义关键字。
|
|
422
573
|
|
|
423
574
|
**参数**:
|
|
424
|
-
- `
|
|
425
|
-
|
|
426
|
-
- 返回 `true` 表示通过
|
|
427
|
-
- 返回 `false` 或错误对象表示失败
|
|
575
|
+
- `keyword` (**string**) - 关键字名称
|
|
576
|
+
- `definition` (**Object**) - AJV 关键字定义
|
|
428
577
|
|
|
429
|
-
**返回**: **
|
|
578
|
+
**返回**: **Validator**
|
|
430
579
|
|
|
431
580
|
**示例**:
|
|
432
581
|
```javascript
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
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
|
-
#### `.
|
|
591
|
+
#### `.addFormat(name, validator)`
|
|
446
592
|
|
|
447
|
-
|
|
593
|
+
添加 AJV 自定义格式。
|
|
448
594
|
|
|
449
595
|
**参数**:
|
|
450
|
-
- `
|
|
596
|
+
- `name` (**string**) - 格式名称
|
|
597
|
+
- `validator` (**Function** | **Object**) - AJV format 定义
|
|
451
598
|
|
|
452
|
-
**返回**: **
|
|
599
|
+
**返回**: **Validator**
|
|
453
600
|
|
|
454
601
|
**示例**:
|
|
455
602
|
```javascript
|
|
456
|
-
|
|
603
|
+
const validator = new Validator();
|
|
604
|
+
validator.addFormat('phone-cn', /^1[3-9]\d{9}$/);
|
|
457
605
|
```
|
|
458
606
|
|
|
459
607
|
---
|
|
460
608
|
|
|
461
|
-
#### `.
|
|
609
|
+
#### `.addSchema(uri, schema)`
|
|
462
610
|
|
|
463
|
-
|
|
611
|
+
添加 schema 引用。
|
|
464
612
|
|
|
465
613
|
**参数**:
|
|
466
|
-
- `
|
|
467
|
-
|
|
468
|
-
- 对象:`{ minLength, maxLength, allowUnderscore, allowNumber }`
|
|
469
|
-
- 默认值:`'medium'` (3-32位)
|
|
614
|
+
- `uri` (**string**) - schema 标识
|
|
615
|
+
- `schema` (**Object**) - JSON Schema
|
|
470
616
|
|
|
471
|
-
**返回**: **
|
|
617
|
+
**返回**: **Validator**
|
|
472
618
|
|
|
473
619
|
**示例**:
|
|
474
620
|
```javascript
|
|
475
|
-
|
|
476
|
-
|
|
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
|
-
#### `.
|
|
627
|
+
#### `.removeSchema(uri)`
|
|
488
628
|
|
|
489
|
-
|
|
629
|
+
删除 schema 引用。
|
|
490
630
|
|
|
491
631
|
**参数**:
|
|
492
|
-
- `
|
|
493
|
-
- `'weak'` - 最少6位
|
|
494
|
-
- `'medium'` - 8位,字母+数字(默认)
|
|
495
|
-
- `'strong'` - 8位,大小写+数字
|
|
496
|
-
- `'veryStrong'` - 10位,大小写+数字+特殊字符
|
|
632
|
+
- `uri` (**string**) - schema 标识
|
|
497
633
|
|
|
498
|
-
**返回**: **
|
|
634
|
+
**返回**: **Validator**
|
|
499
635
|
|
|
500
636
|
**示例**:
|
|
501
637
|
```javascript
|
|
502
|
-
|
|
638
|
+
const validator = new Validator();
|
|
639
|
+
validator.removeSchema('user.schema.json');
|
|
503
640
|
```
|
|
504
641
|
|
|
505
642
|
---
|
|
506
643
|
|
|
507
|
-
#### `.
|
|
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
|
-
|
|
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
|
-
#### `.
|
|
658
|
+
#### `.clearCache()`
|
|
536
659
|
|
|
537
|
-
|
|
660
|
+
清空编译缓存。
|
|
538
661
|
|
|
539
|
-
**返回**:
|
|
662
|
+
**返回**: `void`
|
|
540
663
|
|
|
541
664
|
**示例**:
|
|
542
665
|
```javascript
|
|
543
|
-
const
|
|
544
|
-
|
|
666
|
+
const validator = new Validator();
|
|
667
|
+
validator.clearCache();
|
|
545
668
|
```
|
|
546
669
|
|
|
547
670
|
---
|
|
548
671
|
|
|
549
|
-
#### `.
|
|
550
|
-
|
|
551
|
-
验证数据(便捷方法)。
|
|
672
|
+
#### `.getCacheStats()`
|
|
552
673
|
|
|
553
|
-
|
|
554
|
-
- `data` (**any**) - 待验证数据
|
|
555
|
-
- `context` (**Object**, 可选) - 验证上下文
|
|
674
|
+
获取缓存统计信息。
|
|
556
675
|
|
|
557
|
-
**返回**: **
|
|
558
|
-
- `valid` (**boolean**) - 是否通过
|
|
559
|
-
- `errors` (**Array**, 可选) - 错误列表
|
|
560
|
-
- `data` (**any**, 可选) - 验证通过的数据
|
|
676
|
+
**返回**: **Object**
|
|
561
677
|
|
|
562
678
|
**示例**:
|
|
563
679
|
```javascript
|
|
564
|
-
const
|
|
565
|
-
console.log(
|
|
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
|
-
|
|
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
|
-
**返回**: **
|
|
692
|
+
**返回**: **Validator**
|
|
583
693
|
|
|
584
694
|
**示例**:
|
|
585
695
|
```javascript
|
|
586
|
-
|
|
587
|
-
email: 'email!',
|
|
588
|
-
phone: 'string:11!',
|
|
589
|
-
_default: 'string'
|
|
590
|
-
})
|
|
696
|
+
const validator = Validator.create();
|
|
591
697
|
```
|
|
592
698
|
|
|
593
|
-
|
|
699
|
+
---
|
|
594
700
|
|
|
595
|
-
|
|
701
|
+
#### `Validator.quickValidate(schema, data)`
|
|
702
|
+
|
|
703
|
+
快速验证。
|
|
596
704
|
|
|
597
705
|
**参数**:
|
|
598
|
-
- `
|
|
599
|
-
- `
|
|
600
|
-
- `elseSchema` (**string|Object**, 可选) - 不满足条件时的Schema
|
|
706
|
+
- `schema` (**Object**) - JSON Schema
|
|
707
|
+
- `data` (**any**) - 待验证数据
|
|
601
708
|
|
|
602
|
-
**返回**: **
|
|
709
|
+
**返回**: **boolean**
|
|
603
710
|
|
|
604
711
|
**示例**:
|
|
605
712
|
```javascript
|
|
606
|
-
|
|
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(
|
|
746
|
+
const ddl = exporter.export('users', jsonSchema);
|
|
640
747
|
```
|
|
641
748
|
|
|
642
749
|
**方法**:
|
|
643
|
-
- `export(
|
|
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(
|
|
762
|
+
const ddl = exporter.export('users', jsonSchema);
|
|
656
763
|
```
|
|
657
764
|
|
|
658
765
|
**方法**:
|
|
659
|
-
- `export(
|
|
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.
|
|
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:
|
|
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
|
-
- [示例代码](
|
|
800
|
-
- [GitHub](https://github.com/
|
|
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
|
-
**最后更新**:
|
|
1046
|
+
**最后更新**: 2026-05-08
|
|
805
1047
|
|