schema-dsl 1.1.0 → 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 CHANGED
@@ -11,6 +11,7 @@
11
11
 
12
12
  | 版本 | 日期 | 变更摘要 | 详细 |
13
13
  |------------------|------|---------|------|
14
+ | [v1.1.1](#v111) | 2026-01-06 | 🎉 新功能:ConditionalBuilder 独立消息支持 | [查看详情](#v111) |
14
15
  | [v1.1.0](#v110) | 2026-01-05 | 🎉 重大功能:跨类型联合验证 + 插件系统增强 | [查看详情](#v110) |
15
16
  | [v1.0.9](#v109) | 2026-01-04 | 🎉 重大改进:多语言支持完善 + TypeScript 类型完整 | [查看详情](#v109) |
16
17
  | [v1.0.8](#v108) | 2026-01-04 | 优化:错误消息过滤增强 | [查看详情](#v108) |
@@ -25,31 +26,150 @@
25
26
 
26
27
  ---
27
28
 
28
- ## [v1.1.0] - 2026-01-05
29
+ ## [v1.1.1] - 2026-01-06
29
30
 
30
31
  ### 🎉 新功能
31
32
 
32
- #### 1. 跨类型联合验证 - `types:` 语法
33
+ #### ConditionalBuilder 独立消息支持 - `.and()/.or()` 后可调用 `.message()`
33
34
 
34
- **一个字段支持多种类型**
35
+ **每个条件都可以有自己的错误消息**
35
36
 
36
- 现在可以使用 `types:` 前缀定义跨类型联合验证,支持字段匹配多种不同的数据类型。
37
+ 现在支持在 `.and()` 和 `.or()` 后调用 `.message()` 设置独立的错误消息,让错误提示更精确。
37
38
 
38
39
  **基础用法**:
39
40
 
40
41
  ```javascript
41
- const { dsl, validate } = require('schema-dsl');
42
+ const { dsl } = require('schema-dsl');
43
+
44
+ // ✅ v1.1.1 新功能:每个条件独立消息
45
+ dsl.if(d => !d)
46
+ .message('ACCOUNT_NOT_FOUND')
47
+ .and(d => d.tradable_credits < amount)
48
+ .message('INSUFFICIENT_TRADABLE_CREDITS')
49
+ .assert(account);
50
+
51
+ // 工作原理:
52
+ // - 第一个条件失败 → 返回 'ACCOUNT_NOT_FOUND'
53
+ // - 第二个条件失败 → 返回 'INSUFFICIENT_TRADABLE_CREDITS'
54
+ // - 所有条件通过 → 验证成功
55
+ ```
42
56
 
43
- // 字段可以是字符串或数字
44
- const schema = dsl({
45
- value: 'types:string|number'
57
+ **特性**:
58
+ - 支持多个 `.and()` 条件各有独立消息
59
+ - ✅ 支持 `.or()` 条件独立消息
60
+ - ✅ 自动检测启用链式检查模式
61
+ - ✅ 100% 向后兼容,不影响现有代码
62
+ - ✅ 完整的 TypeScript 类型支持
63
+
64
+ **实际应用示例**:
65
+
66
+ ```javascript
67
+ // 多层验证,每层都有清晰的错误消息
68
+ dsl.if(d => !d)
69
+ .message('ACCOUNT_NOT_FOUND')
70
+ .and(d => d.status !== 'active')
71
+ .message('ACCOUNT_INACTIVE')
72
+ .and(d => d.tradable_credits < amount)
73
+ .message('INSUFFICIENT_TRADABLE_CREDITS')
74
+ .assert(account);
75
+ ```
76
+
77
+ **文档链接**:
78
+ - [条件 API 文档](./docs/conditional-api.md)
79
+ - [README FAQ Q7](./README.md#q7-如何合并多个-dslif-验证)
80
+
81
+ ---
82
+
83
+ #### I18nError 多语言错误抛出机制
84
+
85
+ **统一的多语言错误抛出**
86
+
87
+ 新增 `I18nError` 类和 `dsl.error` 快捷方法,提供统一的多语言错误抛出机制。
88
+
89
+ **基础用法**:
90
+
91
+ ```javascript
92
+ const { I18nError, dsl } = require('schema-dsl');
93
+
94
+ // 方式1:直接抛出
95
+ I18nError.throw('account.notFound');
96
+ // 中文: "账户不存在"
97
+ // 英文: "Account not found"
98
+
99
+ // 方式2:带参数插值
100
+ I18nError.throw('account.insufficientBalance', {
101
+ balance: 50,
102
+ required: 100
46
103
  });
104
+ // 输出: "余额不足,当前余额50,需要100"
105
+
106
+ // 方式3:断言风格(推荐)
107
+ I18nError.assert(account, 'account.notFound');
108
+ I18nError.assert(
109
+ account.balance >= 100,
110
+ 'account.insufficientBalance',
111
+ { balance: account.balance, required: 100 }
112
+ );
113
+
114
+ // 方式4:快捷方法
115
+ dsl.error.throw('user.noPermission');
116
+ dsl.error.assert(user.role === 'admin', 'user.noPermission');
117
+ ```
118
+
119
+ **特性**:
120
+ - ✅ 统一的错误代码格式:`{模块}.{错误类型}`
121
+ - ✅ 自动多语言翻译(支持中英文)
122
+ - ✅ 参数插值支持
123
+ - ✅ Express/Koa 集成(toJSON 方法)
124
+ - ✅ 与 `.message()` 和 `.label()` 使用相同的多语言机制
47
125
 
48
- validate(schema, { value: 'hello' }); // ✅ 通过
49
- validate(schema, { value: 123 }); // ✅ 通过
50
- validate(schema, { value: true }); // ❌ 失败
126
+ **内置错误代码**:
127
+ - 通用: `error.notFound`, `error.forbidden`, `error.unauthorized`
128
+ - 账户: `account.notFound`, `account.insufficientBalance`, `account.insufficientCredits`
129
+ - 用户: `user.notFound`, `user.noPermission`, `user.notVerified`
130
+ - 订单: `order.notPaid`, `order.paymentMissing`, `order.addressMissing`
131
+
132
+ **Express/Koa 集成**:
133
+
134
+ ```javascript
135
+ app.use((error, req, res, next) => {
136
+ if (error instanceof I18nError) {
137
+ return res.status(error.statusCode).json(error.toJSON());
138
+ }
139
+ next(error);
140
+ });
51
141
  ```
52
142
 
143
+ **文档链接**:
144
+ - [使用示例](./examples/i18n-error.examples.js)
145
+ - [README FAQ Q8](./README.md#q8-如何统一抛出多语言错误v111)
146
+
147
+ ---
148
+
149
+ ### 📝 测试
150
+
151
+ - **新增**: 52 个测试用例(24个独立消息 + 28个 I18nError)
152
+ - **总计**: 921 个测试全部通过 (100%)
153
+
154
+ ### 📖 文档
155
+
156
+ - **新增**: docs/conditional-api.md 新增 600+ 行功能说明
157
+ - **新增**: examples/i18n-error.examples.js I18nError 使用示例
158
+ - **更新**: README.md FAQ Q7/Q8 添加新功能说明
159
+ - **更新**: index.d.ts TypeScript 类型注释和示例(I18nError + dsl.error)
160
+
161
+ ---
162
+
163
+ ## [v1.1.0] - 2026-01-05
164
+
165
+ ### 🎉 新功能
166
+
167
+ #### 1. 跨类型联合验证 - `types:` 语法
168
+
169
+ **一个字段支持多种类型**
170
+
171
+ ...existing content...
172
+
53
173
  **带约束的联合类型**:
54
174
 
55
175
  ```javascript