schema-dsl 1.0.5 → 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 +236 -0
- package/README.md +8 -5
- package/docs/typescript-guide.md +29 -9
- package/index.d.ts +42 -250
- package/lib/adapters/DslAdapter.js +60 -33
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -11,6 +11,11 @@
|
|
|
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) |
|
|
17
|
+
| [v1.0.6](#v106) | 2026-01-04 | 🚨 紧急修复:TypeScript 类型污染 | [查看详情](#v106) |
|
|
18
|
+
| [v1.0.5](#v105) | 2026-01-04 | 测试覆盖率提升至 97% | [查看详情](#v105) |
|
|
14
19
|
| [v1.0.4](#v104) | 2025-12-31 | TypeScript 完整支持、validateAsync、ValidationError | [查看详情](#v104) |
|
|
15
20
|
| [v1.0.3](#v103) | 2025-12-31 | ⚠️ 破坏性变更:单值语法修复 | [查看详情](#v103) |
|
|
16
21
|
| [v1.0.2](#v102) | 2025-12-31 | 15个新增验证器、完整文档、75个测试 | [查看详情](#v102) |
|
|
@@ -19,6 +24,237 @@
|
|
|
19
24
|
|
|
20
25
|
---
|
|
21
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
|
+
|
|
185
|
+
## [v1.0.6] - 2026-01-04
|
|
186
|
+
|
|
187
|
+
### 🚨 Fixed (紧急修复)
|
|
188
|
+
|
|
189
|
+
#### TypeScript 类型污染问题 ⭐⭐⭐
|
|
190
|
+
|
|
191
|
+
**问题描述**:
|
|
192
|
+
- v1.0.5 及更早版本中,`index.d.ts` 包含了全局 `interface String` 扩展(第 816-1065 行,共 209 行代码)
|
|
193
|
+
- 这导致 TypeScript 全局类型系统污染,原生 `String.prototype` 方法的类型被错误推断
|
|
194
|
+
- **严重问题**:`String.prototype.trim()` 返回类型从 `string` 被错误推断为 `DslBuilder`
|
|
195
|
+
- 影响所有使用 TypeScript 的项目,导致类型安全问题
|
|
196
|
+
|
|
197
|
+
**修复内容**:
|
|
198
|
+
- ✅ **完全删除**全局 `interface String` 扩展(移除 209 行类型污染代码)
|
|
199
|
+
- ✅ 添加详细的 TypeScript 使用说明文档
|
|
200
|
+
- ✅ 保证原生 String 方法的类型推断正确(`trim()` 正确返回 `string`)
|
|
201
|
+
|
|
202
|
+
**对用户的影响**:
|
|
203
|
+
|
|
204
|
+
1. **JavaScript 用户** ✅ **完全不受影响**
|
|
205
|
+
```javascript
|
|
206
|
+
// 仍然可以正常使用 String 扩展
|
|
207
|
+
const schema = dsl({ email: 'email!'.label('邮箱') });
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
2. **TypeScript 用户** ⚠️ **需要调整用法**
|
|
211
|
+
```typescript
|
|
212
|
+
// ❌ v1.0.5 及之前(有类型污染 bug)
|
|
213
|
+
const schema = dsl({ email: 'email!'.label('邮箱') });
|
|
214
|
+
|
|
215
|
+
// ✅ v1.0.6 推荐写法(获得正确类型提示)
|
|
216
|
+
const schema = dsl({
|
|
217
|
+
email: dsl('email!').label('邮箱')
|
|
218
|
+
});
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
**技术细节**:
|
|
222
|
+
- 文件变化:`index.d.ts` 从 2958 行减少到 2749 行
|
|
223
|
+
- 测试状态:所有 677 个测试通过 ✅
|
|
224
|
+
- 类型验证:原生 `String.prototype.trim()` 现在正确返回 `string` 类型
|
|
225
|
+
|
|
226
|
+
**迁移指南**:
|
|
227
|
+
- JavaScript 项目:无需任何修改
|
|
228
|
+
- TypeScript 项目:使用 `dsl()` 函数包裹字符串字面量获得类型提示
|
|
229
|
+
- 详见:[TypeScript 使用指南](./docs/typescript-guide.md)
|
|
230
|
+
|
|
231
|
+
---
|
|
232
|
+
|
|
233
|
+
## [v1.0.5] - 2026-01-04
|
|
234
|
+
|
|
235
|
+
### Added (新增功能)
|
|
236
|
+
|
|
237
|
+
#### 测试覆盖率大幅提升 ⭐
|
|
238
|
+
|
|
239
|
+
- ✅ **新增 5 个核心类的完整测试**
|
|
240
|
+
- `CacheManager.test.js` - 24 个测试(缓存管理)
|
|
241
|
+
- `ErrorFormatter.test.js` - 9 个测试(错误格式化)
|
|
242
|
+
- `JSONSchemaCore.test.js` - 10 个测试(JSON Schema 核心)
|
|
243
|
+
- `MarkdownExporter.test.js` - 3 个测试(Markdown 导出)
|
|
244
|
+
- `ErrorCodes.test.js` - 4 个测试(错误代码)
|
|
245
|
+
|
|
246
|
+
- ✅ **测试统计**
|
|
247
|
+
- 总测试数:651 → 677(+26 个测试)
|
|
248
|
+
- 测试覆盖率:92% → 97%(+5%)
|
|
249
|
+
- 所有核心类现在都有完整测试覆盖
|
|
250
|
+
|
|
251
|
+
### Fixed (修复)
|
|
252
|
+
|
|
253
|
+
- ✅ 修复缓存配置支持(4 个参数完整支持)
|
|
254
|
+
- ✅ 简化 i18n 配置(从 3 个方法简化为 2 个)
|
|
255
|
+
|
|
256
|
+
---
|
|
257
|
+
|
|
22
258
|
## [v1.0.4] - 2025-12-31
|
|
23
259
|
|
|
24
260
|
### Added (新增功能)
|
package/README.md
CHANGED
|
@@ -220,12 +220,12 @@ console.log(result2.errors); // 错误列表
|
|
|
220
220
|
|
|
221
221
|
### 1.5 TypeScript 用法 ⭐
|
|
222
222
|
|
|
223
|
-
**重要**: TypeScript
|
|
223
|
+
**重要**: TypeScript 中**必须**使用 `dsl()` 包裹字符串以获得类型提示(v1.0.6+ 移除了全局 String 类型扩展以避免类型污染):
|
|
224
224
|
|
|
225
225
|
```typescript
|
|
226
226
|
import { dsl, validateAsync, ValidationError } from 'schema-dsl';
|
|
227
227
|
|
|
228
|
-
// ✅
|
|
228
|
+
// ✅ 正确:使用 dsl() 包裹字符串获得完整类型提示
|
|
229
229
|
const userSchema = dsl({
|
|
230
230
|
username: dsl('string:3-32!')
|
|
231
231
|
.pattern(/^[a-zA-Z0-9_]+$/, '只能包含字母、数字和下划线')
|
|
@@ -256,10 +256,13 @@ try {
|
|
|
256
256
|
}
|
|
257
257
|
```
|
|
258
258
|
|
|
259
|
-
|
|
259
|
+
**为什么必须用 `dsl()` 包裹?**
|
|
260
260
|
- ✅ 完整的类型推导和 IDE 自动提示
|
|
261
|
-
- ✅
|
|
262
|
-
- ✅
|
|
261
|
+
- ✅ 避免污染原生 String 类型(v1.0.6+ 重要改进)
|
|
262
|
+
- ✅ 保证 `trim()`、`toLowerCase()` 等原生方法类型正确
|
|
263
|
+
- ✅ 更好的开发体验和类型安全
|
|
264
|
+
|
|
265
|
+
**JavaScript 用户不受影响**:在 JavaScript 中仍然可以直接使用 `'email!'.label('邮箱')` 语法。
|
|
263
266
|
|
|
264
267
|
**详细说明**: 请查看 [TypeScript 使用指南](./docs/typescript-guide.md)
|
|
265
268
|
|
package/docs/typescript-guide.md
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
# TypeScript 使用指南
|
|
2
2
|
|
|
3
|
-
> **版本**: schema-dsl v1.0.
|
|
4
|
-
> **更新日期**:
|
|
3
|
+
> **版本**: schema-dsl v1.0.6+
|
|
4
|
+
> **更新日期**: 2026-01-04
|
|
5
|
+
> **重要**: v1.0.6 移除了全局 String 类型扩展以避免类型污染
|
|
5
6
|
|
|
6
7
|
---
|
|
7
8
|
|
|
@@ -53,28 +54,47 @@ if (result.valid) {
|
|
|
53
54
|
|
|
54
55
|
## 2. TypeScript 中的链式调用
|
|
55
56
|
|
|
56
|
-
### 2.1
|
|
57
|
+
### 2.1 重要变更(v1.0.6)
|
|
57
58
|
|
|
58
|
-
|
|
59
|
+
**v1.0.6 移除了全局 `interface String` 扩展**,原因:
|
|
60
|
+
- ❌ 全局类型扩展会污染原生 String 类型
|
|
61
|
+
- ❌ 导致 `trim()`、`toLowerCase()` 等原生方法的类型推断错误
|
|
62
|
+
- ❌ 影响所有使用 TypeScript 的项目的类型安全
|
|
63
|
+
|
|
64
|
+
**结果**:在 TypeScript 中直接对字符串链式调用会报类型错误:
|
|
59
65
|
|
|
60
66
|
```typescript
|
|
61
|
-
// ❌ TypeScript
|
|
67
|
+
// ❌ TypeScript 中会报错(v1.0.6+)
|
|
68
|
+
const schema = dsl({
|
|
69
|
+
email: 'email!'.label('邮箱') // 类型错误:Property 'label' does not exist on type 'string'
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// ✅ JavaScript 中仍然可以正常使用
|
|
62
73
|
const schema = dsl({
|
|
63
|
-
email: 'email!'.label('邮箱') //
|
|
74
|
+
email: 'email!'.label('邮箱') // 运行时完全正常
|
|
64
75
|
});
|
|
65
76
|
```
|
|
66
77
|
|
|
67
|
-
### 2.2
|
|
78
|
+
### 2.2 正确用法 ⭐⭐⭐
|
|
68
79
|
|
|
69
|
-
|
|
80
|
+
**TypeScript 中必须使用 `dsl()` 函数包裹字符串**,才能获得类型提示和链式调用:
|
|
70
81
|
|
|
71
82
|
```typescript
|
|
72
|
-
// ✅
|
|
83
|
+
// ✅ 正确:使用 dsl() 包裹(v1.0.6+ 必须)
|
|
73
84
|
const schema = dsl({
|
|
74
85
|
email: dsl('email!').label('邮箱').pattern(/custom/)
|
|
75
86
|
});
|
|
87
|
+
|
|
88
|
+
// ✅ 也可以先定义再使用
|
|
89
|
+
const emailField = dsl('email!').label('邮箱');
|
|
90
|
+
const schema = dsl({ email: emailField });
|
|
76
91
|
```
|
|
77
92
|
|
|
93
|
+
**好处**:
|
|
94
|
+
- ✅ 获得完整的类型推导和 IDE 自动提示
|
|
95
|
+
- ✅ 不污染原生 String 类型(`trim()` 正确返回 `string`)
|
|
96
|
+
- ✅ 更好的类型安全和开发体验
|
|
97
|
+
|
|
78
98
|
### 2.3 工作原理
|
|
79
99
|
|
|
80
100
|
```typescript
|
package/index.d.ts
CHANGED
|
@@ -814,256 +814,47 @@ declare module 'schema-dsl' {
|
|
|
814
814
|
* });
|
|
815
815
|
* ```
|
|
816
816
|
*/
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
*/
|
|
859
|
-
default(value: any): DslBuilder;
|
|
860
|
-
|
|
861
|
-
/**
|
|
862
|
-
* 转为 JSON Schema
|
|
863
|
-
* @deprecated TypeScript 用户请使用 dsl(string).toSchema()
|
|
864
|
-
*/
|
|
865
|
-
toSchema(): JSONSchema;
|
|
866
|
-
|
|
867
|
-
/**
|
|
868
|
-
* 用户名验证
|
|
869
|
-
* @deprecated TypeScript 用户请使用 dsl(string).username()
|
|
870
|
-
*/
|
|
871
|
-
username(preset?: 'short' | 'medium' | 'long' | string): DslBuilder;
|
|
872
|
-
|
|
873
|
-
/**
|
|
874
|
-
* 密码强度验证
|
|
875
|
-
* @deprecated TypeScript 用户请使用 dsl(string).password()
|
|
876
|
-
*/
|
|
877
|
-
password(strength?: 'weak' | 'medium' | 'strong' | 'veryStrong'): DslBuilder;
|
|
878
|
-
|
|
879
|
-
/**
|
|
880
|
-
* 手机号验证
|
|
881
|
-
* @deprecated TypeScript 用户请使用 dsl(string).phone()
|
|
882
|
-
*/
|
|
883
|
-
phone(country?: 'cn' | 'us' | 'uk' | 'hk' | 'tw' | 'international'): DslBuilder;
|
|
884
|
-
|
|
885
|
-
/**
|
|
886
|
-
* 设置格式
|
|
887
|
-
* @deprecated TypeScript 用户请使用 dsl(string).format()
|
|
888
|
-
*/
|
|
889
|
-
format(format: string): DslBuilder;
|
|
890
|
-
|
|
891
|
-
/**
|
|
892
|
-
* 手机号别名
|
|
893
|
-
* @deprecated TypeScript 用户请使用 dsl(string).phoneNumber()
|
|
894
|
-
*/
|
|
895
|
-
phoneNumber(country?: 'cn' | 'us' | 'uk' | 'hk' | 'tw' | 'international'): DslBuilder;
|
|
896
|
-
|
|
897
|
-
/**
|
|
898
|
-
* 身份证验证
|
|
899
|
-
* @deprecated TypeScript 用户请使用 dsl(string).idCard()
|
|
900
|
-
*/
|
|
901
|
-
idCard(country?: 'cn'): DslBuilder;
|
|
902
|
-
|
|
903
|
-
/**
|
|
904
|
-
* 信用卡验证
|
|
905
|
-
* @deprecated TypeScript 用户请使用 dsl(string).creditCard()
|
|
906
|
-
*/
|
|
907
|
-
creditCard(type?: 'visa' | 'mastercard' | 'amex' | 'discover' | 'jcb' | 'unionpay'): DslBuilder;
|
|
908
|
-
|
|
909
|
-
/**
|
|
910
|
-
* 车牌号验证
|
|
911
|
-
* @deprecated TypeScript 用户请使用 dsl(string).licensePlate()
|
|
912
|
-
*/
|
|
913
|
-
licensePlate(country?: 'cn' | 'us' | 'uk'): DslBuilder;
|
|
914
|
-
|
|
915
|
-
/**
|
|
916
|
-
* 邮政编码验证
|
|
917
|
-
* @deprecated TypeScript 用户请使用 dsl(string).postalCode()
|
|
918
|
-
*/
|
|
919
|
-
postalCode(country?: 'cn' | 'us' | 'uk'): DslBuilder;
|
|
920
|
-
|
|
921
|
-
/**
|
|
922
|
-
* 护照号码验证
|
|
923
|
-
* @deprecated TypeScript 用户请使用 dsl(string).passport()
|
|
924
|
-
*/
|
|
925
|
-
passport(country?: 'cn' | 'us' | 'uk'): DslBuilder;
|
|
926
|
-
|
|
927
|
-
/**
|
|
928
|
-
* String 最小长度
|
|
929
|
-
* @deprecated TypeScript 用户请使用 dsl(string).min()
|
|
930
|
-
*/
|
|
931
|
-
min(n: number): DslBuilder;
|
|
932
|
-
|
|
933
|
-
/**
|
|
934
|
-
* String 最大长度
|
|
935
|
-
* @deprecated TypeScript 用户请使用 dsl(string).max()
|
|
936
|
-
*/
|
|
937
|
-
max(n: number): DslBuilder;
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
/**
|
|
941
|
-
* String 只能包含字母和数字
|
|
942
|
-
* @deprecated TypeScript 用户请使用 dsl(string).alphanum()
|
|
943
|
-
*/
|
|
944
|
-
alphanum(): DslBuilder;
|
|
945
|
-
|
|
946
|
-
/**
|
|
947
|
-
* String 不能包含前后空格
|
|
948
|
-
* @deprecated TypeScript 用户请使用 dsl(string).trim()
|
|
949
|
-
*/
|
|
950
|
-
trim(): DslBuilder;
|
|
951
|
-
|
|
952
|
-
/**
|
|
953
|
-
* String 必须是小写
|
|
954
|
-
* @deprecated TypeScript 用户请使用 dsl(string).lowercase()
|
|
955
|
-
*/
|
|
956
|
-
lowercase(): DslBuilder;
|
|
957
|
-
|
|
958
|
-
/**
|
|
959
|
-
* String 必须是大写
|
|
960
|
-
* @deprecated TypeScript 用户请使用 dsl(string).uppercase()
|
|
961
|
-
*/
|
|
962
|
-
uppercase(): DslBuilder;
|
|
963
|
-
|
|
964
|
-
/**
|
|
965
|
-
* Number 小数位数限制
|
|
966
|
-
* @deprecated TypeScript 用户请使用 dsl(string).precision()
|
|
967
|
-
*/
|
|
968
|
-
precision(n: number): DslBuilder;
|
|
969
|
-
|
|
970
|
-
/**
|
|
971
|
-
* Number 倍数验证
|
|
972
|
-
* @deprecated TypeScript 用户请使用 dsl(string).multiple()
|
|
973
|
-
*/
|
|
974
|
-
multiple(n: number): DslBuilder;
|
|
975
|
-
|
|
976
|
-
/**
|
|
977
|
-
* Number 端口号验证
|
|
978
|
-
* @deprecated TypeScript 用户请使用 dsl(string).port()
|
|
979
|
-
*/
|
|
980
|
-
port(): DslBuilder;
|
|
981
|
-
|
|
982
|
-
/**
|
|
983
|
-
* Object 要求所有属性都必须存在
|
|
984
|
-
* @deprecated TypeScript 用户请使用 dsl(obj).requireAll()
|
|
985
|
-
*/
|
|
986
|
-
requireAll(): DslBuilder;
|
|
987
|
-
|
|
988
|
-
/**
|
|
989
|
-
* Object 严格模式
|
|
990
|
-
* @deprecated TypeScript 用户请使用 dsl(obj).strict()
|
|
991
|
-
*/
|
|
992
|
-
strict(): DslBuilder;
|
|
993
|
-
|
|
994
|
-
/**
|
|
995
|
-
* Array 不允许稀疏数组
|
|
996
|
-
* @deprecated TypeScript 用户请使用 dsl(string).noSparse()
|
|
997
|
-
*/
|
|
998
|
-
noSparse(): DslBuilder;
|
|
999
|
-
|
|
1000
|
-
/**
|
|
1001
|
-
* Array 必须包含指定元素
|
|
1002
|
-
* @deprecated TypeScript 用户请使用 dsl(string).includesRequired()
|
|
1003
|
-
*/
|
|
1004
|
-
includesRequired(items: any[]): DslBuilder;
|
|
1005
|
-
|
|
1006
|
-
/**
|
|
1007
|
-
* Date 自定义日期格式验证
|
|
1008
|
-
* @deprecated TypeScript 用户请使用 dsl(string).dateFormat()
|
|
1009
|
-
*/
|
|
1010
|
-
dateFormat(fmt: string): DslBuilder;
|
|
1011
|
-
|
|
1012
|
-
/**
|
|
1013
|
-
* Date 必须晚于指定日期
|
|
1014
|
-
* @deprecated TypeScript 用户请使用 dsl(string).after()
|
|
1015
|
-
*/
|
|
1016
|
-
after(date: string): DslBuilder;
|
|
1017
|
-
|
|
1018
|
-
/**
|
|
1019
|
-
* Date 必须早于指定日期
|
|
1020
|
-
* @deprecated TypeScript 用户请使用 dsl(string).before()
|
|
1021
|
-
*/
|
|
1022
|
-
before(date: string): DslBuilder;
|
|
1023
|
-
|
|
1024
|
-
/**
|
|
1025
|
-
* Pattern 域名验证
|
|
1026
|
-
* @deprecated TypeScript 用户请使用 dsl(string).domain()
|
|
1027
|
-
*/
|
|
1028
|
-
domain(): DslBuilder;
|
|
1029
|
-
|
|
1030
|
-
/**
|
|
1031
|
-
* Pattern IP地址验证
|
|
1032
|
-
* @deprecated TypeScript 用户请使用 dsl(string).ip()
|
|
1033
|
-
*/
|
|
1034
|
-
ip(): DslBuilder;
|
|
1035
|
-
|
|
1036
|
-
/**
|
|
1037
|
-
* Pattern Base64编码验证
|
|
1038
|
-
* @deprecated TypeScript 用户请使用 dsl(string).base64()
|
|
1039
|
-
*/
|
|
1040
|
-
base64(): DslBuilder;
|
|
1041
|
-
|
|
1042
|
-
/**
|
|
1043
|
-
* Pattern JWT令牌验证
|
|
1044
|
-
* @deprecated TypeScript 用户请使用 dsl(string).jwt()
|
|
1045
|
-
*/
|
|
1046
|
-
jwt(): DslBuilder;
|
|
1047
|
-
|
|
1048
|
-
/**
|
|
1049
|
-
* Pattern JSON字符串验证
|
|
1050
|
-
* @deprecated TypeScript 用户请使用 dsl(string).json()
|
|
1051
|
-
*/
|
|
1052
|
-
json(): DslBuilder;
|
|
1053
|
-
|
|
1054
|
-
/**
|
|
1055
|
-
* 日期大于验证
|
|
1056
|
-
* @deprecated TypeScript 用户请使用 dsl(string).dateGreater()
|
|
1057
|
-
*/
|
|
1058
|
-
dateGreater(date: string): DslBuilder;
|
|
1059
|
-
|
|
1060
|
-
/**
|
|
1061
|
-
* 日期小于验证
|
|
1062
|
-
* @deprecated TypeScript 用户请使用 dsl(string).dateLess()
|
|
1063
|
-
*/
|
|
1064
|
-
dateLess(date: string): DslBuilder;
|
|
1065
|
-
}
|
|
1066
|
-
}
|
|
817
|
+
|
|
818
|
+
// ========== String 扩展说明 ==========
|
|
819
|
+
|
|
820
|
+
/**
|
|
821
|
+
* ⚠️ String 原型扩展的 TypeScript 限制
|
|
822
|
+
*
|
|
823
|
+
* 本库在运行时扩展了 String.prototype,允许在 JavaScript 中直接链式调用:
|
|
824
|
+
* ```javascript
|
|
825
|
+
* const schema = dsl({ email: 'email!'.label('邮箱') }); // ✅ JavaScript 中完全正常
|
|
826
|
+
* ```
|
|
827
|
+
*
|
|
828
|
+
* 但在 TypeScript 中,为了**避免污染全局 String 类型**(会导致原生方法如 trim() 的类型推断错误),
|
|
829
|
+
* 我们**不提供**全局 String 接口扩展。
|
|
830
|
+
*
|
|
831
|
+
* TypeScript 用户请使用以下方式:
|
|
832
|
+
*
|
|
833
|
+
* @example
|
|
834
|
+
* ```typescript
|
|
835
|
+
* import { dsl } from 'schema-dsl';
|
|
836
|
+
*
|
|
837
|
+
* // ✅ 推荐:使用 dsl() 函数获得完整类型提示
|
|
838
|
+
* const schema = dsl({
|
|
839
|
+
* email: dsl('email!').label('邮箱').pattern(/custom/)
|
|
840
|
+
* });
|
|
841
|
+
*
|
|
842
|
+
* // ✅ 或者先定义再使用
|
|
843
|
+
* const emailField = dsl('email!').label('邮箱');
|
|
844
|
+
* const schema = dsl({ email: emailField });
|
|
845
|
+
*
|
|
846
|
+
* // ❌ 避免:在 TypeScript 中直接对字符串字面量链式调用
|
|
847
|
+
* // 这在运行时可以工作,但 TypeScript 无法提供类型提示
|
|
848
|
+
* const schema = dsl({
|
|
849
|
+
* email: 'email!'.label('邮箱') // TypeScript: 类型错误
|
|
850
|
+
* });
|
|
851
|
+
* ```
|
|
852
|
+
*
|
|
853
|
+
* 📝 说明:
|
|
854
|
+
* - JavaScript 用户不受影响,可以直接使用字符串链式调用
|
|
855
|
+
* - TypeScript 用户应使用 dsl() 函数包裹字符串以获得类型提示
|
|
856
|
+
* - 移除全局 String 扩展是为了防止污染原生 String 方法的类型定义
|
|
857
|
+
*/
|
|
1067
858
|
|
|
1068
859
|
// ========== dsl() 函数 ==========
|
|
1069
860
|
|
|
@@ -2955,3 +2746,4 @@ declare module 'schema-dsl' {
|
|
|
2955
2746
|
export default dsl;
|
|
2956
2747
|
}
|
|
2957
2748
|
|
|
2749
|
+
|
|
@@ -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,
|