schema-dsl 2.3.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 (109) 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 +35 -0
  11. package/CHANGELOG.md +633 -0
  12. package/CONTRIBUTING.md +368 -0
  13. package/LICENSE +21 -0
  14. package/README.md +1122 -0
  15. package/STATUS.md +273 -0
  16. package/docs/FEATURE-INDEX.md +521 -0
  17. package/docs/INDEX.md +224 -0
  18. package/docs/api-reference.md +1098 -0
  19. package/docs/best-practices.md +672 -0
  20. package/docs/cache-manager.md +336 -0
  21. package/docs/design-philosophy.md +602 -0
  22. package/docs/dsl-syntax.md +654 -0
  23. package/docs/dynamic-locale.md +552 -0
  24. package/docs/error-handling.md +703 -0
  25. package/docs/export-guide.md +459 -0
  26. package/docs/faq.md +576 -0
  27. package/docs/frontend-i18n-guide.md +290 -0
  28. package/docs/i18n-user-guide.md +488 -0
  29. package/docs/label-vs-description.md +262 -0
  30. package/docs/markdown-exporter.md +398 -0
  31. package/docs/mongodb-exporter.md +279 -0
  32. package/docs/multi-type-support.md +319 -0
  33. package/docs/mysql-exporter.md +257 -0
  34. package/docs/plugin-system.md +542 -0
  35. package/docs/postgresql-exporter.md +290 -0
  36. package/docs/quick-start.md +761 -0
  37. package/docs/schema-helper.md +340 -0
  38. package/docs/schema-utils.md +492 -0
  39. package/docs/string-extensions.md +480 -0
  40. package/docs/troubleshooting.md +471 -0
  41. package/docs/type-converter.md +319 -0
  42. package/docs/type-reference.md +219 -0
  43. package/docs/validate.md +486 -0
  44. package/docs/validation-guide.md +484 -0
  45. package/examples/array-dsl-example.js +227 -0
  46. package/examples/custom-extension.js +85 -0
  47. package/examples/dsl-match-example.js +74 -0
  48. package/examples/dsl-style.js +118 -0
  49. package/examples/dynamic-locale-configuration.js +348 -0
  50. package/examples/dynamic-locale-example.js +287 -0
  51. package/examples/export-demo.js +130 -0
  52. package/examples/i18n-full-demo.js +310 -0
  53. package/examples/i18n-memory-safety.examples.js +268 -0
  54. package/examples/markdown-export.js +71 -0
  55. package/examples/middleware-usage.js +93 -0
  56. package/examples/password-reset/README.md +153 -0
  57. package/examples/password-reset/schema.js +26 -0
  58. package/examples/password-reset/test.js +101 -0
  59. package/examples/plugin-system.examples.js +205 -0
  60. package/examples/simple-example.js +122 -0
  61. package/examples/string-extensions.js +297 -0
  62. package/examples/user-registration/README.md +156 -0
  63. package/examples/user-registration/routes.js +92 -0
  64. package/examples/user-registration/schema.js +150 -0
  65. package/examples/user-registration/server.js +74 -0
  66. package/index.d.ts +1999 -0
  67. package/index.js +270 -0
  68. package/index.mjs +30 -0
  69. package/lib/adapters/DslAdapter.js +653 -0
  70. package/lib/adapters/index.js +20 -0
  71. package/lib/config/constants.js +286 -0
  72. package/lib/config/patterns/creditCard.js +9 -0
  73. package/lib/config/patterns/idCard.js +9 -0
  74. package/lib/config/patterns/index.js +8 -0
  75. package/lib/config/patterns/licensePlate.js +4 -0
  76. package/lib/config/patterns/passport.js +4 -0
  77. package/lib/config/patterns/phone.js +9 -0
  78. package/lib/config/patterns/postalCode.js +5 -0
  79. package/lib/core/CacheManager.js +376 -0
  80. package/lib/core/DslBuilder.js +740 -0
  81. package/lib/core/ErrorCodes.js +233 -0
  82. package/lib/core/ErrorFormatter.js +342 -0
  83. package/lib/core/JSONSchemaCore.js +347 -0
  84. package/lib/core/Locale.js +119 -0
  85. package/lib/core/MessageTemplate.js +89 -0
  86. package/lib/core/PluginManager.js +448 -0
  87. package/lib/core/StringExtensions.js +209 -0
  88. package/lib/core/Validator.js +316 -0
  89. package/lib/exporters/MarkdownExporter.js +420 -0
  90. package/lib/exporters/MongoDBExporter.js +162 -0
  91. package/lib/exporters/MySQLExporter.js +212 -0
  92. package/lib/exporters/PostgreSQLExporter.js +289 -0
  93. package/lib/exporters/index.js +24 -0
  94. package/lib/locales/en-US.js +65 -0
  95. package/lib/locales/es-ES.js +66 -0
  96. package/lib/locales/fr-FR.js +66 -0
  97. package/lib/locales/index.js +8 -0
  98. package/lib/locales/ja-JP.js +66 -0
  99. package/lib/locales/zh-CN.js +93 -0
  100. package/lib/utils/LRUCache.js +174 -0
  101. package/lib/utils/SchemaHelper.js +240 -0
  102. package/lib/utils/SchemaUtils.js +313 -0
  103. package/lib/utils/TypeConverter.js +245 -0
  104. package/lib/utils/index.js +13 -0
  105. package/lib/validators/CustomKeywords.js +203 -0
  106. package/lib/validators/index.js +11 -0
  107. package/package.json +70 -0
  108. package/plugins/custom-format.js +101 -0
  109. package/plugins/custom-validator.js +200 -0
