schema-dsl 1.2.5 → 2.0.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 +130 -238
- package/LICENSE +21 -21
- package/README.md +628 -2486
- package/dist/DslBuilder-BIgQOAXp.d.ts +343 -0
- package/dist/DslBuilder-CjHTucNQ.d.cts +343 -0
- package/dist/Validator-CllRdrY0.d.ts +192 -0
- package/dist/Validator-D6okG9tr.d.cts +192 -0
- package/dist/index.cjs +6640 -0
- package/dist/index.d.cts +1151 -0
- package/dist/index.d.ts +1151 -0
- package/dist/index.js +6574 -0
- package/dist/plugin-CIKtTMtS.d.cts +246 -0
- package/dist/plugin-CIKtTMtS.d.ts +246 -0
- package/dist/plugins/custom-format.cjs +3818 -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 +3788 -0
- package/dist/plugins/custom-type-example.cjs +3811 -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 +3781 -0
- package/dist/plugins/custom-validator.cjs +144 -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 +119 -0
- package/docs/FEATURE-INDEX.md +553 -519
- package/docs/add-custom-locale.md +496 -483
- package/docs/add-keyword.md +24 -0
- package/docs/api-reference.md +1047 -805
- package/docs/api.md +13 -0
- package/docs/best-practices-project-structure.md +417 -408
- package/docs/best-practices.md +712 -672
- package/docs/cache-manager.md +344 -336
- package/docs/compile.md +45 -0
- package/docs/conditional-api.md +1307 -1278
- package/docs/custom-extensions-guide.md +339 -411
- package/docs/design-philosophy.md +606 -601
- package/docs/doc-index.md +324 -0
- package/docs/dsl-syntax.md +714 -664
- package/docs/dynamic-locale.md +608 -598
- package/docs/enum.md +482 -475
- package/docs/error-handling.md +1975 -1966
- package/docs/export-guide.md +501 -462
- package/docs/export-limitations.md +567 -551
- package/docs/faq.md +596 -577
- package/docs/frontend-i18n-guide.md +307 -293
- package/docs/i18n-user-guide.md +487 -474
- package/docs/i18n.md +476 -457
- package/docs/index.md +48 -0
- package/docs/json-schema-basics.md +40 -0
- package/docs/label-vs-description.md +271 -262
- package/docs/markdown-exporter.md +406 -397
- package/docs/mongodb-exporter.md +302 -295
- package/docs/multi-language.md +26 -0
- package/docs/multi-type-support.md +322 -329
- package/docs/mysql-exporter.md +280 -273
- package/docs/number-operators.md +449 -442
- package/docs/optional-marker-guide.md +326 -321
- package/docs/performance-guide.md +49 -0
- package/docs/plugin-system.md +381 -542
- package/docs/plugin-type-registration.md +34 -0
- package/docs/postgresql-exporter.md +311 -304
- package/docs/public/favicon.svg +5 -0
- package/docs/quick-start.md +435 -761
- package/docs/runtime-locale-support.md +532 -521
- package/docs/schema-helper.md +345 -340
- package/docs/schema-utils-advanced-issues.md +23 -0
- package/docs/schema-utils-best-practices.md +20 -0
- package/docs/schema-utils-chaining.md +150 -143
- package/docs/schema-utils.md +524 -490
- package/docs/security-checklist.md +20 -0
- package/docs/string-extensions.md +488 -480
- package/docs/troubleshooting.md +486 -471
- package/docs/type-converter.md +310 -319
- package/docs/type-reference.md +242 -219
- package/docs/typescript-guide.md +584 -573
- package/docs/union-type-guide.md +157 -147
- package/docs/union-types.md +284 -277
- package/docs/validate-async.md +491 -480
- package/docs/validate-batch.md +49 -0
- package/docs/validate-dsl-object-support.md +578 -573
- package/docs/validate.md +506 -486
- package/docs/validation-guide.md +502 -484
- package/docs/validator.md +39 -0
- package/package.json +131 -73
- 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 +169 -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 +687 -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 +156 -0
- package/src/exporters/PostgreSQLExporter.ts +222 -0
- package/src/exporters/index.ts +18 -0
- package/src/index.ts +651 -0
- package/{lib/locales/en-US.js → src/locales/en-US.ts} +160 -176
- package/{lib/locales/es-ES.js → src/locales/es-ES.ts} +160 -113
- package/{lib/locales/fr-FR.js → src/locales/fr-FR.ts} +160 -113
- package/src/locales/index.ts +103 -0
- package/{lib/locales/ja-JP.js → src/locales/ja-JP.ts} +160 -118
- package/src/locales/types.ts +156 -0
- package/{lib/locales/zh-CN.js → src/locales/zh-CN.ts} +160 -177
- 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 +124 -0
- package/src/plugins/custom-type-example.ts +106 -0
- package/src/plugins/custom-validator.ts +138 -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 +365 -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 -3658
- package/index.js +0 -475
- package/index.mjs +0 -60
- package/lib/adapters/DslAdapter.js +0 -995
- 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 -1589
- 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/changelogs/v1.0.0.md
DELETED
|
@@ -1,328 +0,0 @@
|
|
|
1
|
-
# v1.0.0 变更日志
|
|
2
|
-
|
|
3
|
-
> **发布日期**: 2025-12-29
|
|
4
|
-
> **版本号**: v1.0.0
|
|
5
|
-
> **类型**: 🎉 正式发布
|
|
6
|
-
|
|
7
|
-
---
|
|
8
|
-
|
|
9
|
-
## 📋 变更概览
|
|
10
|
-
|
|
11
|
-
这是 schema-dsl 的首个正式发布版本,标志着项目达到生产就绪状态。
|
|
12
|
-
|
|
13
|
-
| 里程碑 | 状态 |
|
|
14
|
-
|--------|------|
|
|
15
|
-
| 核心功能完善 | ✅ |
|
|
16
|
-
| 测试覆盖充分 | ✅ |
|
|
17
|
-
| 文档完整 | ✅ |
|
|
18
|
-
| npm 发布 | ✅ |
|
|
19
|
-
| 生产就绪 | ✅ |
|
|
20
|
-
|
|
21
|
-
---
|
|
22
|
-
|
|
23
|
-
## 🎉 核心特性
|
|
24
|
-
|
|
25
|
-
### 1. 极简 DSL 语法
|
|
26
|
-
|
|
27
|
-
**一行代码定义验证规则,代码量减少 65%**
|
|
28
|
-
|
|
29
|
-
```javascript
|
|
30
|
-
const { dsl } = require('schema-dsl');
|
|
31
|
-
|
|
32
|
-
// ✅ 简洁的 DSL 语法
|
|
33
|
-
const schema = dsl({
|
|
34
|
-
username: 'string:3-32!',
|
|
35
|
-
email: 'email!',
|
|
36
|
-
age: 'number:18-120',
|
|
37
|
-
tags: 'array<string>'
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
// ❌ 对比 Joi(需要 8 行)
|
|
41
|
-
const Joi = require('joi');
|
|
42
|
-
const joiSchema = Joi.object({
|
|
43
|
-
username: Joi.string().min(3).max(32).required(),
|
|
44
|
-
email: Joi.string().email().required(),
|
|
45
|
-
age: Joi.number().min(18).max(120),
|
|
46
|
-
tags: Joi.array().items(Joi.string())
|
|
47
|
-
});
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
---
|
|
51
|
-
|
|
52
|
-
### 2. 完整的验证功能
|
|
53
|
-
|
|
54
|
-
#### 基础类型
|
|
55
|
-
|
|
56
|
-
- ✅ `string` - 字符串验证
|
|
57
|
-
- ✅ `number` - 数字验证
|
|
58
|
-
- ✅ `integer` - 整数验证
|
|
59
|
-
- ✅ `boolean` - 布尔值验证
|
|
60
|
-
- ✅ `date` / `datetime` - 日期时间验证
|
|
61
|
-
- ✅ `email` - 邮箱验证
|
|
62
|
-
- ✅ `url` - URL验证
|
|
63
|
-
- ✅ `phone` - 手机号验证
|
|
64
|
-
- ✅ `idCard` - 身份证验证
|
|
65
|
-
|
|
66
|
-
#### 高级功能
|
|
67
|
-
|
|
68
|
-
- ✅ **嵌套对象验证**
|
|
69
|
-
- ✅ **数组验证**
|
|
70
|
-
- ✅ **正则表达式验证**
|
|
71
|
-
- ✅ **自定义验证器**
|
|
72
|
-
- ✅ **条件验证**
|
|
73
|
-
- ✅ **枚举验证**
|
|
74
|
-
|
|
75
|
-
---
|
|
76
|
-
|
|
77
|
-
### 3. 高性能设计
|
|
78
|
-
|
|
79
|
-
**性能测试结果**:
|
|
80
|
-
|
|
81
|
-
| 验证库 | 性能 (ops/s) | 相对速度 |
|
|
82
|
-
|--------|-------------|---------|
|
|
83
|
-
| **schema-dsl** | **2,879,606** | **基准 (1.00x)** |
|
|
84
|
-
| Zod | 1,818,592 | 0.63x (慢 58%) |
|
|
85
|
-
| Joi | 299,761 | 0.10x (慢 861%) |
|
|
86
|
-
| Yup | 106,378 | 0.04x (慢 2607%) |
|
|
87
|
-
|
|
88
|
-
**优化技术**:
|
|
89
|
-
- ✅ WeakMap 缓存机制
|
|
90
|
-
- ✅ 智能编译优化
|
|
91
|
-
- ✅ 批量验证优化
|
|
92
|
-
- ✅ 惰性求值
|
|
93
|
-
|
|
94
|
-
---
|
|
95
|
-
|
|
96
|
-
### 4. TypeScript 完整支持
|
|
97
|
-
|
|
98
|
-
```typescript
|
|
99
|
-
import { dsl, validate, ValidationError } from 'schema-dsl';
|
|
100
|
-
|
|
101
|
-
// 完整的类型推导
|
|
102
|
-
const schema = dsl({
|
|
103
|
-
username: 'string:3-32!',
|
|
104
|
-
email: 'email!'
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
// 类型安全的验证
|
|
108
|
-
const result = validate(schema, data);
|
|
109
|
-
|
|
110
|
-
if (result.valid) {
|
|
111
|
-
console.log(result.data); // 类型推导
|
|
112
|
-
} else {
|
|
113
|
-
console.log(result.errors); // ValidationError[]
|
|
114
|
-
}
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
---
|
|
118
|
-
|
|
119
|
-
### 5. 数据库 Schema 导出
|
|
120
|
-
|
|
121
|
-
**独家功能:从验证规则直接生成数据库结构**
|
|
122
|
-
|
|
123
|
-
```javascript
|
|
124
|
-
const { dsl, exporters } = require('schema-dsl');
|
|
125
|
-
|
|
126
|
-
const schema = dsl({
|
|
127
|
-
username: 'string:3-32!',
|
|
128
|
-
email: 'email!',
|
|
129
|
-
age: 'number:18-120'
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
// MongoDB Schema
|
|
133
|
-
const mongoExporter = new exporters.MongoDBExporter();
|
|
134
|
-
const mongoSchema = mongoExporter.export(schema);
|
|
135
|
-
|
|
136
|
-
// MySQL DDL
|
|
137
|
-
const mysqlExporter = new exporters.MySQLExporter();
|
|
138
|
-
const mysqlDDL = mysqlExporter.export('users', schema);
|
|
139
|
-
|
|
140
|
-
// PostgreSQL DDL
|
|
141
|
-
const pgExporter = new exporters.PostgreSQLExporter();
|
|
142
|
-
const pgDDL = pgExporter.export('users', schema);
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
---
|
|
146
|
-
|
|
147
|
-
### 6. 框架集成
|
|
148
|
-
|
|
149
|
-
**Express**
|
|
150
|
-
```javascript
|
|
151
|
-
const { validateAsync, ValidationError } = require('schema-dsl');
|
|
152
|
-
|
|
153
|
-
app.post('/api/users', async (req, res, next) => {
|
|
154
|
-
try {
|
|
155
|
-
const validData = await validateAsync(userSchema, req.body);
|
|
156
|
-
// 验证通过...
|
|
157
|
-
} catch (error) {
|
|
158
|
-
next(error);
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
**Koa**
|
|
164
|
-
```javascript
|
|
165
|
-
app.use(async (ctx, next) => {
|
|
166
|
-
try {
|
|
167
|
-
ctx.validData = await validateAsync(schema, ctx.request.body);
|
|
168
|
-
await next();
|
|
169
|
-
} catch (error) {
|
|
170
|
-
if (error instanceof ValidationError) {
|
|
171
|
-
ctx.status = 400;
|
|
172
|
-
ctx.body = { errors: error.errors };
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
---
|
|
179
|
-
|
|
180
|
-
### 7. 条件验证
|
|
181
|
-
|
|
182
|
-
```javascript
|
|
183
|
-
const { dsl } = require('schema-dsl');
|
|
184
|
-
|
|
185
|
-
// 快速断言
|
|
186
|
-
dsl.if(d => d.age < 18)
|
|
187
|
-
.message('未成年用户不能注册')
|
|
188
|
-
.assert(userData);
|
|
189
|
-
|
|
190
|
-
// 复杂条件
|
|
191
|
-
dsl.if(d => d.role === 'admin')
|
|
192
|
-
.and(d => d.permissions.includes('delete'))
|
|
193
|
-
.message('权限不足')
|
|
194
|
-
.assert(user);
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
---
|
|
198
|
-
|
|
199
|
-
### 8. Schema 复用工具
|
|
200
|
-
|
|
201
|
-
```javascript
|
|
202
|
-
const { SchemaUtils } = require('schema-dsl');
|
|
203
|
-
|
|
204
|
-
// 选择字段
|
|
205
|
-
const createSchema = SchemaUtils.pick(fullSchema, ['username', 'email']);
|
|
206
|
-
|
|
207
|
-
// 排除字段
|
|
208
|
-
const publicSchema = SchemaUtils.omit(fullSchema, ['password']);
|
|
209
|
-
|
|
210
|
-
// 变为可选
|
|
211
|
-
const updateSchema = SchemaUtils.partial(createSchema);
|
|
212
|
-
|
|
213
|
-
// 扩展字段
|
|
214
|
-
const registerSchema = createSchema.extend({
|
|
215
|
-
captcha: 'string:4-6!',
|
|
216
|
-
agree: 'boolean!'
|
|
217
|
-
});
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
---
|
|
221
|
-
|
|
222
|
-
## 📚 完整文档
|
|
223
|
-
|
|
224
|
-
### 用户指南
|
|
225
|
-
|
|
226
|
-
- ✅ [快速开始](../README.md#快速开始)
|
|
227
|
-
- ✅ [DSL 语法](../docs/dsl-syntax.md)
|
|
228
|
-
- ✅ [验证指南](../docs/validation-guide.md)
|
|
229
|
-
- ✅ [类型参考](../docs/type-reference.md)
|
|
230
|
-
|
|
231
|
-
### API 文档
|
|
232
|
-
|
|
233
|
-
- ✅ [dsl() API](../docs/api-reference.md)
|
|
234
|
-
- ✅ [validate() API](../docs/validate.md)
|
|
235
|
-
- ✅ [validateAsync() API](../docs/validate-async.md)
|
|
236
|
-
- ✅ [SchemaUtils API](../docs/schema-utils.md)
|
|
237
|
-
|
|
238
|
-
### 高级功能
|
|
239
|
-
|
|
240
|
-
- ✅ [条件验证](../docs/conditional-api.md)
|
|
241
|
-
- ✅ [自定义扩展](../docs/custom-extensions-guide.md)
|
|
242
|
-
- ✅ [数据库导出](../docs/mongodb-exporter.md)
|
|
243
|
-
- ✅ [性能优化](../docs/cache-manager.md)
|
|
244
|
-
|
|
245
|
-
### 框架集成
|
|
246
|
-
|
|
247
|
-
- ✅ [Express 集成](../examples/express-integration.js)
|
|
248
|
-
- ✅ [Koa 集成](../examples/middleware-usage.js)
|
|
249
|
-
- ✅ [Fastify 集成](../examples/middleware-usage.js)
|
|
250
|
-
|
|
251
|
-
---
|
|
252
|
-
|
|
253
|
-
## 📊 测试覆盖
|
|
254
|
-
|
|
255
|
-
- ✅ **测试套件**: 800+ 个测试用例
|
|
256
|
-
- ✅ **测试覆盖率**: 95%+
|
|
257
|
-
- ✅ **兼容性测试**: Node.js 14/16/18/20
|
|
258
|
-
|
|
259
|
-
---
|
|
260
|
-
|
|
261
|
-
## 🎯 生产就绪
|
|
262
|
-
|
|
263
|
-
### 质量保证
|
|
264
|
-
|
|
265
|
-
- ✅ 所有测试通过
|
|
266
|
-
- ✅ 无已知严重 Bug
|
|
267
|
-
- ✅ 文档完整
|
|
268
|
-
- ✅ API 稳定
|
|
269
|
-
|
|
270
|
-
### 性能验证
|
|
271
|
-
|
|
272
|
-
- ✅ 基准测试完成
|
|
273
|
-
- ✅ 性能优于主流验证库
|
|
274
|
-
- ✅ 内存占用合理
|
|
275
|
-
|
|
276
|
-
### 安全性
|
|
277
|
-
|
|
278
|
-
- ✅ 无已知安全漏洞
|
|
279
|
-
- ✅ 输入验证严格
|
|
280
|
-
- ✅ 错误处理完善
|
|
281
|
-
|
|
282
|
-
---
|
|
283
|
-
|
|
284
|
-
## 📦 npm 发布
|
|
285
|
-
|
|
286
|
-
```bash
|
|
287
|
-
npm install schema-dsl
|
|
288
|
-
```
|
|
289
|
-
|
|
290
|
-
**包信息**:
|
|
291
|
-
- 包名: `schema-dsl`
|
|
292
|
-
- 版本: `1.0.0`
|
|
293
|
-
- 大小: ~50KB (未压缩)
|
|
294
|
-
- 许可证: MIT
|
|
295
|
-
|
|
296
|
-
---
|
|
297
|
-
|
|
298
|
-
## 🎉 里程碑
|
|
299
|
-
|
|
300
|
-
v1.0.0 标志着 schema-dsl 的以下成就:
|
|
301
|
-
|
|
302
|
-
1. ✅ **功能完整**: 覆盖所有常见验证场景
|
|
303
|
-
2. ✅ **性能优秀**: 超越主流验证库
|
|
304
|
-
3. ✅ **文档完善**: 40+ 篇详细文档
|
|
305
|
-
4. ✅ **测试充分**: 800+ 测试用例
|
|
306
|
-
5. ✅ **生产就绪**: 可用于生产环境
|
|
307
|
-
|
|
308
|
-
---
|
|
309
|
-
|
|
310
|
-
## 🙏 致谢
|
|
311
|
-
|
|
312
|
-
感谢所有测试用户和贡献者的反馈,让 schema-dsl 达到了生产就绪的质量标准。
|
|
313
|
-
|
|
314
|
-
---
|
|
315
|
-
|
|
316
|
-
## 🔗 相关链接
|
|
317
|
-
|
|
318
|
-
- [GitHub Repository](https://github.com/vextjs/schema-dsl)
|
|
319
|
-
- [npm Package](https://www.npmjs.com/package/schema-dsl)
|
|
320
|
-
- [完整文档](../docs/INDEX.md)
|
|
321
|
-
- [在线体验](https://runkit.com/npm/schema-dsl)
|
|
322
|
-
|
|
323
|
-
---
|
|
324
|
-
|
|
325
|
-
**发布者**: schema-dsl Team
|
|
326
|
-
**发布时间**: 2025-12-29
|
|
327
|
-
**下一版本**: v1.0.1
|
|
328
|
-
|
package/changelogs/v1.0.9.md
DELETED
|
@@ -1,367 +0,0 @@
|
|
|
1
|
-
# v1.0.9 变更日志
|
|
2
|
-
|
|
3
|
-
> **发布日期**: 2026-01-04
|
|
4
|
-
> **版本号**: v1.0.9
|
|
5
|
-
> **类型**: 🎉 重大改进
|
|
6
|
-
|
|
7
|
-
---
|
|
8
|
-
|
|
9
|
-
## 📋 变更概览
|
|
10
|
-
|
|
11
|
-
| 类型 | 数量 | 说明 |
|
|
12
|
-
|------|------|------|
|
|
13
|
-
| 🎉 新功能 | 2 | 完整多语言支持、I18nError类 |
|
|
14
|
-
| ⚡ 功能增强 | 1 | TypeScript类型定义完善 |
|
|
15
|
-
| 📚 文档更新 | 5 | 多语言相关文档 |
|
|
16
|
-
|
|
17
|
-
---
|
|
18
|
-
|
|
19
|
-
## 🎉 新功能
|
|
20
|
-
|
|
21
|
-
### 1. 完整的多语言支持
|
|
22
|
-
|
|
23
|
-
**内置5种语言,开箱即用的国际化方案**
|
|
24
|
-
|
|
25
|
-
#### 支持的语言
|
|
26
|
-
|
|
27
|
-
| 语言代码 | 语言名称 | 完整度 | 说明 |
|
|
28
|
-
|---------|---------|--------|------|
|
|
29
|
-
| zh-CN | 简体中文 | 100% | 默认语言 |
|
|
30
|
-
| en-US | 英文(美国) | 100% | 完整翻译 |
|
|
31
|
-
| ja-JP | 日语 | 95% | 主要功能已翻译 |
|
|
32
|
-
| es-ES | 西班牙语 | 90% | 核心功能已翻译 |
|
|
33
|
-
| fr-FR | 法语 | 90% | 核心功能已翻译 |
|
|
34
|
-
|
|
35
|
-
#### 快速配置
|
|
36
|
-
|
|
37
|
-
**方式1:使用内置语言**
|
|
38
|
-
|
|
39
|
-
```javascript
|
|
40
|
-
const { dsl, validate, Locale } = require('schema-dsl');
|
|
41
|
-
|
|
42
|
-
// 设置全局语言
|
|
43
|
-
Locale.setLocale('zh-CN');
|
|
44
|
-
|
|
45
|
-
const schema = dsl({ username: 'string:3-32!' });
|
|
46
|
-
|
|
47
|
-
// 中文错误消息
|
|
48
|
-
const result = validate(schema, { username: 'ab' });
|
|
49
|
-
console.log(result.errors[0].message);
|
|
50
|
-
// => "username长度不能少于3个字符"
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
**方式2:配置自定义语言包目录**
|
|
54
|
-
|
|
55
|
-
```javascript
|
|
56
|
-
const path = require('path');
|
|
57
|
-
|
|
58
|
-
// 应用启动时配置一次
|
|
59
|
-
dsl.config({
|
|
60
|
-
i18n: path.join(__dirname, 'locales')
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
// 自动加载目录下所有语言文件
|
|
64
|
-
// locales/zh-CN.js
|
|
65
|
-
// locales/en-US.js
|
|
66
|
-
// locales/custom-lang.js
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
#### 自定义语言包
|
|
70
|
-
|
|
71
|
-
```javascript
|
|
72
|
-
// locales/zh-CN.js
|
|
73
|
-
module.exports = {
|
|
74
|
-
'required': '{{#path}} 是必填项',
|
|
75
|
-
'string.min': '{{#path}} 长度不能少于 {{#min}} 个字符',
|
|
76
|
-
'string.max': '{{#path}} 长度不能超过 {{#max}} 个字符',
|
|
77
|
-
'email': '{{#path}} 必须是有效的邮箱地址',
|
|
78
|
-
'account.notFound': '账户不存在',
|
|
79
|
-
'account.insufficientBalance': '余额不足,当前余额{{balance}},需要{{required}}'
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
// locales/en-US.js
|
|
83
|
-
module.exports = {
|
|
84
|
-
'required': '{{#path}} is required',
|
|
85
|
-
'string.min': '{{#path}} must be at least {{#min}} characters',
|
|
86
|
-
'string.max': '{{#path}} must not exceed {{#max}} characters',
|
|
87
|
-
'email': '{{#path}} must be a valid email',
|
|
88
|
-
'account.notFound': 'Account not found',
|
|
89
|
-
'account.insufficientBalance': 'Insufficient balance, current: {{balance}}, required: {{required}}'
|
|
90
|
-
};
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
#### 运行时切换语言
|
|
94
|
-
|
|
95
|
-
```javascript
|
|
96
|
-
// 全局切换
|
|
97
|
-
Locale.setLocale('en-US');
|
|
98
|
-
|
|
99
|
-
// 验证时指定语言
|
|
100
|
-
validate(schema, data, { locale: 'ja-JP' });
|
|
101
|
-
|
|
102
|
-
// 获取当前语言
|
|
103
|
-
const currentLocale = Locale.getLocale(); // 'en-US'
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
#### 参数插值
|
|
107
|
-
|
|
108
|
-
```javascript
|
|
109
|
-
// 自动插值
|
|
110
|
-
const schema = dsl({ age: 'number:18-120!' });
|
|
111
|
-
validate(schema, { age: 15 });
|
|
112
|
-
// => "age must be at least 18" (en-US)
|
|
113
|
-
// => "age 必须至少为 18" (zh-CN)
|
|
114
|
-
|
|
115
|
-
// 自定义参数插值
|
|
116
|
-
const error = new I18nError('account.insufficientBalance', {
|
|
117
|
-
balance: 50,
|
|
118
|
-
required: 100
|
|
119
|
-
});
|
|
120
|
-
console.log(error.message);
|
|
121
|
-
// => "余额不足,当前余额50,需要100" (zh-CN)
|
|
122
|
-
// => "Insufficient balance, current: 50, required: 100" (en-US)
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
---
|
|
126
|
-
|
|
127
|
-
### 2. I18nError 多语言错误类
|
|
128
|
-
|
|
129
|
-
**统一的多语言错误处理类**
|
|
130
|
-
|
|
131
|
-
#### 基础用法
|
|
132
|
-
|
|
133
|
-
```javascript
|
|
134
|
-
const { I18nError } = require('schema-dsl');
|
|
135
|
-
|
|
136
|
-
// 创建错误
|
|
137
|
-
const error = new I18nError(
|
|
138
|
-
'account.notFound', // 错误代码
|
|
139
|
-
{}, // 参数(可选)
|
|
140
|
-
404 // HTTP状态码(可选)
|
|
141
|
-
);
|
|
142
|
-
|
|
143
|
-
console.log(error.code); // 'account.notFound'
|
|
144
|
-
console.log(error.message); // '账户不存在' (zh-CN)
|
|
145
|
-
console.log(error.statusCode); // 404
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
#### 静态方法
|
|
149
|
-
|
|
150
|
-
```javascript
|
|
151
|
-
// 直接抛出错误
|
|
152
|
-
I18nError.throw('user.noPermission');
|
|
153
|
-
|
|
154
|
-
// 断言风格
|
|
155
|
-
I18nError.assert(user, 'user.notFound');
|
|
156
|
-
I18nError.assert(
|
|
157
|
-
user.role === 'admin',
|
|
158
|
-
'user.noPermission'
|
|
159
|
-
);
|
|
160
|
-
|
|
161
|
-
// 创建错误对象
|
|
162
|
-
const error = I18nError.create('account.notFound', {}, 404);
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
#### 错误对象属性
|
|
166
|
-
|
|
167
|
-
```javascript
|
|
168
|
-
const error = new I18nError('account.insufficientBalance', {
|
|
169
|
-
balance: 50,
|
|
170
|
-
required: 100
|
|
171
|
-
}, 400);
|
|
172
|
-
|
|
173
|
-
console.log(error.code); // 'account.insufficientBalance'
|
|
174
|
-
console.log(error.message); // '余额不足,当前余额50,需要100'
|
|
175
|
-
console.log(error.statusCode); // 400
|
|
176
|
-
console.log(error.params); // { balance: 50, required: 100 }
|
|
177
|
-
|
|
178
|
-
// JSON序列化
|
|
179
|
-
console.log(error.toJSON());
|
|
180
|
-
// {
|
|
181
|
-
// code: 'account.insufficientBalance',
|
|
182
|
-
// message: '余额不足,当前余额50,需要100',
|
|
183
|
-
// statusCode: 400,
|
|
184
|
-
// params: { balance: 50, required: 100 }
|
|
185
|
-
// }
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
#### Express/Koa 集成
|
|
189
|
-
|
|
190
|
-
```javascript
|
|
191
|
-
// Express 错误处理中间件
|
|
192
|
-
app.use((error, req, res, next) => {
|
|
193
|
-
if (error instanceof I18nError) {
|
|
194
|
-
return res.status(error.statusCode).json({
|
|
195
|
-
success: false,
|
|
196
|
-
code: error.code,
|
|
197
|
-
message: error.message,
|
|
198
|
-
params: error.params
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
next(error);
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
// 业务代码
|
|
205
|
-
app.post('/api/withdraw', async (req, res, next) => {
|
|
206
|
-
try {
|
|
207
|
-
const account = await getAccount(req.user.id);
|
|
208
|
-
I18nError.assert(account, 'account.notFound', {}, 404);
|
|
209
|
-
|
|
210
|
-
I18nError.assert(
|
|
211
|
-
account.balance >= req.body.amount,
|
|
212
|
-
'account.insufficientBalance',
|
|
213
|
-
{ balance: account.balance, required: req.body.amount },
|
|
214
|
-
400
|
|
215
|
-
);
|
|
216
|
-
|
|
217
|
-
// 处理提现...
|
|
218
|
-
res.json({ success: true });
|
|
219
|
-
} catch (error) {
|
|
220
|
-
next(error);
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
---
|
|
226
|
-
|
|
227
|
-
## ⚡ 功能增强
|
|
228
|
-
|
|
229
|
-
### TypeScript 类型定义完善
|
|
230
|
-
|
|
231
|
-
**完整的类型定义,100% 类型安全**
|
|
232
|
-
|
|
233
|
-
#### 新增类型
|
|
234
|
-
|
|
235
|
-
```typescript
|
|
236
|
-
// Locale 类型
|
|
237
|
-
interface Locale {
|
|
238
|
-
setLocale(locale: string): void;
|
|
239
|
-
getLocale(): string;
|
|
240
|
-
getMessage(code: string, params?: Record<string, any>): string;
|
|
241
|
-
setMessages(messages: Record<string, string>): void;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// I18nError 类型
|
|
245
|
-
class I18nError extends Error {
|
|
246
|
-
code: string;
|
|
247
|
-
statusCode: number;
|
|
248
|
-
params: Record<string, any>;
|
|
249
|
-
|
|
250
|
-
constructor(
|
|
251
|
-
code: string,
|
|
252
|
-
params?: Record<string, any>,
|
|
253
|
-
statusCode?: number
|
|
254
|
-
);
|
|
255
|
-
|
|
256
|
-
static throw(code: string, params?: Record<string, any>, statusCode?: number): never;
|
|
257
|
-
static assert(condition: any, code: string, params?: Record<string, any>, statusCode?: number): asserts condition;
|
|
258
|
-
static create(code: string, params?: Record<string, any>, statusCode?: number): I18nError;
|
|
259
|
-
|
|
260
|
-
toJSON(): object;
|
|
261
|
-
is(code: string): boolean;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// ValidationOptions 扩展
|
|
265
|
-
interface ValidationOptions {
|
|
266
|
-
locale?: string; // 新增:验证时指定语言
|
|
267
|
-
// ...其他选项
|
|
268
|
-
}
|
|
269
|
-
```
|
|
270
|
-
|
|
271
|
-
#### 使用示例
|
|
272
|
-
|
|
273
|
-
```typescript
|
|
274
|
-
import { dsl, validate, I18nError, Locale } from 'schema-dsl';
|
|
275
|
-
|
|
276
|
-
// 完整的类型推导
|
|
277
|
-
const schema = dsl({ email: 'email!' });
|
|
278
|
-
|
|
279
|
-
// 验证时指定语言
|
|
280
|
-
const result = validate(schema, data, { locale: 'en-US' });
|
|
281
|
-
|
|
282
|
-
// I18nError 类型安全
|
|
283
|
-
try {
|
|
284
|
-
I18nError.throw('account.notFound', {}, 404);
|
|
285
|
-
} catch (error) {
|
|
286
|
-
if (error instanceof I18nError) {
|
|
287
|
-
console.log(error.code); // string
|
|
288
|
-
console.log(error.statusCode); // number
|
|
289
|
-
console.log(error.params); // Record<string, any>
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
```
|
|
293
|
-
|
|
294
|
-
---
|
|
295
|
-
|
|
296
|
-
## 📚 文档更新
|
|
297
|
-
|
|
298
|
-
1. ✅ **多语言用户指南**: [docs/i18n-user-guide.md](../docs/i18n-user-guide.md)
|
|
299
|
-
2. ✅ **I18n 完整文档**: [docs/i18n.md](../docs/i18n.md)
|
|
300
|
-
3. ✅ **I18nError API 文档**: [docs/error-handling.md](../docs/error-handling.md)
|
|
301
|
-
4. ✅ **自定义语言包指南**: [docs/add-custom-locale.md](../docs/add-custom-locale.md)
|
|
302
|
-
5. ✅ **前端 i18n 集成**: [docs/frontend-i18n-guide.md](../docs/frontend-i18n-guide.md)
|
|
303
|
-
|
|
304
|
-
---
|
|
305
|
-
|
|
306
|
-
## 🔄 破坏性变更
|
|
307
|
-
|
|
308
|
-
**无破坏性变更** - 所有新功能都是可选的,默认行为保持不变。
|
|
309
|
-
|
|
310
|
-
---
|
|
311
|
-
|
|
312
|
-
## 🎯 升级指南
|
|
313
|
-
|
|
314
|
-
### 从 v1.0.8 升级到 v1.0.9
|
|
315
|
-
|
|
316
|
-
```bash
|
|
317
|
-
npm install schema-dsl@1.0.9
|
|
318
|
-
```
|
|
319
|
-
|
|
320
|
-
**可选:启用多语言**
|
|
321
|
-
|
|
322
|
-
```javascript
|
|
323
|
-
const { Locale } = require('schema-dsl');
|
|
324
|
-
|
|
325
|
-
// 设置全局语言(可选)
|
|
326
|
-
Locale.setLocale('zh-CN');
|
|
327
|
-
|
|
328
|
-
// 或在验证时指定(可选)
|
|
329
|
-
validate(schema, data, { locale: 'en-US' });
|
|
330
|
-
```
|
|
331
|
-
|
|
332
|
-
**可选:使用 I18nError**
|
|
333
|
-
|
|
334
|
-
```javascript
|
|
335
|
-
const { I18nError } = require('schema-dsl');
|
|
336
|
-
|
|
337
|
-
// 替代普通 Error
|
|
338
|
-
I18nError.throw('account.notFound');
|
|
339
|
-
```
|
|
340
|
-
|
|
341
|
-
---
|
|
342
|
-
|
|
343
|
-
## 📦 依赖变更
|
|
344
|
-
|
|
345
|
-
**无依赖变更**
|
|
346
|
-
|
|
347
|
-
---
|
|
348
|
-
|
|
349
|
-
## 🙏 致谢
|
|
350
|
-
|
|
351
|
-
感谢社区贡献者提供的多语言翻译和反馈。
|
|
352
|
-
|
|
353
|
-
---
|
|
354
|
-
|
|
355
|
-
## 🔗 相关链接
|
|
356
|
-
|
|
357
|
-
- [GitHub Repository](https://github.com/vextjs/schema-dsl)
|
|
358
|
-
- [npm Package](https://www.npmjs.com/package/schema-dsl)
|
|
359
|
-
- [完整多语言文档](../docs/i18n.md)
|
|
360
|
-
- [I18nError 示例](../examples/i18n-error.examples.js)
|
|
361
|
-
|
|
362
|
-
---
|
|
363
|
-
|
|
364
|
-
**发布者**: schema-dsl Team
|
|
365
|
-
**发布时间**: 2026-01-04
|
|
366
|
-
**下一版本**: v1.1.0
|
|
367
|
-
|