schema-dsl 1.0.0

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.
Files changed (116) hide show
  1. package/.eslintignore +10 -0
  2. package/.eslintrc.json +27 -0
  3. package/.github/CODE_OF_CONDUCT.md +45 -0
  4. package/.github/ISSUE_TEMPLATE/bug_report.md +57 -0
  5. package/.github/ISSUE_TEMPLATE/config.yml +11 -0
  6. package/.github/ISSUE_TEMPLATE/feature_request.md +45 -0
  7. package/.github/ISSUE_TEMPLATE/question.md +31 -0
  8. package/.github/PULL_REQUEST_TEMPLATE.md +70 -0
  9. package/.github/SECURITY.md +184 -0
  10. package/.github/workflows/ci.yml +33 -0
  11. package/CHANGELOG.md +633 -0
  12. package/CONTRIBUTING.md +368 -0
  13. package/LICENSE +21 -0
  14. package/README.md +1184 -0
  15. package/STATUS.md +101 -0
  16. package/docs/FEATURE-INDEX.md +519 -0
  17. package/docs/INDEX.md +253 -0
  18. package/docs/api-reference.md +1096 -0
  19. package/docs/best-practices.md +672 -0
  20. package/docs/cache-manager.md +336 -0
  21. package/docs/design-philosophy.md +601 -0
  22. package/docs/dsl-syntax.md +653 -0
  23. package/docs/dynamic-locale.md +552 -0
  24. package/docs/error-handling.md +703 -0
  25. package/docs/export-guide.md +462 -0
  26. package/docs/export-limitations.md +551 -0
  27. package/docs/faq.md +577 -0
  28. package/docs/frontend-i18n-guide.md +290 -0
  29. package/docs/i18n-user-guide.md +476 -0
  30. package/docs/label-vs-description.md +262 -0
  31. package/docs/markdown-exporter.md +397 -0
  32. package/docs/mongodb-exporter.md +295 -0
  33. package/docs/multi-type-support.md +319 -0
  34. package/docs/mysql-exporter.md +273 -0
  35. package/docs/plugin-system.md +542 -0
  36. package/docs/postgresql-exporter.md +304 -0
  37. package/docs/quick-start.md +761 -0
  38. package/docs/schema-helper.md +340 -0
  39. package/docs/schema-utils-chaining.md +143 -0
  40. package/docs/schema-utils.md +490 -0
  41. package/docs/string-extensions.md +480 -0
  42. package/docs/troubleshooting.md +471 -0
  43. package/docs/type-converter.md +319 -0
  44. package/docs/type-reference.md +219 -0
  45. package/docs/validate-async.md +480 -0
  46. package/docs/validate.md +486 -0
  47. package/docs/validation-guide.md +484 -0
  48. package/examples/array-dsl-example.js +227 -0
  49. package/examples/custom-extension.js +85 -0
  50. package/examples/dsl-match-example.js +74 -0
  51. package/examples/dsl-style.js +118 -0
  52. package/examples/dynamic-locale-configuration.js +348 -0
  53. package/examples/dynamic-locale-example.js +287 -0
  54. package/examples/export-demo.js +130 -0
  55. package/examples/express-integration.js +376 -0
  56. package/examples/i18n-full-demo.js +310 -0
  57. package/examples/i18n-memory-safety.examples.js +268 -0
  58. package/examples/markdown-export.js +71 -0
  59. package/examples/middleware-usage.js +93 -0
  60. package/examples/new-features-comparison.js +315 -0
  61. package/examples/password-reset/README.md +153 -0
  62. package/examples/password-reset/schema.js +26 -0
  63. package/examples/password-reset/test.js +101 -0
  64. package/examples/plugin-system.examples.js +205 -0
  65. package/examples/schema-utils-chaining.examples.js +250 -0
  66. package/examples/simple-example.js +122 -0
  67. package/examples/string-extensions.js +297 -0
  68. package/examples/user-registration/README.md +156 -0
  69. package/examples/user-registration/routes.js +92 -0
  70. package/examples/user-registration/schema.js +150 -0
  71. package/examples/user-registration/server.js +74 -0
  72. package/index.d.ts +1999 -0
  73. package/index.js +282 -0
  74. package/index.mjs +30 -0
  75. package/lib/adapters/DslAdapter.js +699 -0
  76. package/lib/adapters/index.js +20 -0
  77. package/lib/config/constants.js +286 -0
  78. package/lib/config/patterns/creditCard.js +9 -0
  79. package/lib/config/patterns/idCard.js +9 -0
  80. package/lib/config/patterns/index.js +8 -0
  81. package/lib/config/patterns/licensePlate.js +4 -0
  82. package/lib/config/patterns/passport.js +4 -0
  83. package/lib/config/patterns/phone.js +9 -0
  84. package/lib/config/patterns/postalCode.js +5 -0
  85. package/lib/core/CacheManager.js +376 -0
  86. package/lib/core/DslBuilder.js +740 -0
  87. package/lib/core/ErrorCodes.js +233 -0
  88. package/lib/core/ErrorFormatter.js +342 -0
  89. package/lib/core/JSONSchemaCore.js +347 -0
  90. package/lib/core/Locale.js +119 -0
  91. package/lib/core/MessageTemplate.js +89 -0
  92. package/lib/core/PluginManager.js +448 -0
  93. package/lib/core/StringExtensions.js +209 -0
  94. package/lib/core/Validator.js +376 -0
  95. package/lib/errors/ValidationError.js +191 -0
  96. package/lib/exporters/MarkdownExporter.js +420 -0
  97. package/lib/exporters/MongoDBExporter.js +162 -0
  98. package/lib/exporters/MySQLExporter.js +212 -0
  99. package/lib/exporters/PostgreSQLExporter.js +289 -0
  100. package/lib/exporters/index.js +24 -0
  101. package/lib/locales/en-US.js +65 -0
  102. package/lib/locales/es-ES.js +66 -0
  103. package/lib/locales/fr-FR.js +66 -0
  104. package/lib/locales/index.js +8 -0
  105. package/lib/locales/ja-JP.js +66 -0
  106. package/lib/locales/zh-CN.js +93 -0
  107. package/lib/utils/LRUCache.js +174 -0
  108. package/lib/utils/SchemaHelper.js +240 -0
  109. package/lib/utils/SchemaUtils.js +445 -0
  110. package/lib/utils/TypeConverter.js +245 -0
  111. package/lib/utils/index.js +13 -0
  112. package/lib/validators/CustomKeywords.js +203 -0
  113. package/lib/validators/index.js +11 -0
  114. package/package.json +70 -0
  115. package/plugins/custom-format.js +101 -0
  116. package/plugins/custom-validator.js +200 -0
