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.
- package/.eslintignore +10 -0
- package/.eslintrc.json +27 -0
- package/.github/CODE_OF_CONDUCT.md +45 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +57 -0
- package/.github/ISSUE_TEMPLATE/config.yml +11 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +45 -0
- package/.github/ISSUE_TEMPLATE/question.md +31 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +70 -0
- package/.github/SECURITY.md +184 -0
- package/.github/workflows/ci.yml +35 -0
- package/CHANGELOG.md +633 -0
- package/CONTRIBUTING.md +368 -0
- package/LICENSE +21 -0
- package/README.md +1122 -0
- package/STATUS.md +273 -0
- package/docs/FEATURE-INDEX.md +521 -0
- package/docs/INDEX.md +224 -0
- package/docs/api-reference.md +1098 -0
- package/docs/best-practices.md +672 -0
- package/docs/cache-manager.md +336 -0
- package/docs/design-philosophy.md +602 -0
- package/docs/dsl-syntax.md +654 -0
- package/docs/dynamic-locale.md +552 -0
- package/docs/error-handling.md +703 -0
- package/docs/export-guide.md +459 -0
- package/docs/faq.md +576 -0
- package/docs/frontend-i18n-guide.md +290 -0
- package/docs/i18n-user-guide.md +488 -0
- package/docs/label-vs-description.md +262 -0
- package/docs/markdown-exporter.md +398 -0
- package/docs/mongodb-exporter.md +279 -0
- package/docs/multi-type-support.md +319 -0
- package/docs/mysql-exporter.md +257 -0
- package/docs/plugin-system.md +542 -0
- package/docs/postgresql-exporter.md +290 -0
- package/docs/quick-start.md +761 -0
- package/docs/schema-helper.md +340 -0
- package/docs/schema-utils.md +492 -0
- package/docs/string-extensions.md +480 -0
- package/docs/troubleshooting.md +471 -0
- package/docs/type-converter.md +319 -0
- package/docs/type-reference.md +219 -0
- package/docs/validate.md +486 -0
- package/docs/validation-guide.md +484 -0
- package/examples/array-dsl-example.js +227 -0
- package/examples/custom-extension.js +85 -0
- package/examples/dsl-match-example.js +74 -0
- package/examples/dsl-style.js +118 -0
- package/examples/dynamic-locale-configuration.js +348 -0
- package/examples/dynamic-locale-example.js +287 -0
- package/examples/export-demo.js +130 -0
- package/examples/i18n-full-demo.js +310 -0
- package/examples/i18n-memory-safety.examples.js +268 -0
- package/examples/markdown-export.js +71 -0
- package/examples/middleware-usage.js +93 -0
- package/examples/password-reset/README.md +153 -0
- package/examples/password-reset/schema.js +26 -0
- package/examples/password-reset/test.js +101 -0
- package/examples/plugin-system.examples.js +205 -0
- package/examples/simple-example.js +122 -0
- package/examples/string-extensions.js +297 -0
- package/examples/user-registration/README.md +156 -0
- package/examples/user-registration/routes.js +92 -0
- package/examples/user-registration/schema.js +150 -0
- package/examples/user-registration/server.js +74 -0
- package/index.d.ts +1999 -0
- package/index.js +270 -0
- package/index.mjs +30 -0
- package/lib/adapters/DslAdapter.js +653 -0
- package/lib/adapters/index.js +20 -0
- package/lib/config/constants.js +286 -0
- package/lib/config/patterns/creditCard.js +9 -0
- package/lib/config/patterns/idCard.js +9 -0
- package/lib/config/patterns/index.js +8 -0
- package/lib/config/patterns/licensePlate.js +4 -0
- package/lib/config/patterns/passport.js +4 -0
- package/lib/config/patterns/phone.js +9 -0
- package/lib/config/patterns/postalCode.js +5 -0
- package/lib/core/CacheManager.js +376 -0
- package/lib/core/DslBuilder.js +740 -0
- package/lib/core/ErrorCodes.js +233 -0
- package/lib/core/ErrorFormatter.js +342 -0
- package/lib/core/JSONSchemaCore.js +347 -0
- package/lib/core/Locale.js +119 -0
- package/lib/core/MessageTemplate.js +89 -0
- package/lib/core/PluginManager.js +448 -0
- package/lib/core/StringExtensions.js +209 -0
- package/lib/core/Validator.js +316 -0
- package/lib/exporters/MarkdownExporter.js +420 -0
- package/lib/exporters/MongoDBExporter.js +162 -0
- package/lib/exporters/MySQLExporter.js +212 -0
- package/lib/exporters/PostgreSQLExporter.js +289 -0
- package/lib/exporters/index.js +24 -0
- package/lib/locales/en-US.js +65 -0
- package/lib/locales/es-ES.js +66 -0
- package/lib/locales/fr-FR.js +66 -0
- package/lib/locales/index.js +8 -0
- package/lib/locales/ja-JP.js +66 -0
- package/lib/locales/zh-CN.js +93 -0
- package/lib/utils/LRUCache.js +174 -0
- package/lib/utils/SchemaHelper.js +240 -0
- package/lib/utils/SchemaUtils.js +313 -0
- package/lib/utils/TypeConverter.js +245 -0
- package/lib/utils/index.js +13 -0
- package/lib/validators/CustomKeywords.js +203 -0
- package/lib/validators/index.js +11 -0
- package/package.json +70 -0
- package/plugins/custom-format.js +101 -0
- 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
|
+
|