schema-dsl 1.0.6 → 1.0.7
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 +161 -1
- package/lib/adapters/DslAdapter.js +60 -33
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -11,8 +11,10 @@
|
|
|
11
11
|
|
|
12
12
|
| 版本 | 日期 | 变更摘要 | 详细 |
|
|
13
13
|
|------|------|---------|------|
|
|
14
|
+
| [v1.0.7](#v107) | 2026-01-04 | 修复:dsl.match/dsl.if 嵌套支持 dsl() 包裹 | [查看详情](#v107) |
|
|
15
|
+
| [v1.0.6](#v106) | 2026-01-04 | 🚨 紧急修复:TypeScript 类型污染 | [查看详情](#v106) |
|
|
16
|
+
| [v1.0.7](#v107) | 2026-01-04 | 修复:dsl.match/dsl.if 嵌套支持 dsl() 包裹 | [查看详情](#v107) |
|
|
14
17
|
| [v1.0.6](#v106) | 2026-01-04 | 🚨 紧急修复:TypeScript 类型污染 | [查看详情](#v106) |
|
|
15
|
-
| [v1.0.5](#v105) | 2026-01-04 | 测试覆盖率提升至 97% | [查看详情](#v105) |
|
|
16
18
|
| [v1.0.5](#v105) | 2026-01-04 | 测试覆盖率提升至 97% | [查看详情](#v105) |
|
|
17
19
|
| [v1.0.4](#v104) | 2025-12-31 | TypeScript 完整支持、validateAsync、ValidationError | [查看详情](#v104) |
|
|
18
20
|
| [v1.0.3](#v103) | 2025-12-31 | ⚠️ 破坏性变更:单值语法修复 | [查看详情](#v103) |
|
|
@@ -22,6 +24,164 @@
|
|
|
22
24
|
|
|
23
25
|
---
|
|
24
26
|
|
|
27
|
+
## [v1.0.7] - 2026-01-04
|
|
28
|
+
|
|
29
|
+
### Fixed (修复)
|
|
30
|
+
|
|
31
|
+
#### 全面支持 dsl.match/dsl.if 所有嵌套组合 ⭐⭐⭐
|
|
32
|
+
|
|
33
|
+
**问题描述**:
|
|
34
|
+
在 v1.0.6 中,TypeScript 用户被要求使用 `dsl()` 包裹字符串,但在 `dsl.match()` 和 `dsl.if()` 中使用嵌套结构时会失败。用户报告了复杂嵌套场景无法正常工作。
|
|
35
|
+
|
|
36
|
+
**修复内容**:
|
|
37
|
+
- ✅ **Match 嵌套 Match** - 多级条件分支
|
|
38
|
+
- ✅ **Match 嵌套 If** - 在分支中使用条件
|
|
39
|
+
- ✅ **If 嵌套 Match** - 条件中使用多分支(用户报告的场景)
|
|
40
|
+
- ✅ **If 嵌套 If** - 多层条件嵌套
|
|
41
|
+
- ✅ **_default 中嵌套** - 默认规则支持 Match/If
|
|
42
|
+
- ✅ **三层嵌套** - 支持任意深度嵌套
|
|
43
|
+
|
|
44
|
+
**修复前问题**:
|
|
45
|
+
```javascript
|
|
46
|
+
// ❌ v1.0.6 中所有嵌套都会失败
|
|
47
|
+
credit_price: dsl.if('enabled',
|
|
48
|
+
dsl.match('payment_type', {
|
|
49
|
+
'credit': dsl('integer:1-10000!').label('价格'),
|
|
50
|
+
'_default': 'integer:1-10000'
|
|
51
|
+
}),
|
|
52
|
+
'integer:1-10000'
|
|
53
|
+
)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**修复后**:
|
|
57
|
+
```javascript
|
|
58
|
+
// ✅ v1.0.7 完全支持所有嵌套组合
|
|
59
|
+
|
|
60
|
+
// 1. If 嵌套 Match(用户场景)
|
|
61
|
+
credit_price: dsl.if('enabled',
|
|
62
|
+
dsl.match('payment_type', {
|
|
63
|
+
'credit': dsl('integer:1-10000!').label('credit_price'),
|
|
64
|
+
'_default': 'integer:1-10000'
|
|
65
|
+
}),
|
|
66
|
+
'integer:1-10000'
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
// 2. Match 嵌套 Match
|
|
70
|
+
value: dsl.match('category', {
|
|
71
|
+
'contact': dsl.match('type', {
|
|
72
|
+
'email': dsl('email!').label('邮箱'),
|
|
73
|
+
'phone': dsl('string:11!').label('手机号')
|
|
74
|
+
}),
|
|
75
|
+
'payment': dsl.match('type', {
|
|
76
|
+
'credit': dsl('integer:1-10000!'),
|
|
77
|
+
'cash': dsl('number:0.01-10000!')
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
// 3. Match 嵌套 If
|
|
82
|
+
discount: dsl.match('user_type', {
|
|
83
|
+
'member': dsl.if('is_vip',
|
|
84
|
+
dsl('number:10-50!').label('VIP会员折扣'),
|
|
85
|
+
dsl('number:5-20!').label('普通会员折扣')
|
|
86
|
+
),
|
|
87
|
+
'guest': dsl('number:0-10').label('访客折扣')
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
// 4. If 嵌套 If
|
|
91
|
+
price: dsl.if('is_member',
|
|
92
|
+
dsl.if('is_premium',
|
|
93
|
+
dsl('number:100-500!').label('高级会员价'),
|
|
94
|
+
dsl('number:200-800!').label('普通会员价')
|
|
95
|
+
),
|
|
96
|
+
dsl('number:500-1000!').label('非会员价')
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
// 5. 三层嵌套
|
|
100
|
+
value: dsl.match('level1', {
|
|
101
|
+
'A': dsl.match('level2', {
|
|
102
|
+
'A1': dsl.if('level3',
|
|
103
|
+
dsl('integer:1-100!'),
|
|
104
|
+
dsl('integer:1-50!')
|
|
105
|
+
)
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
**技术细节**:
|
|
111
|
+
- 修复了 `DslAdapter._buildMatchSchema()` 方法(第476-489行)
|
|
112
|
+
- 修复了 `DslAdapter._buildIfSchema()` 方法
|
|
113
|
+
- 递归处理所有 `_isMatch` 和 `_isIf` 结构
|
|
114
|
+
- 新增 null/undefined 分支值处理逻辑
|
|
115
|
+
- 支持任意深度嵌套(已测试至5层)
|
|
116
|
+
|
|
117
|
+
**已知限制**:
|
|
118
|
+
1. **自定义验证器传递限制** - `.custom()` 验证器在嵌套 Match/If 中可能不会完全传递
|
|
119
|
+
```javascript
|
|
120
|
+
// .custom() 的自定义消息可能在深层嵌套中丢失
|
|
121
|
+
value: dsl.match('type', {
|
|
122
|
+
'email': dsl('string!').custom((v) => v.includes('@'))
|
|
123
|
+
})
|
|
124
|
+
// 基础验证(必填、类型)会生效,但 custom 验证可能不完全传递
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
2. **嵌套字段路径限制** - `dsl.match(field)` 的 field 参数不支持嵌套路径
|
|
128
|
+
```javascript
|
|
129
|
+
// ❌ 不支持
|
|
130
|
+
dsl.match('config.engine', {...})
|
|
131
|
+
|
|
132
|
+
// ✅ 支持 - 使用扁平化字段
|
|
133
|
+
dsl.match('config_engine', {...})
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
3. **自定义消息传递** - 在多层嵌套中,自定义错误消息可能不会完全保留
|
|
137
|
+
|
|
138
|
+
**测试覆盖**:
|
|
139
|
+
- 测试总数:**686 → 720**(**+34 个全面测试**)
|
|
140
|
+
- **v1.0.7 新增测试场景**:
|
|
141
|
+
|
|
142
|
+
**基础嵌套组合**(6个测试):
|
|
143
|
+
- Match 嵌套 Match - 多级条件分支
|
|
144
|
+
- Match 嵌套 If - 在分支中使用条件
|
|
145
|
+
- If 嵌套 Match - 条件中使用多分支
|
|
146
|
+
- If 嵌套 If - 多层条件嵌套
|
|
147
|
+
- _default 中使用嵌套 - 默认规则支持 Match/If
|
|
148
|
+
- 三层嵌套 - 任意深度嵌套验证
|
|
149
|
+
|
|
150
|
+
**参数验证和错误处理**(7个测试):
|
|
151
|
+
- 空 map 处理 - Match 中空映射表的行为
|
|
152
|
+
- null/undefined 分支值 - 空值分支的处理
|
|
153
|
+
- 参数验证 - match/if 参数的有效性检查
|
|
154
|
+
- 对象/数组作为条件值 - 复杂类型作为条件的验证
|
|
155
|
+
- 循环依赖检测 - 防止无限递归
|
|
156
|
+
- 同一字段多规则引用 - 字段重复使用场景
|
|
157
|
+
- undefined else 分支 - If 中省略 else 的行为
|
|
158
|
+
|
|
159
|
+
**深度嵌套和高级场景**(6个测试):
|
|
160
|
+
- 4层嵌套 - Match-Match-Match-If 四层组合
|
|
161
|
+
- 大量分支(10+)- 单个 Match 包含15个分支
|
|
162
|
+
- .custom() 验证器在嵌套中 - 自定义验证器的传递(已知限制)
|
|
163
|
+
- 复杂对象规则 - 嵌套中包含复杂对象 schema
|
|
164
|
+
- 混合嵌套 - Match 中 If,If 中 Match,再嵌套对象
|
|
165
|
+
- 5层超深嵌套 - 极端深度测试
|
|
166
|
+
|
|
167
|
+
**覆盖率提升测试**(14个测试):
|
|
168
|
+
- 错误处理和边界情况(6个)
|
|
169
|
+
- 特殊DSL语法覆盖(4个)
|
|
170
|
+
- 极端和性能测试(4个)
|
|
171
|
+
|
|
172
|
+
**代码修复**:
|
|
173
|
+
- 修复 `lib/adapters/DslAdapter.js` 中 null/undefined 分支处理
|
|
174
|
+
- 新增代码行:476-489(处理空值分支)
|
|
175
|
+
|
|
176
|
+
- **测试覆盖率**:73.12% → 73.17%(语句覆盖率)
|
|
177
|
+
- **Match/If核心功能覆盖率**:~100%(所有嵌套组合和边界情况)
|
|
178
|
+
- 所有测试通过 ✅
|
|
179
|
+
|
|
180
|
+
**迁移指南**:
|
|
181
|
+
无需任何代码修改,所有嵌套场景现在都自动支持。
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
25
185
|
## [v1.0.6] - 2026-01-04
|
|
26
186
|
|
|
27
187
|
### 🚨 Fixed (紧急修复)
|
|
@@ -454,25 +454,42 @@ class DslAdapter {
|
|
|
454
454
|
const build = (index) => {
|
|
455
455
|
if (index >= entries.length) {
|
|
456
456
|
if (defaultDsl) {
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
457
|
+
// 处理 _default 值(可能是 Match、If 或普通 DSL)
|
|
458
|
+
if (defaultDsl._isMatch) {
|
|
459
|
+
return this._buildMatchSchema(defaultDsl.field, targetField, defaultDsl.map);
|
|
460
|
+
} else if (defaultDsl._isIf) {
|
|
461
|
+
return this._buildIfSchema(defaultDsl.condition, targetField, defaultDsl.then, defaultDsl.else);
|
|
462
|
+
} else {
|
|
463
|
+
const s = this._resolveDsl(defaultDsl);
|
|
464
|
+
const isRequired = s._required;
|
|
465
|
+
this._cleanRequiredMarks(s);
|
|
466
|
+
const result = { properties: { [targetField]: s } };
|
|
467
|
+
if (isRequired) result.required = [targetField];
|
|
468
|
+
return result;
|
|
469
|
+
}
|
|
465
470
|
}
|
|
466
471
|
return {};
|
|
467
472
|
}
|
|
468
473
|
|
|
469
474
|
const [val, dsl] = entries[index];
|
|
470
|
-
const branchSchema = this._resolveDsl(dsl);
|
|
471
|
-
const isRequired = branchSchema._required;
|
|
472
|
-
this._cleanRequiredMarks(branchSchema);
|
|
473
475
|
|
|
474
|
-
|
|
475
|
-
|
|
476
|
+
// 处理分支值(可能是 Match、If 或普通 DSL)
|
|
477
|
+
let thenSchema;
|
|
478
|
+
// 先检查 null/undefined
|
|
479
|
+
if (dsl === null || dsl === undefined) {
|
|
480
|
+
// 跳过 null/undefined 分支,继续下一个
|
|
481
|
+
return build(index + 1);
|
|
482
|
+
} else if (dsl._isMatch) {
|
|
483
|
+
thenSchema = this._buildMatchSchema(dsl.field, targetField, dsl.map);
|
|
484
|
+
} else if (dsl._isIf) {
|
|
485
|
+
thenSchema = this._buildIfSchema(dsl.condition, targetField, dsl.then, dsl.else);
|
|
486
|
+
} else {
|
|
487
|
+
const branchSchema = this._resolveDsl(dsl);
|
|
488
|
+
const isRequired = branchSchema._required;
|
|
489
|
+
this._cleanRequiredMarks(branchSchema);
|
|
490
|
+
thenSchema = { properties: { [targetField]: branchSchema } };
|
|
491
|
+
if (isRequired) thenSchema.required = [targetField];
|
|
492
|
+
}
|
|
476
493
|
|
|
477
494
|
return {
|
|
478
495
|
if: { properties: { [conditionField]: { const: val } } },
|
|
@@ -490,31 +507,41 @@ class DslAdapter {
|
|
|
490
507
|
* @static
|
|
491
508
|
*/
|
|
492
509
|
static _buildIfSchema(conditionField, targetField, thenDsl, elseDsl) {
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
510
|
+
// 处理 then 分支
|
|
511
|
+
let thenResult;
|
|
512
|
+
if (thenDsl && thenDsl._isMatch) {
|
|
513
|
+
// then 是一个 Match 结构,需要递归构建
|
|
514
|
+
thenResult = this._buildMatchSchema(thenDsl.field, targetField, thenDsl.map);
|
|
515
|
+
} else if (thenDsl && thenDsl._isIf) {
|
|
516
|
+
// then 是一个 If 结构,需要递归构建
|
|
517
|
+
thenResult = this._buildIfSchema(thenDsl.condition, targetField, thenDsl.then, thenDsl.else);
|
|
518
|
+
} else {
|
|
519
|
+
const thenSchema = this._resolveDsl(thenDsl);
|
|
520
|
+
const thenRequired = thenSchema._required;
|
|
521
|
+
this._cleanRequiredMarks(thenSchema);
|
|
522
|
+
thenResult = { properties: { [targetField]: thenSchema } };
|
|
523
|
+
if (thenRequired) thenResult.required = [targetField];
|
|
524
|
+
}
|
|
501
525
|
|
|
526
|
+
// 处理 else 分支
|
|
502
527
|
let elseResult = {};
|
|
503
|
-
if (
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
if (
|
|
528
|
+
if (elseDsl) {
|
|
529
|
+
if (elseDsl._isMatch) {
|
|
530
|
+
// else 也是一个 Match 结构
|
|
531
|
+
elseResult = this._buildMatchSchema(elseDsl.field, targetField, elseDsl.map);
|
|
532
|
+
} else if (elseDsl._isIf) {
|
|
533
|
+
// else 也是一个 If 结构
|
|
534
|
+
elseResult = this._buildIfSchema(elseDsl.condition, targetField, elseDsl.then, elseDsl.else);
|
|
535
|
+
} else {
|
|
536
|
+
const elseSchema = this._resolveDsl(elseDsl);
|
|
537
|
+
const elseRequired = elseSchema._required;
|
|
538
|
+
this._cleanRequiredMarks(elseSchema);
|
|
539
|
+
elseResult = { properties: { [targetField]: elseSchema } };
|
|
540
|
+
if (elseRequired) elseResult.required = [targetField];
|
|
541
|
+
}
|
|
508
542
|
}
|
|
509
543
|
|
|
510
|
-
// 简单假设 condition 是字段存在性或布尔值 true
|
|
511
|
-
// 如果需要更复杂的条件,可能需要解析 condition 字符串
|
|
512
|
-
// 这里假设 conditionField 是一个布尔字段,检查它是否为 true
|
|
513
|
-
// 或者检查它是否存在 (如果它是一个 required 字段)
|
|
514
|
-
|
|
515
544
|
// 默认行为:检查字段值为 true (适用于 isVip: boolean)
|
|
516
|
-
// 如果需要检查存在性,JSON Schema 比较复杂
|
|
517
|
-
|
|
518
545
|
return {
|
|
519
546
|
if: { properties: { [conditionField]: { const: true } } },
|
|
520
547
|
then: thenResult,
|