@@ -0,0 +1,340 @@
1
+ # SchemaHelper Schema辅助工具
2
+
3
+ > **模块**: `lib/utils/SchemaHelper.js`
4
+
5
+ > **用途**: 提供 JSON Schema 操作的常用辅助方法
6
+
7
+ ---
8
+
9
+ ## 📑 目录
10
+
11
+ - [概述](#概述)
12
+ - [快速开始](#快速开始)
13
+ - [API 参考](#api-参考)
14
+ - [实用示例](#实用示例)
15
+
16
+ ---
17
+
18
+ ## 概述
19
+
20
+ `SchemaHelper` 是一个静态工具类,提供各种 Schema 操作的辅助方法,包括验证、克隆、扁平化、比较等功能。
21
+
22
+ ### 核心功能
23
+
24
+ - ✅ 验证 Schema 有效性
25
+ - ✅ 生成 Schema 唯一 ID
26
+ - ✅ 深度克隆 Schema
27
+ - ✅ 扁平化嵌套 Schema
28
+ - ✅ 提取所有字段路径
29
+ - ✅ 提取 required 字段
30
+ - ✅ 比较两个 Schema
31
+ - ✅ 计算 Schema 复杂度
32
+ - ✅ 生成 Schema 摘要
33
+
34
+ ---
35
+
36
+ ## 快速开始
37
+
38
+ ```javascript
39
+ const { SchemaHelper } = require('schema-dsl/lib/utils');
40
+ const { dsl } = require('schema-dsl');
41
+
42
+ // 创建 Schema
43
+ const userSchema = dsl({
44
+ username: 'string:3-32!',
45
+ email: 'email!',
46
+ profile: {
47
+ bio: 'string:500',
48
+ avatar: 'url'
49
+ }
50
+ });
51
+
52
+ // 获取 Schema 摘要
53
+ const summary = SchemaHelper.summarizeSchema(userSchema);
54
+ console.log(summary);
55
+ // {
56
+ // type: 'object',
57
+ // fieldCount: 4,
58
+ // requiredCount: 2,
59
+ // complexity: 1,
60
+ // hasNested: true,
61
+ // fields: ['username', 'email', 'profile.bio', 'profile.avatar']
62
+ // }
63
+ ```
64
+
65
+ ---
66
+
67
+ ## API 参考
68
+
69
+ ### `isValidSchema(schema)`
70
+
71
+ 检查是否为有效的 JSON Schema。
72
+
73
+ ```javascript
74
+ SchemaHelper.isValidSchema({ type: 'string' }); // true
75
+ SchemaHelper.isValidSchema({ properties: {} }); // true
76
+ SchemaHelper.isValidSchema({}); // false
77
+ SchemaHelper.isValidSchema(null); // false
78
+ ```
79
+
80
+ **判断标准**:至少包含 `type`、`properties`、`items` 或 `$ref` 之一。
81
+
82
+ ---
83
+
84
+ ### `generateSchemaId(schema)`
85
+
86
+ 生成 Schema 的唯一 ID(基于内容哈希)。
87
+
88
+ ```javascript
89
+ const id = SchemaHelper.generateSchemaId(userSchema);
90
+ console.log(id); // 'schema_1a2b3c4d'
91
+ ```
92
+
93
+ **用途**:缓存、去重、唯一标识。
94
+
95
+ ---
96
+
97
+ ### `cloneSchema(schema)`
98
+
99
+ 深度克隆 Schema 对象。
100
+
101
+ ```javascript
102
+ const cloned = SchemaHelper.cloneSchema(userSchema);
103
+
104
+ // 修改克隆不影响原对象
105
+ cloned.properties.newField = { type: 'string' };
106
+ console.log(userSchema.properties.newField); // undefined
107
+ ```
108
+
109
+ ---
110
+
111
+ ### `flattenSchema(schema, prefix)`
112
+
113
+ 扁平化嵌套 Schema。
114
+
115
+ ```javascript
116
+ const schema = dsl({
117
+ user: {
118
+ name: 'string!',
119
+ address: {
120
+ city: 'string!',
121
+ zip: 'string'
122
+ }
123
+ }
124
+ });
125
+
126
+ const flat = SchemaHelper.flattenSchema(schema);
127
+ // {
128
+ // 'user.name': { type: 'string' },
129
+ // 'user.address.city': { type: 'string' },
130
+ // 'user.address.zip': { type: 'string' }
131
+ // }
132
+ ```
133
+
134
+ ---
135
+
136
+ ### `getFieldPaths(schema)`
137
+
138
+ 获取 Schema 中所有字段路径。
139
+
140
+ ```javascript
141
+ const paths = SchemaHelper.getFieldPaths(userSchema);
142
+ // ['username', 'email', 'profile', 'profile.bio', 'profile.avatar']
143
+ ```
144
+
145
+ **数组字段**:使用 `[]` 表示,如 `items[].name`
146
+
147
+ ---
148
+
149
+ ### `extractRequiredFields(schema)`
150
+
151
+ 提取 Schema 中所有 required 字段(包括嵌套)。
152
+
153
+ ```javascript
154
+ const required = SchemaHelper.extractRequiredFields(userSchema);
155
+ // ['username', 'email']
156
+ ```
157
+
158
+ ---
159
+
160
+ ### `compareSchemas(schema1, schema2)`
161
+
162
+ 比较两个 Schema 是否相同。
163
+
164
+ ```javascript
165
+ const schema1 = dsl({ name: 'string!' });
166
+ const schema2 = dsl({ name: 'string!' });
167
+ const schema3 = dsl({ name: 'string' });
168
+
169
+ SchemaHelper.compareSchemas(schema1, schema2); // true
170
+ SchemaHelper.compareSchemas(schema1, schema3); // false
171
+ ```
172
+
173
+ ---
174
+
175
+ ### `simplifySchema(schema)`
176
+
177
+ 简化 Schema(移除不必要的字段)。
178
+
179
+ ```javascript
180
+ const schema = {
181
+ $schema: 'http://json-schema.org/draft-07/schema#',
182
+ type: 'object',
183
+ properties: {},
184
+ required: []
185
+ };
186
+
187
+ const simplified = SchemaHelper.simplifySchema(schema);
188
+ // { type: 'object' }
189
+ ```
190
+
191
+ **移除内容**:`$schema`、空的 `properties`、空的 `required`
192
+
193
+ ---
194
+
195
+ ### `isValidPropertyName(name)`
196
+
197
+ 验证属性名是否合法。
198
+
199
+ ```javascript
200
+ SchemaHelper.isValidPropertyName('userName'); // true
201
+ SchemaHelper.isValidPropertyName('user_name'); // true
202
+ SchemaHelper.isValidPropertyName('user-name'); // true
203
+ SchemaHelper.isValidPropertyName('123name'); // false
204
+ SchemaHelper.isValidPropertyName('user name'); // false
205
+ ```
206
+
207
+ **规则**:以字母或下划线开头,只能包含字母、数字、下划线、连字符。
208
+
209
+ ---
210
+
211
+ ### `getSchemaComplexity(schema)`
212
+
213
+ 获取 Schema 的复杂度(最大嵌套层级)。
214
+
215
+ ```javascript
216
+ // 无嵌套
217
+ const simple = dsl({ name: 'string!' });
218
+ SchemaHelper.getSchemaComplexity(simple); // 0
219
+
220
+ // 一层嵌套
221
+ const nested = dsl({
222
+ user: {
223
+ name: 'string!'
224
+ }
225
+ });
226
+ SchemaHelper.getSchemaComplexity(nested); // 1
227
+
228
+ // 多层嵌套
229
+ const deep = dsl({
230
+ level1: {
231
+ level2: {
232
+ level3: 'string!'
233
+ }
234
+ }
235
+ });
236
+ SchemaHelper.getSchemaComplexity(deep); // 2
237
+ ```
238
+
239
+ ---
240
+
241
+ ### `summarizeSchema(schema)`
242
+
243
+ 生成 Schema 的摘要信息。
244
+
245
+ ```javascript
246
+ const summary = SchemaHelper.summarizeSchema(userSchema);
247
+ // {
248
+ // type: 'object',
249
+ // fieldCount: 4,
250
+ // requiredCount: 2,
251
+ // complexity: 1,
252
+ // hasNested: true,
253
+ // fields: ['username', 'email', 'profile.bio', 'profile.avatar']
254
+ // }
255
+ ```
256
+
257
+ **用途**:调试、日志、文档生成。
258
+
259
+ ---
260
+
261
+ ## 实用示例
262
+
263
+ ### Schema 分析工具
264
+
265
+ ```javascript
266
+ const { SchemaHelper } = require('schema-dsl/lib/utils');
267
+ const { dsl } = require('schema-dsl');
268
+
269
+ function analyzeSchema(schema, name = 'Schema') {
270
+ console.log(`\n=== ${name} 分析 ===`);
271
+
272
+ // 有效性检查
273
+ if (!SchemaHelper.isValidSchema(schema)) {
274
+ console.log('❌ 无效的 Schema');
275
+ return;
276
+ }
277
+
278
+ // 生成摘要
279
+ const summary = SchemaHelper.summarizeSchema(schema);
280
+ console.log(`类型: ${summary.type}`);
281
+ console.log(`字段数: ${summary.fieldCount}`);
282
+ console.log(`必填数: ${summary.requiredCount}`);
283
+ console.log(`嵌套层级: ${summary.complexity}`);
284
+ console.log(`字段列表: ${summary.fields.join(', ')}`);
285
+
286
+ // 必填字段
287
+ const required = SchemaHelper.extractRequiredFields(schema);
288
+ console.log(`必填字段: ${required.join(', ') || '无'}`);
289
+
290
+ // 唯一 ID
291
+ console.log(`Schema ID: ${SchemaHelper.generateSchemaId(schema)}`);
292
+ }
293
+
294
+ // 使用
295
+ const userSchema = dsl({
296
+ username: 'string:3-32!',
297
+ email: 'email!',
298
+ profile: {
299
+ bio: 'string:500',
300
+ avatar: 'url'
301
+ }
302
+ });
303
+
304
+ analyzeSchema(userSchema, 'User Schema');
305
+ ```
306
+
307
+ ### Schema 版本比较
308
+
309
+ ```javascript
310
+ function compareSchemaVersions(oldSchema, newSchema) {
311
+ if (SchemaHelper.compareSchemas(oldSchema, newSchema)) {
312
+ console.log('✅ Schema 未变化');
313
+ return;
314
+ }
315
+
316
+ const oldFields = new Set(SchemaHelper.getFieldPaths(oldSchema));
317
+ const newFields = new Set(SchemaHelper.getFieldPaths(newSchema));
318
+
319
+ // 新增字段
320
+ const added = [...newFields].filter(f => !oldFields.has(f));
321
+ if (added.length) {
322
+ console.log('➕ 新增字段:', added.join(', '));
323
+ }
324
+
325
+ // 删除字段
326
+ const removed = [...oldFields].filter(f => !newFields.has(f));
327
+ if (removed.length) {
328
+ console.log('➖ 删除字段:', removed.join(', '));
329
+ }
330
+ }
331
+ ```
332
+
333
+ ---
334
+
335
+ ## 相关文档
336
+
337
+ - [TypeConverter](type-converter.md)
338
+ - [SchemaUtils](schema-utils.md)
339
+ - [API 参考](api-reference.md)
340
+