schema-dsl 1.0.9 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +325 -2
- package/README.md +419 -189
- package/STATUS.md +65 -3
- package/docs/FEATURE-INDEX.md +1 -1
- package/docs/best-practices.md +3 -3
- package/docs/cache-manager.md +1 -1
- package/docs/conditional-api.md +1278 -0
- package/docs/dsl-syntax.md +1 -1
- package/docs/dynamic-locale.md +2 -2
- package/docs/error-handling.md +2 -2
- package/docs/export-guide.md +2 -2
- package/docs/export-limitations.md +3 -3
- package/docs/faq.md +6 -6
- package/docs/frontend-i18n-guide.md +1 -1
- package/docs/mongodb-exporter.md +3 -3
- package/docs/multi-type-support.md +12 -2
- package/docs/mysql-exporter.md +1 -1
- package/docs/plugin-system.md +4 -4
- package/docs/postgresql-exporter.md +1 -1
- package/docs/quick-start.md +4 -4
- package/docs/troubleshooting.md +2 -2
- package/docs/type-reference.md +5 -5
- package/docs/typescript-guide.md +5 -6
- package/docs/union-type-guide.md +147 -0
- package/docs/union-types.md +277 -0
- package/docs/validate-async.md +1 -1
- package/examples/array-dsl-example.js +1 -1
- package/examples/conditional-example.js +288 -0
- package/examples/conditional-non-object.js +129 -0
- package/examples/conditional-validate-example.js +321 -0
- package/examples/i18n-error.examples.js +181 -0
- package/examples/union-type-example.js +127 -0
- package/examples/union-types-example.js +77 -0
- package/index.d.ts +655 -7
- package/index.js +54 -3
- package/lib/adapters/DslAdapter.js +14 -5
- package/lib/core/ConditionalBuilder.js +503 -0
- package/lib/core/DslBuilder.js +113 -0
- package/lib/core/Locale.js +13 -8
- package/lib/core/Validator.js +250 -2
- package/lib/errors/I18nError.js +222 -0
- package/lib/locales/en-US.js +39 -0
- package/lib/locales/es-ES.js +4 -0
- package/lib/locales/fr-FR.js +4 -0
- package/lib/locales/ja-JP.js +9 -0
- package/lib/locales/zh-CN.js +39 -0
- package/package.json +3 -1
package/index.d.ts
CHANGED
|
@@ -218,14 +218,82 @@ declare module 'schema-dsl' {
|
|
|
218
218
|
* ```
|
|
219
219
|
*/
|
|
220
220
|
export class DslBuilder {
|
|
221
|
+
/**
|
|
222
|
+
* 注册自定义类型(供插件使用)
|
|
223
|
+
* @param name - 类型名称
|
|
224
|
+
* @param schema - JSON Schema对象或生成函数
|
|
225
|
+
* @static
|
|
226
|
+
* @since v1.1.0
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* ```typescript
|
|
230
|
+
* // 注册自定义类型
|
|
231
|
+
* DslBuilder.registerType('phone-cn', {
|
|
232
|
+
* type: 'string',
|
|
233
|
+
* pattern: '^1[3-9]\\d{9}$',
|
|
234
|
+
* minLength: 11,
|
|
235
|
+
* maxLength: 11
|
|
236
|
+
* });
|
|
237
|
+
*
|
|
238
|
+
* // 在DSL中使用
|
|
239
|
+
* const schema = dsl({ phone: 'phone-cn!' });
|
|
240
|
+
* const schema2 = dsl({ contact: 'types:email|phone-cn' });
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
static registerType(name: string, schema: JSONSchema | (() => JSONSchema)): void;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* 检查类型是否已注册
|
|
247
|
+
* @param type - 类型名称
|
|
248
|
+
* @returns 是否已注册
|
|
249
|
+
* @static
|
|
250
|
+
* @since v1.1.0
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```typescript
|
|
254
|
+
* DslBuilder.hasType('string'); // true (内置)
|
|
255
|
+
* DslBuilder.hasType('phone-cn'); // false (未注册)
|
|
256
|
+
* DslBuilder.registerType('phone-cn', { ... });
|
|
257
|
+
* DslBuilder.hasType('phone-cn'); // true (已注册)
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
static hasType(type: string): boolean;
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* 获取所有已注册的自定义类型
|
|
264
|
+
* @returns 自定义类型名称数组
|
|
265
|
+
* @static
|
|
266
|
+
* @since v1.1.0
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* ```typescript
|
|
270
|
+
* const types = DslBuilder.getCustomTypes();
|
|
271
|
+
* console.log(types); // ['phone-cn', 'order-id', ...]
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
274
|
+
static getCustomTypes(): string[];
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* 清除所有自定义类型(主要用于测试)
|
|
278
|
+
* @static
|
|
279
|
+
* @since v1.1.0
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```typescript
|
|
283
|
+
* DslBuilder.clearCustomTypes();
|
|
284
|
+
* ```
|
|
285
|
+
*/
|
|
286
|
+
static clearCustomTypes(): void;
|
|
287
|
+
|
|
221
288
|
/**
|
|
222
289
|
* 构造函数
|
|
223
|
-
* @param dslString - DSL字符串(如 'email!', 'string:3-32!')
|
|
224
|
-
*
|
|
290
|
+
* @param dslString - DSL字符串(如 'email!', 'string:3-32!', 'types:string|number')
|
|
291
|
+
*
|
|
225
292
|
* @example
|
|
226
293
|
* ```typescript
|
|
227
294
|
* const builder = new DslBuilder('email!');
|
|
228
295
|
* const builder2 = new DslBuilder('string:3-32');
|
|
296
|
+
* const builder3 = new DslBuilder('types:string|number');
|
|
229
297
|
* ```
|
|
230
298
|
*/
|
|
231
299
|
constructor(dslString: string);
|
|
@@ -331,11 +399,8 @@ declare module 'schema-dsl' {
|
|
|
331
399
|
* });
|
|
332
400
|
* ```
|
|
333
401
|
*/
|
|
334
|
-
when(
|
|
335
|
-
|
|
336
|
-
then: DslBuilder | JSONSchema;
|
|
337
|
-
otherwise?: DslBuilder | JSONSchema;
|
|
338
|
-
}): this;
|
|
402
|
+
// ⚠️ DEPRECATED: .when() method removed - use dsl.if() instead
|
|
403
|
+
// when(refField: string, options: { is: any; then: DslBuilder | JSONSchema; otherwise?: DslBuilder | JSONSchema; }): this;
|
|
339
404
|
|
|
340
405
|
/**
|
|
341
406
|
* 设置默认值
|
|
@@ -1184,6 +1249,86 @@ declare module 'schema-dsl' {
|
|
|
1184
1249
|
*
|
|
1185
1250
|
* JavaScript 用户请直接使用 `dsl.if()`
|
|
1186
1251
|
* TypeScript 用户可以使用 `dsl['if']()` 或 `dsl._if()`
|
|
1252
|
+
*
|
|
1253
|
+
* @alias _if
|
|
1254
|
+
*/
|
|
1255
|
+
export const _if: (condition: any, thenSchema: any, elseSchema?: any) => any;
|
|
1256
|
+
|
|
1257
|
+
/**
|
|
1258
|
+
* 多语言错误快捷方法 (v1.1.1+)
|
|
1259
|
+
*
|
|
1260
|
+
* @description 统一的多语言错误抛出机制
|
|
1261
|
+
*
|
|
1262
|
+
* @example
|
|
1263
|
+
* ```typescript
|
|
1264
|
+
* import { dsl } from 'schema-dsl';
|
|
1265
|
+
*
|
|
1266
|
+
* // 创建错误
|
|
1267
|
+
* const error = dsl.error.create('account.notFound');
|
|
1268
|
+
*
|
|
1269
|
+
* // 直接抛出
|
|
1270
|
+
* dsl.error.throw('account.notFound');
|
|
1271
|
+
*
|
|
1272
|
+
* // 断言风格
|
|
1273
|
+
* dsl.error.assert(account, 'account.notFound');
|
|
1274
|
+
* dsl.error.assert(
|
|
1275
|
+
* account.balance >= 100,
|
|
1276
|
+
* 'account.insufficientBalance',
|
|
1277
|
+
* { balance: account.balance, required: 100 }
|
|
1278
|
+
* );
|
|
1279
|
+
* ```
|
|
1280
|
+
*/
|
|
1281
|
+
export const error: {
|
|
1282
|
+
/**
|
|
1283
|
+
* 创建多语言错误(不抛出)
|
|
1284
|
+
* @param code - 错误代码(多语言 key)
|
|
1285
|
+
* @param params - 错误参数
|
|
1286
|
+
* @param statusCode - HTTP 状态码
|
|
1287
|
+
* @returns 错误实例
|
|
1288
|
+
*/
|
|
1289
|
+
create(
|
|
1290
|
+
code: string,
|
|
1291
|
+
params?: Record<string, any>,
|
|
1292
|
+
statusCode?: number
|
|
1293
|
+
): I18nError;
|
|
1294
|
+
|
|
1295
|
+
/**
|
|
1296
|
+
* 抛出多语言错误
|
|
1297
|
+
* @param code - 错误代码(多语言 key)
|
|
1298
|
+
* @param params - 错误参数
|
|
1299
|
+
* @param statusCode - HTTP 状态码
|
|
1300
|
+
* @throws I18nError
|
|
1301
|
+
*/
|
|
1302
|
+
throw(
|
|
1303
|
+
code: string,
|
|
1304
|
+
params?: Record<string, any>,
|
|
1305
|
+
statusCode?: number
|
|
1306
|
+
): never;
|
|
1307
|
+
|
|
1308
|
+
/**
|
|
1309
|
+
* 断言方法 - 条件不满足时抛错
|
|
1310
|
+
* @param condition - 条件表达式
|
|
1311
|
+
* @param code - 错误代码(多语言 key)
|
|
1312
|
+
* @param params - 错误参数
|
|
1313
|
+
* @param statusCode - HTTP 状态码
|
|
1314
|
+
* @throws I18nError 条件为 false 时抛出
|
|
1315
|
+
*/
|
|
1316
|
+
assert(
|
|
1317
|
+
condition: any,
|
|
1318
|
+
code: string,
|
|
1319
|
+
params?: Record<string, any>,
|
|
1320
|
+
statusCode?: number
|
|
1321
|
+
): asserts condition;
|
|
1322
|
+
};
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
/**
|
|
1326
|
+
* 条件规则的别名(用于 TypeScript)
|
|
1327
|
+
*
|
|
1328
|
+
* @description 因为 TypeScript 中 `if` 是保留字,提供 `_if` 作为别名
|
|
1329
|
+
*
|
|
1330
|
+
* JavaScript 用户请直接使用 `dsl.if()`
|
|
1331
|
+
* TypeScript 用户可以使用 `dsl['if']()` 或 `dsl._if()`
|
|
1187
1332
|
*/
|
|
1188
1333
|
export { _if as if };
|
|
1189
1334
|
|
|
@@ -1507,6 +1652,161 @@ declare module 'schema-dsl' {
|
|
|
1507
1652
|
getErrorCount(): number;
|
|
1508
1653
|
}
|
|
1509
1654
|
|
|
1655
|
+
/**
|
|
1656
|
+
* I18nError - 多语言错误类
|
|
1657
|
+
*
|
|
1658
|
+
* @version 1.1.1
|
|
1659
|
+
*
|
|
1660
|
+
* @description 统一的多语言错误抛出机制,支持:
|
|
1661
|
+
* - 多语言 key 自动翻译
|
|
1662
|
+
* - 参数插值(如 {{#balance}}, {{#required}})
|
|
1663
|
+
* - 自定义错误代码
|
|
1664
|
+
* - Express/Koa 集成
|
|
1665
|
+
*
|
|
1666
|
+
* @example 基础用法
|
|
1667
|
+
* ```typescript
|
|
1668
|
+
* import { I18nError } from 'schema-dsl';
|
|
1669
|
+
*
|
|
1670
|
+
* // 直接抛出
|
|
1671
|
+
* throw I18nError.create('account.notFound');
|
|
1672
|
+
* // 中文: "账户不存在"
|
|
1673
|
+
* // 英文: "Account not found"
|
|
1674
|
+
* ```
|
|
1675
|
+
*
|
|
1676
|
+
* @example 带参数
|
|
1677
|
+
* ```typescript
|
|
1678
|
+
* I18nError.throw('account.insufficientBalance', {
|
|
1679
|
+
* balance: 50,
|
|
1680
|
+
* required: 100
|
|
1681
|
+
* });
|
|
1682
|
+
* // 输出: "余额不足,当前余额50,需要100"
|
|
1683
|
+
* ```
|
|
1684
|
+
*
|
|
1685
|
+
* @example 断言风格
|
|
1686
|
+
* ```typescript
|
|
1687
|
+
* function getAccount(id: string) {
|
|
1688
|
+
* const account = db.findAccount(id);
|
|
1689
|
+
* I18nError.assert(account, 'account.notFound');
|
|
1690
|
+
* I18nError.assert(
|
|
1691
|
+
* account.balance >= 100,
|
|
1692
|
+
* 'account.insufficientBalance',
|
|
1693
|
+
* { balance: account.balance, required: 100 }
|
|
1694
|
+
* );
|
|
1695
|
+
* return account;
|
|
1696
|
+
* }
|
|
1697
|
+
* ```
|
|
1698
|
+
*
|
|
1699
|
+
* @example Express 集成
|
|
1700
|
+
* ```typescript
|
|
1701
|
+
* app.use((error, req, res, next) => {
|
|
1702
|
+
* if (error instanceof I18nError) {
|
|
1703
|
+
* return res.status(error.statusCode).json(error.toJSON());
|
|
1704
|
+
* }
|
|
1705
|
+
* next(error);
|
|
1706
|
+
* });
|
|
1707
|
+
* ```
|
|
1708
|
+
*/
|
|
1709
|
+
export class I18nError extends Error {
|
|
1710
|
+
/** 错误名称(固定为 'I18nError') */
|
|
1711
|
+
readonly name: 'I18nError';
|
|
1712
|
+
|
|
1713
|
+
/** 错误消息(已翻译) */
|
|
1714
|
+
message: string;
|
|
1715
|
+
|
|
1716
|
+
/** 错误代码(多语言 key) */
|
|
1717
|
+
code: string;
|
|
1718
|
+
|
|
1719
|
+
/** 错误参数(用于插值) */
|
|
1720
|
+
params: Record<string, any>;
|
|
1721
|
+
|
|
1722
|
+
/** HTTP 状态码 */
|
|
1723
|
+
statusCode: number;
|
|
1724
|
+
|
|
1725
|
+
/** 使用的语言环境 */
|
|
1726
|
+
locale: string;
|
|
1727
|
+
|
|
1728
|
+
/**
|
|
1729
|
+
* 构造函数
|
|
1730
|
+
* @param code - 错误代码(多语言 key)
|
|
1731
|
+
* @param params - 错误参数(用于插值)
|
|
1732
|
+
* @param statusCode - HTTP 状态码(默认 400)
|
|
1733
|
+
* @param locale - 语言环境(默认使用当前语言)
|
|
1734
|
+
*/
|
|
1735
|
+
constructor(
|
|
1736
|
+
code: string,
|
|
1737
|
+
params?: Record<string, any>,
|
|
1738
|
+
statusCode?: number,
|
|
1739
|
+
locale?: string
|
|
1740
|
+
);
|
|
1741
|
+
|
|
1742
|
+
/**
|
|
1743
|
+
* 静态工厂方法 - 创建错误(不抛出)
|
|
1744
|
+
* @param code - 错误代码
|
|
1745
|
+
* @param params - 错误参数
|
|
1746
|
+
* @param statusCode - HTTP 状态码
|
|
1747
|
+
* @returns 错误实例
|
|
1748
|
+
*/
|
|
1749
|
+
static create(
|
|
1750
|
+
code: string,
|
|
1751
|
+
params?: Record<string, any>,
|
|
1752
|
+
statusCode?: number
|
|
1753
|
+
): I18nError;
|
|
1754
|
+
|
|
1755
|
+
/**
|
|
1756
|
+
* 静态工厂方法 - 直接抛出错误
|
|
1757
|
+
* @param code - 错误代码
|
|
1758
|
+
* @param params - 错误参数
|
|
1759
|
+
* @param statusCode - HTTP 状态码
|
|
1760
|
+
* @throws I18nError
|
|
1761
|
+
*/
|
|
1762
|
+
static throw(
|
|
1763
|
+
code: string,
|
|
1764
|
+
params?: Record<string, any>,
|
|
1765
|
+
statusCode?: number
|
|
1766
|
+
): never;
|
|
1767
|
+
|
|
1768
|
+
/**
|
|
1769
|
+
* 断言方法 - 条件不满足时抛错
|
|
1770
|
+
* @param condition - 条件表达式
|
|
1771
|
+
* @param code - 错误代码
|
|
1772
|
+
* @param params - 错误参数
|
|
1773
|
+
* @param statusCode - HTTP 状态码
|
|
1774
|
+
* @throws I18nError 条件为 false 时抛出
|
|
1775
|
+
*/
|
|
1776
|
+
static assert(
|
|
1777
|
+
condition: any,
|
|
1778
|
+
code: string,
|
|
1779
|
+
params?: Record<string, any>,
|
|
1780
|
+
statusCode?: number
|
|
1781
|
+
): asserts condition;
|
|
1782
|
+
|
|
1783
|
+
/**
|
|
1784
|
+
* 检查错误是否为指定代码
|
|
1785
|
+
* @param code - 错误代码
|
|
1786
|
+
* @returns 是否匹配
|
|
1787
|
+
*/
|
|
1788
|
+
is(code: string): boolean;
|
|
1789
|
+
|
|
1790
|
+
/**
|
|
1791
|
+
* 转为 JSON 格式(用于 API 响应)
|
|
1792
|
+
* @returns JSON 对象
|
|
1793
|
+
*/
|
|
1794
|
+
toJSON(): {
|
|
1795
|
+
error: string;
|
|
1796
|
+
code: string;
|
|
1797
|
+
message: string;
|
|
1798
|
+
params: Record<string, any>;
|
|
1799
|
+
statusCode: number;
|
|
1800
|
+
locale: string;
|
|
1801
|
+
};
|
|
1802
|
+
|
|
1803
|
+
/**
|
|
1804
|
+
* 转为字符串
|
|
1805
|
+
* @returns 格式化的错误信息
|
|
1806
|
+
*/
|
|
1807
|
+
toString(): string;
|
|
1808
|
+
}
|
|
1809
|
+
|
|
1510
1810
|
/**
|
|
1511
1811
|
* 获取默认Validator实例(单例)
|
|
1512
1812
|
*
|
|
@@ -2788,6 +3088,354 @@ declare module 'schema-dsl' {
|
|
|
2788
3088
|
*/
|
|
2789
3089
|
export const VERSION: string;
|
|
2790
3090
|
|
|
3091
|
+
/**
|
|
3092
|
+
* 链式条件构建器
|
|
3093
|
+
*
|
|
3094
|
+
* @description 提供流畅的条件判断 API,类似 JavaScript if-else 语句
|
|
3095
|
+
*
|
|
3096
|
+
* @example
|
|
3097
|
+
* ```typescript
|
|
3098
|
+
* import { dsl } from 'schema-dsl';
|
|
3099
|
+
*
|
|
3100
|
+
* // 简单条件 + 错误消息
|
|
3101
|
+
* const schema = dsl({
|
|
3102
|
+
* email: dsl.if((data) => data.age >= 18)
|
|
3103
|
+
* .message('未成年用户不能注册')
|
|
3104
|
+
* });
|
|
3105
|
+
*
|
|
3106
|
+
* // 多条件 and
|
|
3107
|
+
* const schema2 = dsl({
|
|
3108
|
+
* email: dsl.if((data) => data.age >= 18)
|
|
3109
|
+
* .and((data) => data.userType === 'admin')
|
|
3110
|
+
* .then('email!')
|
|
3111
|
+
* });
|
|
3112
|
+
*
|
|
3113
|
+
* // 多条件 or
|
|
3114
|
+
* const schema3 = dsl({
|
|
3115
|
+
* status: dsl.if((data) => data.age < 18)
|
|
3116
|
+
* .or((data) => data.isBlocked)
|
|
3117
|
+
* .message('不允许注册')
|
|
3118
|
+
* });
|
|
3119
|
+
* ```
|
|
3120
|
+
*/
|
|
3121
|
+
export class ConditionalBuilder {
|
|
3122
|
+
/**
|
|
3123
|
+
* 开始条件判断
|
|
3124
|
+
* @param condition - 条件函数,接收完整数据对象
|
|
3125
|
+
* @returns 当前实例(支持链式调用)
|
|
3126
|
+
*/
|
|
3127
|
+
if(condition: (data: any) => boolean): this;
|
|
3128
|
+
|
|
3129
|
+
/**
|
|
3130
|
+
* 添加 AND 条件(与前一个条件组合)
|
|
3131
|
+
*
|
|
3132
|
+
* @version 1.1.1 支持为每个 .and() 条件设置独立的错误消息
|
|
3133
|
+
*
|
|
3134
|
+
* @param condition - 条件函数
|
|
3135
|
+
* @returns 当前实例(支持链式调用)
|
|
3136
|
+
*
|
|
3137
|
+
* @example 基础用法(传统 AND 逻辑)
|
|
3138
|
+
* ```typescript
|
|
3139
|
+
* // 所有条件都为 true 才失败
|
|
3140
|
+
* dsl.if(d => d.age >= 18)
|
|
3141
|
+
* .and(d => d.userType === 'admin')
|
|
3142
|
+
* .then('email!')
|
|
3143
|
+
* ```
|
|
3144
|
+
*
|
|
3145
|
+
* @example v1.1.0+ 独立消息(推荐)
|
|
3146
|
+
* ```typescript
|
|
3147
|
+
* // 每个条件都有自己的错误消息
|
|
3148
|
+
* dsl.if(d => !d)
|
|
3149
|
+
* .message('ACCOUNT_NOT_FOUND')
|
|
3150
|
+
* .and(d => d.balance < 100)
|
|
3151
|
+
* .message('INSUFFICIENT_BALANCE')
|
|
3152
|
+
* .assert(account);
|
|
3153
|
+
*
|
|
3154
|
+
* // 工作原理:链式检查模式
|
|
3155
|
+
* // - 第一个条件失败 → 返回 'ACCOUNT_NOT_FOUND'
|
|
3156
|
+
* // - 第二个条件失败 → 返回 'INSUFFICIENT_BALANCE'
|
|
3157
|
+
* // - 所有条件通过 → 验证成功
|
|
3158
|
+
* ```
|
|
3159
|
+
*
|
|
3160
|
+
* @example 多个 .and() 条件
|
|
3161
|
+
* ```typescript
|
|
3162
|
+
* dsl.if(d => !d)
|
|
3163
|
+
* .message('NOT_FOUND')
|
|
3164
|
+
* .and(d => d.status !== 'active')
|
|
3165
|
+
* .message('INACTIVE')
|
|
3166
|
+
* .and(d => d.balance < 100)
|
|
3167
|
+
* .message('INSUFFICIENT')
|
|
3168
|
+
* .assert(account);
|
|
3169
|
+
* // 依次检查,第一个失败的返回其消息
|
|
3170
|
+
* ```
|
|
3171
|
+
*/
|
|
3172
|
+
and(condition: (data: any) => boolean): this;
|
|
3173
|
+
|
|
3174
|
+
/**
|
|
3175
|
+
* 添加 OR 条件(与前一个条件组合)
|
|
3176
|
+
*
|
|
3177
|
+
* @version 1.1.1 支持为 .or() 条件设置独立的错误消息
|
|
3178
|
+
*
|
|
3179
|
+
* @param condition - 条件函数
|
|
3180
|
+
* @returns 当前实例(支持链式调用)
|
|
3181
|
+
*
|
|
3182
|
+
* @example 基础用法
|
|
3183
|
+
* ```typescript
|
|
3184
|
+
* // 任一条件为 true 就失败
|
|
3185
|
+
* dsl.if((data) => data.age < 18)
|
|
3186
|
+
* .or((data) => data.isBlocked)
|
|
3187
|
+
* .message('不允许注册')
|
|
3188
|
+
* ```
|
|
3189
|
+
*
|
|
3190
|
+
* @example v1.1.0+ 独立消息
|
|
3191
|
+
* ```typescript
|
|
3192
|
+
* dsl.if(d => d.age < 18)
|
|
3193
|
+
* .message('未成年用户不能注册')
|
|
3194
|
+
* .or(d => d.isBlocked)
|
|
3195
|
+
* .message('账户已被封禁')
|
|
3196
|
+
* .assert(data);
|
|
3197
|
+
* // 哪个条件为 true 就返回哪个消息
|
|
3198
|
+
* ```
|
|
3199
|
+
*/
|
|
3200
|
+
or(condition: (data: any) => boolean): this;
|
|
3201
|
+
|
|
3202
|
+
/**
|
|
3203
|
+
* 添加 else-if 分支
|
|
3204
|
+
* @param condition - 条件函数
|
|
3205
|
+
* @returns 当前实例(支持链式调用)
|
|
3206
|
+
*/
|
|
3207
|
+
elseIf(condition: (data: any) => boolean): this;
|
|
3208
|
+
|
|
3209
|
+
/**
|
|
3210
|
+
* 设置错误消息(支持多语言 key)
|
|
3211
|
+
*
|
|
3212
|
+
* @version 1.1.1 支持为 .and() 和 .or() 条件设置独立消息
|
|
3213
|
+
*
|
|
3214
|
+
* 条件为 true 时自动抛出此错误
|
|
3215
|
+
*
|
|
3216
|
+
* @param msg - 错误消息或多语言 key
|
|
3217
|
+
* @returns 当前实例(支持链式调用)
|
|
3218
|
+
*
|
|
3219
|
+
* @example 基础用法
|
|
3220
|
+
* ```typescript
|
|
3221
|
+
* // 如果是未成年人(条件为true),抛出错误
|
|
3222
|
+
* dsl.if((data) => data.age < 18)
|
|
3223
|
+
* .message('未成年用户不能注册')
|
|
3224
|
+
* ```
|
|
3225
|
+
*
|
|
3226
|
+
* @example v1.1.0+ 为 .and() 设置独立消息
|
|
3227
|
+
* ```typescript
|
|
3228
|
+
* dsl.if((data) => !data)
|
|
3229
|
+
* .message('账户不存在')
|
|
3230
|
+
* .and((data) => data.balance < 100)
|
|
3231
|
+
* .message('余额不足')
|
|
3232
|
+
* .assert(account);
|
|
3233
|
+
* // 每个条件都有自己的错误消息
|
|
3234
|
+
* ```
|
|
3235
|
+
*
|
|
3236
|
+
* @example 链式检查模式说明
|
|
3237
|
+
* ```typescript
|
|
3238
|
+
* // 启用条件:
|
|
3239
|
+
* // 1. 使用 .message() 模式(不是 .then()/.else())
|
|
3240
|
+
* // 2. root 条件有 .message()
|
|
3241
|
+
* // 3. 有 .and() 条件
|
|
3242
|
+
* // 4. 没有 .or() 条件
|
|
3243
|
+
*
|
|
3244
|
+
* // ✅ 启用链式检查
|
|
3245
|
+
* dsl.if(d => !d).message('A').and(d => d < 100).message('B')
|
|
3246
|
+
*
|
|
3247
|
+
* // ❌ 不启用(有 .or())
|
|
3248
|
+
* dsl.if(d => !d).message('A').and(d => d < 100).or(d => d > 200).message('B')
|
|
3249
|
+
* ```
|
|
3250
|
+
*/
|
|
3251
|
+
message(msg: string): this;
|
|
3252
|
+
|
|
3253
|
+
/**
|
|
3254
|
+
* 设置满足条件时的 Schema
|
|
3255
|
+
* @param schema - DSL 字符串或 Schema 对象
|
|
3256
|
+
* @returns 当前实例(支持链式调用)
|
|
3257
|
+
*/
|
|
3258
|
+
then(schema: string | DslBuilder | JSONSchema): this;
|
|
3259
|
+
|
|
3260
|
+
/**
|
|
3261
|
+
* 设置默认 Schema(所有条件都不满足时)
|
|
3262
|
+
* 可选:不写 else 就是不验证
|
|
3263
|
+
* @param schema - DSL 字符串、Schema 对象或 null
|
|
3264
|
+
* @returns 当前实例(支持链式调用)
|
|
3265
|
+
*/
|
|
3266
|
+
else(schema: string | DslBuilder | JSONSchema | null): this;
|
|
3267
|
+
|
|
3268
|
+
/**
|
|
3269
|
+
* 快捷验证方法 - 返回完整验证结果
|
|
3270
|
+
* @param data - 待验证的数据(任意类型)
|
|
3271
|
+
* @param options - 验证选项(可选)
|
|
3272
|
+
* @returns 验证结果 { valid, errors, data }
|
|
3273
|
+
*
|
|
3274
|
+
* @example
|
|
3275
|
+
* ```typescript
|
|
3276
|
+
* // 一行代码验证
|
|
3277
|
+
* const result = dsl.if(d => d.age < 18)
|
|
3278
|
+
* .message('未成年')
|
|
3279
|
+
* .validate({ age: 16 });
|
|
3280
|
+
*
|
|
3281
|
+
* // 复用验证器
|
|
3282
|
+
* const validator = dsl.if(d => d.age < 18).message('未成年');
|
|
3283
|
+
* const r1 = validator.validate({ age: 16 });
|
|
3284
|
+
* const r2 = validator.validate({ age: 20 });
|
|
3285
|
+
* ```
|
|
3286
|
+
*/
|
|
3287
|
+
validate<T = any>(data: T, options?: ValidateOptions): ValidationResult<T>;
|
|
3288
|
+
|
|
3289
|
+
/**
|
|
3290
|
+
* 异步验证方法 - 失败自动抛出异常
|
|
3291
|
+
* @param data - 待验证的数据
|
|
3292
|
+
* @param options - 验证选项(可选)
|
|
3293
|
+
* @returns 验证通过返回数据,失败抛出异常
|
|
3294
|
+
* @throws ValidationError 验证失败抛出异常
|
|
3295
|
+
*
|
|
3296
|
+
* @example
|
|
3297
|
+
* ```typescript
|
|
3298
|
+
* // 异步验证,失败自动抛错
|
|
3299
|
+
* try {
|
|
3300
|
+
* const data = await dsl.if(d => d.age < 18)
|
|
3301
|
+
* .message('未成年')
|
|
3302
|
+
* .validateAsync({ age: 16 });
|
|
3303
|
+
* } catch (error) {
|
|
3304
|
+
* console.log(error.message);
|
|
3305
|
+
* }
|
|
3306
|
+
*
|
|
3307
|
+
* // Express 中间件
|
|
3308
|
+
* app.post('/register', async (req, res, next) => {
|
|
3309
|
+
* try {
|
|
3310
|
+
* await dsl.if(d => d.age < 18)
|
|
3311
|
+
* .message('未成年用户不能注册')
|
|
3312
|
+
* .validateAsync(req.body);
|
|
3313
|
+
* // 验证通过,继续处理...
|
|
3314
|
+
* } catch (error) {
|
|
3315
|
+
* next(error);
|
|
3316
|
+
* }
|
|
3317
|
+
* });
|
|
3318
|
+
* ```
|
|
3319
|
+
*/
|
|
3320
|
+
validateAsync<T = any>(data: T, options?: ValidateOptions): Promise<T>;
|
|
3321
|
+
|
|
3322
|
+
/**
|
|
3323
|
+
* 断言方法 - 同步验证,失败直接抛错
|
|
3324
|
+
* @param data - 待验证的数据
|
|
3325
|
+
* @param options - 验证选项(可选)
|
|
3326
|
+
* @returns 验证通过返回数据
|
|
3327
|
+
* @throws Error 验证失败抛出错误
|
|
3328
|
+
*
|
|
3329
|
+
* @example
|
|
3330
|
+
* ```typescript
|
|
3331
|
+
* // 断言验证,失败直接抛错
|
|
3332
|
+
* try {
|
|
3333
|
+
* dsl.if(d => d.age < 18)
|
|
3334
|
+
* .message('未成年')
|
|
3335
|
+
* .assert({ age: 16 });
|
|
3336
|
+
* } catch (error) {
|
|
3337
|
+
* console.log(error.message);
|
|
3338
|
+
* }
|
|
3339
|
+
*
|
|
3340
|
+
* // 函数中快速断言
|
|
3341
|
+
* function registerUser(userData: any) {
|
|
3342
|
+
* dsl.if(d => d.age < 18)
|
|
3343
|
+
* .message('未成年用户不能注册')
|
|
3344
|
+
* .assert(userData);
|
|
3345
|
+
*
|
|
3346
|
+
* // 验证通过,继续处理...
|
|
3347
|
+
* return createUser(userData);
|
|
3348
|
+
* }
|
|
3349
|
+
* ```
|
|
3350
|
+
*/
|
|
3351
|
+
assert<T = any>(data: T, options?: ValidateOptions): T;
|
|
3352
|
+
|
|
3353
|
+
/**
|
|
3354
|
+
* 快捷检查方法 - 只返回 boolean
|
|
3355
|
+
* @param data - 待验证的数据
|
|
3356
|
+
* @returns 验证是否通过
|
|
3357
|
+
*
|
|
3358
|
+
* @example
|
|
3359
|
+
* ```typescript
|
|
3360
|
+
* // 快速判断
|
|
3361
|
+
* const isValid = dsl.if(d => d.age < 18)
|
|
3362
|
+
* .message('未成年')
|
|
3363
|
+
* .check({ age: 16 });
|
|
3364
|
+
* // => false
|
|
3365
|
+
*
|
|
3366
|
+
* // 断言场景
|
|
3367
|
+
* if (!validator.check(userData)) {
|
|
3368
|
+
* console.log('验证失败');
|
|
3369
|
+
* }
|
|
3370
|
+
* ```
|
|
3371
|
+
*/
|
|
3372
|
+
check(data: any): boolean;
|
|
3373
|
+
}
|
|
3374
|
+
|
|
3375
|
+
/**
|
|
3376
|
+
* dsl 函数扩展:条件判断(支持两种方式)
|
|
3377
|
+
*/
|
|
3378
|
+
export interface DslFunction {
|
|
3379
|
+
/**
|
|
3380
|
+
* 方式一:函数条件(运行时动态判断)
|
|
3381
|
+
*
|
|
3382
|
+
* 创建链式条件构建器,在验证时根据实际数据动态判断
|
|
3383
|
+
*
|
|
3384
|
+
* @param condition - 条件函数,接收完整数据对象
|
|
3385
|
+
* @returns ConditionalBuilder 实例
|
|
3386
|
+
*
|
|
3387
|
+
* @example 简单条件 + 错误消息
|
|
3388
|
+
* ```typescript
|
|
3389
|
+
* const schema = dsl({
|
|
3390
|
+
* age: 'number!',
|
|
3391
|
+
* status: dsl.if((data) => data.age < 18)
|
|
3392
|
+
* .message('未成年用户不能注册')
|
|
3393
|
+
* });
|
|
3394
|
+
* ```
|
|
3395
|
+
*
|
|
3396
|
+
* @example 条件 + then/else(动态Schema)
|
|
3397
|
+
* ```typescript
|
|
3398
|
+
* const schema = dsl({
|
|
3399
|
+
* userType: 'string!',
|
|
3400
|
+
* email: dsl.if((data) => data.userType === 'admin')
|
|
3401
|
+
* .then('email!') // 管理员必填
|
|
3402
|
+
* .else('email') // 普通用户可选
|
|
3403
|
+
* });
|
|
3404
|
+
* ```
|
|
3405
|
+
*
|
|
3406
|
+
* @example 多条件组合
|
|
3407
|
+
* ```typescript
|
|
3408
|
+
* const schema = dsl({
|
|
3409
|
+
* email: dsl.if((data) => data.age >= 18)
|
|
3410
|
+
* .and((data) => data.userType === 'admin')
|
|
3411
|
+
* .then('email!')
|
|
3412
|
+
* .else('email')
|
|
3413
|
+
* });
|
|
3414
|
+
* ```
|
|
3415
|
+
*/
|
|
3416
|
+
if(condition: (data: any) => boolean): ConditionalBuilder;
|
|
3417
|
+
|
|
3418
|
+
/**
|
|
3419
|
+
* 方式二:字段条件(Schema 定义时静态判断)
|
|
3420
|
+
*
|
|
3421
|
+
* 基于字段值的静态布尔条件判断
|
|
3422
|
+
*
|
|
3423
|
+
* @param conditionField - 条件字段名
|
|
3424
|
+
* @param thenSchema - 条件为 true 时的 Schema
|
|
3425
|
+
* @param elseSchema - 条件为 false 时的 Schema
|
|
3426
|
+
* @returns 条件结构对象
|
|
3427
|
+
*
|
|
3428
|
+
* @example 基于字段值的条件
|
|
3429
|
+
* ```typescript
|
|
3430
|
+
* const schema = dsl({
|
|
3431
|
+
* isVip: 'boolean',
|
|
3432
|
+
* discount: dsl.if('isVip', 'number:0-50', 'number:0-10')
|
|
3433
|
+
* });
|
|
3434
|
+
* ```
|
|
3435
|
+
*/
|
|
3436
|
+
if(conditionField: string, thenSchema: string | DslBuilder | JSONSchema, elseSchema?: string | DslBuilder | JSONSchema): any;
|
|
3437
|
+
}
|
|
3438
|
+
|
|
2791
3439
|
/**
|
|
2792
3440
|
* 默认导出(dsl函数)
|
|
2793
3441
|
*
|