@@ -0,0 +1,205 @@
1
+ /**
2
+ * 插件系统使用示例
3
+ */
4
+
5
+ const { dsl, validate, PluginManager } = require('../index');
6
+
7
+ // ========== 1. 基础使用 ==========
8
+
9
+ console.log('=== 1. 基础使用 ===\n');
10
+
11
+ // 创建插件管理器
12
+ const pluginManager = new PluginManager();
13
+
14
+ // 注册插件
15
+ const customValidatorPlugin = require('../plugins/custom-validator');
16
+ pluginManager.register(customValidatorPlugin);
17
+
18
+ const customFormatPlugin = require('../plugins/custom-format');
19
+ pluginManager.register(customFormatPlugin);
20
+
21
+ // 安装插件
22
+ const schemaDsl = require('../index');
23
+ pluginManager.install(schemaDsl);
24
+
25
+ console.log('已安装插件:', pluginManager.list());
26
+ console.log('');
27
+
28
+ // ========== 2. 使用自定义格式 ==========
29
+
30
+ console.log('=== 2. 使用自定义格式 ===\n');
31
+
32
+ const contactSchema = dsl({
33
+ phone: 'string!',
34
+ email: 'email!',
35
+ wechat: 'string'
36
+ });
37
+
38
+ // 测试数据
39
+ const validContact = {
40
+ phone: '13800138000',
41
+ email: 'test@example.com',
42
+ wechat: 'my_wechat_id'
43
+ };
44
+
45
+ const result1 = validate(contactSchema, validContact);
46
+ console.log('验证结果:', result1.valid ? '✅ 通过' : '❌ 失败');
47
+ if (!result1.valid) {
48
+ console.log('错误:', result1.errors);
49
+ }
50
+ console.log('');
51
+
52
+ // ========== 3. 使用钩子系统 ==========
53
+
54
+ console.log('=== 3. 使用钩子系统 ===\n');
55
+
56
+ // 注册钩子
57
+ pluginManager.hook('onBeforeValidate', (schema, data) => {
58
+ console.log('[Hook] 验证前:', { schema: '...', data });
59
+ });
60
+
61
+ pluginManager.hook('onAfterValidate', (result) => {
62
+ console.log('[Hook] 验证后:', { valid: result.valid });
63
+ });
64
+
65
+ // 运行钩子
66
+ const testSchema = dsl({ name: 'string!' });
67
+ const testData = { name: 'John' };
68
+
69
+ pluginManager.runHook('onBeforeValidate', testSchema, testData);
70
+ const result2 = validate(testSchema, testData);
71
+ pluginManager.runHook('onAfterValidate', result2);
72
+ console.log('');
73
+
74
+ // ========== 4. 自定义插件 ==========
75
+
76
+ console.log('=== 4. 自定义插件 ===\n');
77
+
78
+ // 创建自定义插件
79
+ const myPlugin = {
80
+ name: 'my-plugin',
81
+ version: '1.0.0',
82
+ description: '我的自定义插件',
83
+
84
+ install(schemaDsl, options, context) {
85
+ console.log('[Plugin] my-plugin 安装中...');
86
+ console.log(' 选项:', options);
87
+ console.log(' 已注册插件数:', context.plugins.size);
88
+
89
+ // 添加自定义方法
90
+ schemaDsl.myCustomMethod = () => {
91
+ console.log(' 这是自定义方法!');
92
+ };
93
+ },
94
+
95
+ uninstall(schemaDsl, context) {
96
+ console.log('[Plugin] my-plugin 卸载中...');
97
+ delete schemaDsl.myCustomMethod;
98
+ },
99
+
100
+ hooks: {
101
+ onBeforeValidate(schema, data) {
102
+ console.log(' [my-plugin] 验证前钩子');
103
+ }
104
+ }
105
+ };
106
+
107
+ // 注册并安装
108
+ pluginManager.register(myPlugin);
109
+ pluginManager.install(schemaDsl, 'my-plugin', { custom: true });
110
+
111
+ // 使用自定义方法
112
+ if (schemaDsl.myCustomMethod) {
113
+ schemaDsl.myCustomMethod();
114
+ }
115
+ console.log('');
116
+
117
+ // ========== 5. 插件管理 ==========
118
+
119
+ console.log('=== 5. 插件管理 ===\n');
120
+
121
+ // 查看所有插件
122
+ console.log('所有插件:', pluginManager.list());
123
+
124
+ // 检查插件是否存在
125
+ console.log('my-plugin 是否存在:', pluginManager.has('my-plugin'));
126
+
127
+ // 获取插件数量
128
+ console.log('插件数量:', pluginManager.size);
129
+ console.log('');
130
+
131
+ // ========== 6. 实用插件示例 ==========
132
+
133
+ console.log('=== 6. 实用插件示例 ===\n');
134
+
135
+ // 日志插件
136
+ const loggingPlugin = {
137
+ name: 'logging',
138
+ version: '1.0.0',
139
+
140
+ install(schemaDsl, options, context) {
141
+ // 包装 validate 方法
142
+ const originalValidate = schemaDsl.validate;
143
+ schemaDsl.validate = function (...args) {
144
+ console.log('[Logging] 开始验证');
145
+ const start = Date.now();
146
+ const result = originalValidate.apply(this, args);
147
+ const duration = Date.now() - start;
148
+ console.log(`[Logging] 验证完成,耗时 ${duration}ms,结果: ${result.valid ? '通过' : '失败'}`);
149
+ return result;
150
+ };
151
+ }
152
+ };
153
+
154
+ pluginManager.register(loggingPlugin);
155
+ pluginManager.install(schemaDsl, 'logging');
156
+
157
+ // 测试日志插件
158
+ const schema = dsl({ email: 'email!' });
159
+ validate(schema, { email: 'test@example.com' });
160
+ console.log('');
161
+
162
+ // ========== 7. 卸载插件 ==========
163
+
164
+ console.log('=== 7. 卸载插件 ===\n');
165
+
166
+ pluginManager.uninstall('my-plugin', schemaDsl);
167
+ console.log('my-plugin 已卸载');
168
+ console.log('当前插件数:', pluginManager.size);
169
+ console.log('');
170
+
171
+ // ========== 8. 批量插件 ==========
172
+
173
+ console.log('=== 8. 批量插件 ===\n');
174
+
175
+ // 清空所有插件
176
+ pluginManager.clear(schemaDsl);
177
+ console.log('所有插件已清空');
178
+ console.log('当前插件数:', pluginManager.size);
179
+ console.log('');
180
+
181
+ // 批量注册多个插件
182
+ const plugins = [
183
+ customValidatorPlugin,
184
+ customFormatPlugin
185
+ ];
186
+
187
+ plugins.forEach(plugin => pluginManager.register(plugin));
188
+ pluginManager.install(schemaDsl); // 安装所有插件
189
+
190
+ console.log('批量安装完成:', pluginManager.list());
191
+ console.log('');
192
+
193
+ // ========== 总结 ==========
194
+
195
+ console.log('=== 总结 ===\n');
196
+ console.log('✅ 插件系统支持:');
197
+ console.log(' - 动态注册/卸载插件');
198
+ console.log(' - 生命周期钩子');
199
+ console.log(' - 自定义验证器和格式');
200
+ console.log(' - 插件间通信');
201
+ console.log(' - 事件监听');
202
+ console.log('');
203
+ console.log('示例运行完成!');
204
+
205
+
@@ -0,0 +1,250 @@
1
+ /**
2
+ * SchemaUtils 核心方法示例
3
+ *
4
+ * 展示 v2.1.0 简化后的核心 4 个方法:
5
+ * 1. omit() - 排除字段
6
+ * 2. pick() - 保留字段
7
+ * 3. partial() - 部分验证
8
+ * 4. extend() - 扩展字段
9
+ *
10
+ * @version 2.1.0 (简化版)
11
+ * @date 2025-12-29
12
+ */
13
+
14
+ const { dsl, validate, SchemaUtils } = require('../index');
15
+
16
+ console.log('========================================');
17
+ console.log(' SchemaUtils 核心方法示例');
18
+ console.log('========================================\n');
19
+
20
+ // ===== 定义基础 Schema =====
21
+
22
+ const fullUserSchema = dsl({
23
+ id: 'objectId!',
24
+ name: 'string:1-50!',
25
+ email: 'email!',
26
+ password: 'string:8-32!',
27
+ age: 'integer:18-120',
28
+ role: 'admin|user|guest',
29
+ createdAt: 'date',
30
+ updatedAt: 'date'
31
+ });
32
+
33
+ console.log('基础 Schema 字段:', Object.keys(fullUserSchema.properties));
34
+ console.log('必填字段:', fullUserSchema.required);
35
+ console.log('');
36
+
37
+ // ===== 1. omit() - 排除字段 =====
38
+
39
+ console.log('========== 1. omit() - 排除字段 ==========\n');
40
+
41
+ const publicSchema = SchemaUtils.omit(fullUserSchema, ['password', 'createdAt', 'updatedAt']);
42
+
43
+ console.log('原 Schema 字段:', Object.keys(fullUserSchema.properties));
44
+ console.log('omit 后字段:', Object.keys(publicSchema.properties));
45
+ console.log('password 字段被移除:', publicSchema.properties.password === undefined);
46
+ console.log('');
47
+
48
+ // ===== 2. pick() - 保留字段 =====
49
+
50
+ console.log('========== 2. pick() - 保留字段 ==========\n');
51
+
52
+ const nameEmailSchema = SchemaUtils.pick(fullUserSchema, ['name', 'email']);
53
+
54
+ console.log('原 Schema 字段:', Object.keys(fullUserSchema.properties));
55
+ console.log('pick 后字段:', Object.keys(nameEmailSchema.properties));
56
+ console.log('只保留 name 和 email');
57
+ console.log('');
58
+
59
+ // ===== 3. partial() - 部分验证 =====
60
+
61
+ console.log('========== 3. partial() - 部分验证 ==========\n');
62
+
63
+ const partialSchema = SchemaUtils.partial(fullUserSchema);
64
+
65
+ console.log('原 Schema 必填字段:', fullUserSchema.required);
66
+ console.log('partial Schema 必填字段:', partialSchema.required);
67
+
68
+ // 测试:缺少必填字段
69
+ const result4 = validate(partialSchema, {
70
+ name: 'John'
71
+ // 缺少其他必填字段
72
+ });
73
+
74
+ console.log('\n验证结果(缺少必填字段):');
75
+ console.log(' valid:', result4.valid);
76
+ if (result4.valid) {
77
+ console.log(' 数据:', result4.data);
78
+ }
79
+ console.log('');
80
+
81
+ // 部分验证 - 只验证指定字段
82
+ const nameAgeSchema = SchemaUtils.partial(fullUserSchema, ['name', 'age']);
83
+
84
+ console.log('partial(schema, [name, age]) 保留字段:', Object.keys(nameAgeSchema.properties));
85
+
86
+ const result5 = validate(nameAgeSchema, {
87
+ name: 'John',
88
+ age: 30
89
+ });
90
+
91
+ console.log('验证结果(只验证 name 和 age):');
92
+ console.log(' valid:', result5.valid);
93
+ console.log('');
94
+
95
+ // ===== 4. extend() - 扩展字段 =====
96
+
97
+ console.log('========== 4. extend() - 扩展字段 ==========\n');
98
+
99
+ const extendedSchema = SchemaUtils.extend(fullUserSchema, {
100
+ avatar: 'url',
101
+ bio: 'string:0-500'
102
+ });
103
+
104
+ console.log('原 Schema 字段:', Object.keys(fullUserSchema.properties));
105
+ console.log('extend 后字段:', Object.keys(extendedSchema.properties));
106
+ console.log('新增字段: avatar, bio');
107
+ console.log('');
108
+
109
+ // ===== 5. 链式调用 =====
110
+
111
+ console.log('========== 5. 链式调用 ==========\n');
112
+
113
+ // 示例 1: omit
114
+ console.log('示例 1: omit (创建用户 Schema)');
115
+ const createSchema = SchemaUtils.omit(fullUserSchema, ['id', 'createdAt', 'updatedAt']);
116
+
117
+ console.log(' 字段:', Object.keys(createSchema.properties));
118
+ console.log('');
119
+
120
+ // 示例 2: pick + partial
121
+ console.log('示例 2: pick + partial (更新用户 Schema)');
122
+ const updateSchema = SchemaUtils
123
+ .pick(fullUserSchema, ['name', 'age'])
124
+ .partial();
125
+
126
+ console.log(' 字段:', Object.keys(updateSchema.properties));
127
+ console.log(' 必填字段:', updateSchema.required);
128
+ console.log('');
129
+
130
+ // 示例 3: pick + extend (用户建议的常见场景)
131
+ console.log('示例 3: pick + extend (自定义用户 Schema)');
132
+ const customSchema = SchemaUtils
133
+ .pick(fullUserSchema, ['name', 'email'])
134
+ .extend({ avatar: 'url', bio: 'string:0-500' });
135
+
136
+ console.log(' 字段:', Object.keys(customSchema.properties));
137
+ console.log('');
138
+
139
+ // 示例 4: omit + extend
140
+ console.log('示例 4: omit + extend (增强用户 Schema)');
141
+ const enhancedSchema = SchemaUtils
142
+ .omit(fullUserSchema, ['password'])
143
+ .extend({ avatar: 'url', bio: 'string:0-500' });
144
+
145
+ console.log(' 字段:', Object.keys(enhancedSchema.properties));
146
+ console.log(' 新增字段: avatar, bio');
147
+ console.log('');
148
+
149
+ // ===== 6. 实际 CRUD 场景 =====
150
+
151
+ console.log('========== 6. 实际 CRUD 场景 ==========\n');
152
+
153
+ console.log('场景 1: POST /users - 创建用户');
154
+ const postSchema = SchemaUtils.omit(fullUserSchema, ['id', 'createdAt', 'updatedAt']);
155
+
156
+ const postData = {
157
+ name: 'John Doe',
158
+ email: 'john@example.com',
159
+ password: 'password123',
160
+ age: 30,
161
+ role: 'user'
162
+ };
163
+
164
+ const postResult = validate(postSchema, postData);
165
+ console.log(' 验证结果:', postResult.valid);
166
+ console.log(' 数据:', postResult.data);
167
+ console.log('');
168
+
169
+ console.log('场景 2: GET /users/:id - 查询用户');
170
+ const getSchema = SchemaUtils.omit(fullUserSchema, ['password']);
171
+
172
+ const userData = {
173
+ id: '507f1f77bcf86cd799439011',
174
+ name: 'John Doe',
175
+ email: 'john@example.com',
176
+ password: 'password123', // 已从 schema 移除,但验证时会被忽略
177
+ age: 30,
178
+ role: 'user',
179
+ createdAt: new Date(),
180
+ updatedAt: new Date()
181
+ };
182
+
183
+ const getResult = validate(getSchema, userData);
184
+ console.log(' 验证结果:', getResult.valid);
185
+ console.log(' 保留字段:', Object.keys(getResult.data));
186
+ console.log('');
187
+
188
+ console.log('场景 3: PATCH /users/:id - 部分更新用户');
189
+ const patchSchema = SchemaUtils
190
+ .pick(fullUserSchema, ['name', 'age'])
191
+ .partial();
192
+
193
+ const patchData = {
194
+ name: 'John Updated'
195
+ // 只更新 name,age 缺失也可以
196
+ };
197
+
198
+ const patchResult = validate(patchSchema, patchData);
199
+ console.log(' 验证结果:', patchResult.valid);
200
+ console.log(' 数据:', patchResult.data);
201
+ console.log('');
202
+
203
+ console.log('场景 4: PUT /users/:id - 替换用户');
204
+ const putSchema = SchemaUtils.omit(fullUserSchema, ['id', 'createdAt', 'updatedAt']);
205
+
206
+ const putData = {
207
+ name: 'John Replaced',
208
+ email: 'john.new@example.com',
209
+ password: 'newpassword123',
210
+ age: 31,
211
+ role: 'admin'
212
+ };
213
+
214
+ const putResult = validate(putSchema, putData);
215
+ console.log(' 验证结果:', putResult.valid);
216
+ console.log(' 数据:', putResult.data);
217
+ console.log('');
218
+
219
+ // ===== 7. 不可变性验证 =====
220
+
221
+ console.log('========== 7. 不可变性验证 ==========\n');
222
+
223
+ const original = dsl({
224
+ name: 'string!',
225
+ email: 'email!'
226
+ });
227
+
228
+ const modified = SchemaUtils.omit(original, ['email']);
229
+
230
+ console.log('原 Schema 字段:', Object.keys(original.properties));
231
+ console.log('修改后 Schema 字段:', Object.keys(modified.properties));
232
+ console.log('原 Schema 未被修改:', original.properties.email !== undefined);
233
+ console.log('');
234
+
235
+ console.log('========================================');
236
+ console.log(' 所有示例运行完成!');
237
+ console.log('========================================\n');
238
+
239
+ console.log('核心方法总结:');
240
+ console.log(' 1. omit() - 排除字段(50%场景)');
241
+ console.log(' 2. pick() - 保留字段(30%场景)');
242
+ console.log(' 3. partial() - 部分验证(25%场景)');
243
+ console.log(' 4. extend() - 扩展字段(15%场景)');
244
+ console.log('');
245
+ console.log('常见组合:');
246
+ console.log(' - POST: omit(系统字段)');
247
+ console.log(' - GET: omit(敏感字段)');
248
+ console.log(' - PATCH: pick(可修改字段).partial()');
249
+ console.log(' - 自定义: pick(基础字段).extend(新字段)');
250
+
@@ -0,0 +1,122 @@
1
+ /**
2
+ * SchemaIO v2.0.1 - 简洁示例
3
+ *
4
+ * 展示最简洁、最直观的用法
5
+ */
6
+
7
+ const { dsl, validate, SchemaUtils } = require('../index');
8
+
9
+ console.log('========== SchemaIO v2.0.1 简洁示例 ==========\n');
10
+
11
+ // ========== 1. 数组验证 - 超简洁 ==========
12
+ console.log('✨ 1. 数组验证');
13
+
14
+ const articleSchema = dsl({
15
+ title: 'string:5-100!',
16
+ tags: 'array!1-10' // ✨ 简洁:必填,1-10个元素
17
+ });
18
+
19
+ console.log('验证文章:', validate(articleSchema, {
20
+ title: 'Hello World',
21
+ tags: ['javascript', 'nodejs']
22
+ }).valid);
23
+ console.log('');
24
+
25
+ // ========== 2. when条件 - 超直观 ==========
26
+ console.log('✨ 2. 条件验证');
27
+
28
+ const contactSchema = dsl({
29
+ type: 'email|phone',
30
+ contact: dsl.match('type', {
31
+ email: 'email!',
32
+ phone: 'phone:cn!'
33
+ })
34
+ });
35
+
36
+ console.log('验证邮箱:', validate(contactSchema, { type: 'email', contact: 'test@example.com' }).valid);
37
+ console.log('验证手机:', validate(contactSchema, { type: 'phone', contact: '13800138000' }).valid);
38
+ console.log('');
39
+
40
+ // ========== 3. 快捷方法 - 不用找正则 ==========
41
+ console.log('✨ 3. 快捷方法');
42
+
43
+ const userSchema = dsl({
44
+ mobile: 'string!'.phoneNumber('cn'),
45
+ id: 'string!'.idCard('cn')
46
+ });
47
+
48
+ console.log('验证快捷方法:', validate(userSchema, {
49
+ mobile: '13800138000',
50
+ id: '110101199003071234'
51
+ }).valid);
52
+ console.log('');
53
+
54
+ // ========== 4. Schema复用 - 不重复代码 ==========
55
+ console.log('✨ 4. Schema复用');
56
+
57
+ // 定义一次,到处使用
58
+ const emailField = SchemaUtils.reusable(() => dsl('email!'));
59
+
60
+ const loginForm = dsl({ email: emailField() });
61
+ const registerForm = dsl({ email: emailField(), name: 'string!' });
62
+
63
+ console.log('复用Schema成功');
64
+ console.log('');
65
+
66
+ // ========== 5. Schema合并 - 灵活组合 ==========
67
+ console.log('✨ 5. Schema合并');
68
+
69
+ const baseUser = dsl({ name: 'string!', email: 'email!' });
70
+ const withAge = dsl({ age: 'number:18-120' });
71
+
72
+ const fullUser = SchemaUtils.extend(baseUser, withAge);
73
+
74
+ console.log('合并后字段:', Object.keys(fullUser.properties));
75
+ console.log('');
76
+
77
+ // ========== 6. 批量验证 - 快50倍 ==========
78
+ console.log('✨ 6. 批量验证');
79
+
80
+ const users = [
81
+ { email: 'user1@example.com' },
82
+ { email: 'invalid' },
83
+ { email: 'user3@example.com' }
84
+ ];
85
+
86
+ const { Validator } = require('../index');
87
+ const batchResult = SchemaUtils.validateBatch(
88
+ dsl({ email: 'email!' }),
89
+ users,
90
+ new Validator() // 批量验证需要 Validator 实例以复用编译结果
91
+ );
92
+
93
+ console.log('批量验证:', {
94
+ 总数: batchResult.summary.total,
95
+ 有效: batchResult.summary.valid,
96
+ 无效: batchResult.summary.invalid,
97
+ 耗时: `${batchResult.summary.duration}ms`
98
+ });
99
+ console.log('');
100
+
101
+ // ========== 总结 ==========
102
+ console.log('========== 核心理念 ==========');
103
+ console.log(`
104
+ ✨ SchemaIO v2.0.1 三大特点:
105
+
106
+ 1. 简洁
107
+ 'array!1-10' // 一眼看懂
108
+ .phoneNumber('cn') // 不用找正则
109
+
110
+ 2. 直观
111
+ dsl.match('type', { // 清晰的条件映射
112
+ email: 'email!',
113
+ phone: 'string:11!'
114
+ })
115
+
116
+ 3. 强大
117
+ SchemaUtils.extend() // 扩展字段
118
+ validateBatch() // 快50倍
119
+
120
+ 🎉 简洁 + 直观 + 强大 = 完美验证库!
121
+ `);
122
+