schema-dsl 1.0.4 → 1.0.6

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,9 @@
11
11
 
12
12
  | 版本 | 日期 | 变更摘要 | 详细 |
13
13
  |------|------|---------|------|
14
+ | [v1.0.6](#v106) | 2026-01-04 | 🚨 紧急修复:TypeScript 类型污染 | [查看详情](#v106) |
15
+ | [v1.0.5](#v105) | 2026-01-04 | 测试覆盖率提升至 97% | [查看详情](#v105) |
16
+ | [v1.0.5](#v105) | 2026-01-04 | 测试覆盖率提升至 97% | [查看详情](#v105) |
14
17
  | [v1.0.4](#v104) | 2025-12-31 | TypeScript 完整支持、validateAsync、ValidationError | [查看详情](#v104) |
15
18
  | [v1.0.3](#v103) | 2025-12-31 | ⚠️ 破坏性变更:单值语法修复 | [查看详情](#v103) |
16
19
  | [v1.0.2](#v102) | 2025-12-31 | 15个新增验证器、完整文档、75个测试 | [查看详情](#v102) |
@@ -19,6 +22,79 @@
19
22
 
20
23
  ---
21
24
 
25
+ ## [v1.0.6] - 2026-01-04
26
+
27
+ ### 🚨 Fixed (紧急修复)
28
+
29
+ #### TypeScript 类型污染问题 ⭐⭐⭐
30
+
31
+ **问题描述**:
32
+ - v1.0.5 及更早版本中,`index.d.ts` 包含了全局 `interface String` 扩展(第 816-1065 行,共 209 行代码)
33
+ - 这导致 TypeScript 全局类型系统污染,原生 `String.prototype` 方法的类型被错误推断
34
+ - **严重问题**:`String.prototype.trim()` 返回类型从 `string` 被错误推断为 `DslBuilder`
35
+ - 影响所有使用 TypeScript 的项目,导致类型安全问题
36
+
37
+ **修复内容**:
38
+ - ✅ **完全删除**全局 `interface String` 扩展(移除 209 行类型污染代码)
39
+ - ✅ 添加详细的 TypeScript 使用说明文档
40
+ - ✅ 保证原生 String 方法的类型推断正确(`trim()` 正确返回 `string`)
41
+
42
+ **对用户的影响**:
43
+
44
+ 1. **JavaScript 用户** ✅ **完全不受影响**
45
+ ```javascript
46
+ // 仍然可以正常使用 String 扩展
47
+ const schema = dsl({ email: 'email!'.label('邮箱') });
48
+ ```
49
+
50
+ 2. **TypeScript 用户** ⚠️ **需要调整用法**
51
+ ```typescript
52
+ // ❌ v1.0.5 及之前(有类型污染 bug)
53
+ const schema = dsl({ email: 'email!'.label('邮箱') });
54
+
55
+ // ✅ v1.0.6 推荐写法(获得正确类型提示)
56
+ const schema = dsl({
57
+ email: dsl('email!').label('邮箱')
58
+ });
59
+ ```
60
+
61
+ **技术细节**:
62
+ - 文件变化:`index.d.ts` 从 2958 行减少到 2749 行
63
+ - 测试状态:所有 677 个测试通过 ✅
64
+ - 类型验证:原生 `String.prototype.trim()` 现在正确返回 `string` 类型
65
+
66
+ **迁移指南**:
67
+ - JavaScript 项目:无需任何修改
68
+ - TypeScript 项目:使用 `dsl()` 函数包裹字符串字面量获得类型提示
69
+ - 详见:[TypeScript 使用指南](./docs/typescript-guide.md)
70
+
71
+ ---
72
+
73
+ ## [v1.0.5] - 2026-01-04
74
+
75
+ ### Added (新增功能)
76
+
77
+ #### 测试覆盖率大幅提升 ⭐
78
+
79
+ - ✅ **新增 5 个核心类的完整测试**
80
+ - `CacheManager.test.js` - 24 个测试(缓存管理)
81
+ - `ErrorFormatter.test.js` - 9 个测试(错误格式化)
82
+ - `JSONSchemaCore.test.js` - 10 个测试(JSON Schema 核心)
83
+ - `MarkdownExporter.test.js` - 3 个测试(Markdown 导出)
84
+ - `ErrorCodes.test.js` - 4 个测试(错误代码)
85
+
86
+ - ✅ **测试统计**
87
+ - 总测试数:651 → 677(+26 个测试)
88
+ - 测试覆盖率:92% → 97%(+5%)
89
+ - 所有核心类现在都有完整测试覆盖
90
+
91
+ ### Fixed (修复)
92
+
93
+ - ✅ 修复缓存配置支持(4 个参数完整支持)
94
+ - ✅ 简化 i18n 配置(从 3 个方法简化为 2 个)
95
+
96
+ ---
97
+
22
98
  ## [v1.0.4] - 2025-12-31
23
99
 
24
100
  ### Added (新增功能)
package/README.md CHANGED
@@ -96,6 +96,8 @@ validate(schema, data, { locale: 'en-US' });
96
96
  **一份定义,多处使用**
97
97
 
98
98
  ```javascript
99
+ const { dsl, exporters } = require('schema-dsl');
100
+
99
101
  const schema = dsl({
100
102
  username: 'string:3-32!',
101
103
  email: 'email!',
@@ -103,13 +105,16 @@ const schema = dsl({
103
105
  });
104
106
 
105
107
  // 导出 MongoDB Schema
106
- schema.exportTo('mongodb', { collectionName: 'users' });
108
+ const mongoExporter = new exporters.MongoDBExporter();
109
+ const mongoSchema = mongoExporter.export(schema);
107
110
 
108
111
  // 导出 MySQL 建表语句
109
- schema.exportTo('mysql', { tableName: 'users' });
112
+ const mysqlExporter = new exporters.MySQLExporter();
113
+ const mysqlDDL = mysqlExporter.export('users', schema);
110
114
 
111
115
  // 导出 PostgreSQL 建表语句
112
- schema.exportTo('postgresql', { tableName: 'users' });
116
+ const pgExporter = new exporters.PostgreSQLExporter();
117
+ const pgDDL = pgExporter.export('users', schema);
113
118
  ```
114
119
 
115
120
  **✅ 独家功能**:从验证规则直接生成数据库结构!
@@ -215,12 +220,12 @@ console.log(result2.errors); // 错误列表
215
220
 
216
221
  ### 1.5 TypeScript 用法 ⭐
217
222
 
218
- **重要**: TypeScript 中使用链式调用需要用 `dsl()` 包裹字符串以获得完整的类型推导:
223
+ **重要**: TypeScript 中**必须**使用 `dsl()` 包裹字符串以获得类型提示(v1.0.6+ 移除了全局 String 类型扩展以避免类型污染):
219
224
 
220
225
  ```typescript
221
226
  import { dsl, validateAsync, ValidationError } from 'schema-dsl';
222
227
 
223
- // ✅ 推荐:使用 dsl() 包裹字符串获得完整类型提示
228
+ // ✅ 正确:使用 dsl() 包裹字符串获得完整类型提示
224
229
  const userSchema = dsl({
225
230
  username: dsl('string:3-32!')
226
231
  .pattern(/^[a-zA-Z0-9_]+$/, '只能包含字母、数字和下划线')
@@ -251,10 +256,13 @@ try {
251
256
  }
252
257
  ```
253
258
 
254
- **为什么要用 `dsl()` 包裹?**
259
+ **为什么必须用 `dsl()` 包裹?**
255
260
  - ✅ 完整的类型推导和 IDE 自动提示
256
- - ✅ 避免 TypeScript 严格模式警告
257
- - ✅ 更好的开发体验
261
+ - ✅ 避免污染原生 String 类型(v1.0.6+ 重要改进)
262
+ - ✅ 保证 `trim()`、`toLowerCase()` 等原生方法类型正确
263
+ - ✅ 更好的开发体验和类型安全
264
+
265
+ **JavaScript 用户不受影响**:在 JavaScript 中仍然可以直接使用 `'email!'.label('邮箱')` 语法。
258
266
 
259
267
  **详细说明**: 请查看 [TypeScript 使用指南](./docs/typescript-guide.md)
260
268
 
@@ -525,17 +533,82 @@ dsl({
525
533
  email: 'email!'.label('用户邮箱'),
526
534
 
527
535
  // 字段描述
528
- bio: 'string:10-500'.description('用户简介,10-500字符'),
536
+ bio: 'string:10-500'.description('用户简介,10-500字符')
537
+ })
538
+ ```
539
+
540
+ ### 条件验证 - dsl.match 和 dsl.if
541
+
542
+ **根据其他字段的值动态决定验证规则**
543
+
544
+ ```javascript
545
+ const { dsl } = require('schema-dsl');
546
+
547
+ // 1. dsl.match - 根据字段值匹配不同规则(类似 switch-case)
548
+ const contactSchema = dsl({
549
+ contactType: 'email|phone|wechat',
529
550
 
530
- // 条件验证
531
- discount: 'number'.when('vip', {
532
- is: true,
533
- then: 'number:10-50!', // VIP 用户折扣必填
534
- otherwise: 'number' // 普通用户可选
551
+ // 根据 contactType 的值决定 contact 字段的验证规则
552
+ contact: dsl.match('contactType', {
553
+ email: 'email!', // contactType='email' 时验证邮箱格式
554
+ phone: 'string:11!', // contactType='phone' 时验证11位手机号
555
+ wechat: 'string:6-20!', // contactType='wechat' 时验证微信号
556
+ _default: 'string' // 默认规则(可选)
535
557
  })
536
- })
558
+ });
559
+
560
+ // ✅ 验证通过
561
+ validate(contactSchema, { contactType: 'email', contact: 'user@example.com' });
562
+ validate(contactSchema, { contactType: 'phone', contact: '13800138000' });
563
+
564
+ // ❌ 验证失败
565
+ validate(contactSchema, { contactType: 'email', contact: 'invalid' });
566
+
567
+
568
+ // 2. dsl.if - 简单条件分支(类似 if-else)
569
+ const vipSchema = dsl({
570
+ isVip: 'boolean!',
571
+
572
+ // 如果是 VIP,折扣必须在 10-50 之间;否则在 0-10 之间
573
+ discount: dsl.if('isVip', 'number:10-50!', 'number:0-10')
574
+ });
575
+
576
+ // ✅ VIP 用户
577
+ validate(vipSchema, { isVip: true, discount: 30 });
578
+
579
+ // ❌ 非 VIP 用户折扣超过 10
580
+ validate(vipSchema, { isVip: false, discount: 15 });
581
+
582
+
583
+ // 3. 实际应用场景:订单验证
584
+ const orderSchema = dsl({
585
+ paymentMethod: 'alipay|wechat|card|cod', // cod = 货到付款
586
+
587
+ // 根据支付方式决定支付信息格式
588
+ paymentInfo: dsl.match('paymentMethod', {
589
+ alipay: 'email!', // 支付宝:邮箱
590
+ wechat: 'string:20-30', // 微信:支付串
591
+ card: 'string:16-19', // 银行卡:卡号
592
+ cod: 'string:0-0', // 货到付款:无需支付信息
593
+ _default: 'string'
594
+ }),
595
+
596
+ // 货到付款需要详细地址
597
+ address: dsl.if('paymentMethod',
598
+ 'string:10-200!', // cod = 货到付款时地址必填
599
+ 'string:10-200' // 其他支付方式地址可选
600
+ )
601
+ });
537
602
  ```
538
603
 
604
+ **💡 使用场景**:
605
+ - ✅ 多种联系方式验证(邮箱/手机/微信)
606
+ - ✅ VIP 和普通用户不同的折扣范围
607
+ - ✅ 不同支付方式的支付信息格式
608
+ - ✅ 根据用户类型决定必填字段
609
+
610
+ **查看完整示例**: [examples/dsl-match-example.js](./examples/dsl-match-example.js)
611
+
539
612
  ---
540
613
 
541
614
  ## 🔧 核心功能
@@ -653,32 +726,36 @@ const markdown = exporters.MarkdownExporter.export(userSchema, {
653
726
  ### 4. 多语言支持
654
727
 
655
728
  ```javascript
656
- const { dsl, Locale } = require('schema-dsl');
729
+ const { dsl, validate } = require('schema-dsl');
730
+ const path = require('path');
731
+
732
+ // 方式 1: 从目录加载语言包(推荐)
733
+ dsl.config({
734
+ i18n: path.join(__dirname, 'i18n/dsl') // 直接传字符串路径
735
+ });
657
736
 
658
- // 配置语言包
737
+ // 方式 2: 直接传入语言包对象
659
738
  dsl.config({
660
739
  i18n: {
661
- locales: {
662
- 'zh-CN': {
663
- 'label.username': '用户名',
664
- 'label.email': '邮箱地址',
665
- 'required': '{{#label}}不能为空',
666
- 'string.min': '{{#label}}长度不能少于{{#limit}}个字符'
667
- },
668
- 'en-US': {
669
- 'label.username': 'Username',
670
- 'label.email': 'Email Address',
671
- 'required': '{{#label}} is required',
672
- 'string.min': '{{#label}} must be at least {{#limit}} characters'
673
- }
740
+ 'zh-CN': {
741
+ 'label.username': '用户名',
742
+ 'label.email': '邮箱地址',
743
+ 'required': '{{#label}}不能为空',
744
+ 'string.min': '{{#label}}长度不能少于{{#limit}}个字符'
745
+ },
746
+ 'en-US': {
747
+ 'label.username': 'Username',
748
+ 'label.email': 'Email Address',
749
+ 'required': '{{#label}} is required',
750
+ 'string.min': '{{#label}} must be at least {{#limit}} characters'
674
751
  }
675
752
  }
676
753
  });
677
754
 
678
755
  // 使用 Label Key
679
756
  const schema = dsl({
680
- username: 'string:3-32!'.label('label.username'),
681
- email: 'email!'.label('label.email')
757
+ username: dsl('string:3-32!').label('label.username'),
758
+ email: dsl('email!').label('label.email')
682
759
  });
683
760
 
684
761
  // 验证时指定语言
@@ -687,16 +764,44 @@ const result1 = validate(schema, data, { locale: 'zh-CN' });
687
764
 
688
765
  const result2 = validate(schema, data, { locale: 'en-US' });
689
766
  // 错误消息:Username must be at least 3 characters
767
+ ```
690
768
 
691
- // 从文件加载语言包
692
- dsl.config({
693
- i18n: {
694
- localesPath: './i18n' // 自动加载 ./i18n/*.js *.json
769
+ ### 5. 缓存配置 (v1.0.4+)
770
+
771
+ ```javascript
772
+ const { dsl, config } = require('schema-dsl');
773
+
774
+ // 配置缓存选项(推荐在使用 DSL 之前调用)
775
+ config({
776
+ cache: {
777
+ maxSize: 1000, // 最大缓存条目数(默认:100)
778
+ ttl: 7200000, // 缓存过期时间(毫秒,默认:3600000,即1小时)
779
+ enabled: true, // 是否启用缓存(默认:true)
780
+ statsEnabled: true // 是否启用统计(默认:true)
695
781
  }
696
782
  });
783
+
784
+ // 之后创建的 Schema 将使用新的缓存配置
785
+ const schema = dsl({ name: 'string!' });
786
+
787
+ // 也可以在 Validator 创建后动态修改配置(向后兼容)
788
+ const { getDefaultValidator } = require('schema-dsl');
789
+ const validator = getDefaultValidator();
790
+ console.log('当前缓存配置:', validator.cache.options);
791
+
792
+ // 动态修改
793
+ config({
794
+ cache: { maxSize: 5000 } // 只修改某个参数
795
+ });
697
796
  ```
698
797
 
699
- ### 5. 插件系统
798
+ **缓存说明**:
799
+ - Schema 编译结果会被缓存以提高性能
800
+ - 使用 LRU(最近最少使用)淘汰策略
801
+ - 支持 TTL(生存时间)自动过期
802
+ - 可通过 `validator.cache.getStats()` 查看缓存统计信息
803
+
804
+ ### 6. 插件系统
700
805
 
701
806
  ```javascript
702
807
  const { PluginManager } = require('schema-dsl');
@@ -748,7 +853,7 @@ const schema = dsl({
748
853
  });
749
854
  ```
750
855
 
751
- ### 6. 错误处理
856
+ ### 7. 错误处理
752
857
 
753
858
  ```javascript
754
859
  const { validate, ValidationError } = require('schema-dsl');
package/docs/i18n.md CHANGED
@@ -19,12 +19,12 @@ schema-dsl 支持完整的多语言功能,允许你自定义字段标签和错
19
19
  const { dsl, validate } = require('schema-dsl');
20
20
  const path = require('path');
21
21
 
22
- // 方式 1: 从目录加载(推荐)
22
+ // 方式 1: 从目录加载(推荐)
23
23
  dsl.config({
24
- i18n: path.join(__dirname, 'i18n/dsl')
24
+ i18n: path.join(__dirname, 'i18n/dsl') // 直接传字符串路径
25
25
  });
26
26
 
27
- // 方式 2: 直接传入对象
27
+ // 方式 2: 直接传入对象
28
28
  dsl.config({
29
29
  i18n: {
30
30
  'zh-CN': require('./i18n/dsl/zh-CN'),
@@ -3,7 +3,7 @@
3
3
  ## 📋 测试概述
4
4
 
5
5
  **测试日期**: 2024-01-XX
6
- **测试版本**: v1.0.3
6
+ **测试版本**: v1.0.4
7
7
  **测试环境**: Node.js (运行环境)
8
8
  **对比库**: Joi, Yup, Zod, Ajv
9
9
 
@@ -1,7 +1,8 @@
1
1
  # TypeScript 使用指南
2
2
 
3
- > **版本**: schema-dsl v1.0.3+
4
- > **更新日期**: 2025-12-31
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
- 由于 TypeScript 对全局 `String.prototype` 扩展的类型推导限制,在 `.ts` 文件中直接使用字符串链式调用可能会缺少类型提示:
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
- **使用 `dsl()` 函数包裹字符串**,可以获得完整的类型推导和 IDE 提示:
80
+ **TypeScript 中必须使用 `dsl()` 函数包裹字符串**,才能获得类型提示和链式调用:
70
81
 
71
82
  ```typescript
72
- // ✅ 推荐:使用 dsl() 包裹
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
@@ -550,5 +570,5 @@ dsl.config({
550
570
  ---
551
571
 
552
572
  **更新日期**: 2025-12-31
553
- **文档版本**: v1.0.3
573
+ **文档版本**: v1.0.4
554
574
 
@@ -474,7 +474,7 @@ new ValidationError(errors, data)
474
474
 
475
475
  ---
476
476
 
477
- **版本**: v1.0.3
477
+ **版本**: v1.0.4
478
478
  **更新日期**: 2025-12-29
479
479
  **作者**: SchemaIO Team
480
480
 
@@ -61,10 +61,17 @@ const schema = {
61
61
  }
62
62
  };
63
63
 
64
- // 方式2: DSL 语法 (推荐)
64
+ // 方式2: DSL 语法 + 约束(v1.0.3+)
65
65
  const schema2 = dsl({
66
- code: 'string!',
67
- exactLength: 6
66
+ code: 'string:6!' // 精确长度 6
67
+ });
68
+
69
+ // 方式3: 链式调用 JSON Schema
70
+ const schema3 = dsl({
71
+ code: {
72
+ type: 'string',
73
+ exactLength: 6
74
+ }
68
75
  });
69
76
  ```
70
77