wormajs 0.2.1 → 0.2.2
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/dist/core/loader/astLoader/generates/tuple.js +8 -4
- package/dist/core/loader/astLoader/parsers/array.js +1 -1
- package/dist/core/loader/astLoader/parsers/forward/tuple.js +7 -2
- package/dist/core/loader/astLoader/parsers/simple.js +4 -0
- package/dist/core/loader/astLoader/parsers/tuple.js +14 -1
- package/dist/core/loader/astLoader/parsers/utils.js +7 -1
- package/dist/core/workerPool/worker.js +2 -1
- package/package.json +1 -1
|
@@ -16,8 +16,12 @@ function tupleTypeGenerator(ast, ctx) {
|
|
|
16
16
|
const spreadParam = ast.spreadParam ?? {
|
|
17
17
|
type: type_1.ASTType.ANY,
|
|
18
18
|
};
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
// 封闭元组(禁止额外项)时不展开剩余项
|
|
20
|
+
const isClosed = spreadParam.type === type_1.ASTType.NEVER;
|
|
21
|
+
if (!isClosed) {
|
|
22
|
+
while (params.length < minItems) {
|
|
23
|
+
params.push(spreadParam);
|
|
24
|
+
}
|
|
21
25
|
}
|
|
22
26
|
const lines = [`[`];
|
|
23
27
|
params.forEach((param, idx, arr) => {
|
|
@@ -26,12 +30,12 @@ function tupleTypeGenerator(ast, ctx) {
|
|
|
26
30
|
const endText = idx === arr.length - 1 ? '' : ',';
|
|
27
31
|
`${value}${endText}`.split('\n').forEach(line => lines.push(` ${line}`));
|
|
28
32
|
});
|
|
29
|
-
if (maxItems < minItems) {
|
|
33
|
+
if (maxItems < minItems && !isClosed) {
|
|
30
34
|
ctx.pathKey = spreadParam.keyName;
|
|
31
35
|
lines.push(`,\n...Array<${(0, utils_1.getValue)(ctx.next(spreadParam, ctx.options), ctx.options)}>`);
|
|
32
36
|
}
|
|
33
37
|
lines.push(`]`);
|
|
34
|
-
if (maxItems > minItems) {
|
|
38
|
+
if (maxItems > minItems && !isClosed) {
|
|
35
39
|
const nextParams = [...params];
|
|
36
40
|
for (let i = 0; i < maxItems - minItems; i += 1) {
|
|
37
41
|
nextParams.push(spreadParam);
|
|
@@ -6,7 +6,7 @@ const type_1 = require("../../../../type");
|
|
|
6
6
|
const utils_1 = require("./utils");
|
|
7
7
|
function arrayTypeParser(schema, ctx) {
|
|
8
8
|
ctx.pathKey = '[]';
|
|
9
|
-
const itemsAst = ctx.next(schema.items, ctx.options);
|
|
9
|
+
const itemsAst = ctx.next(schema.items || { type: 'any' }, ctx.options);
|
|
10
10
|
const result = {
|
|
11
11
|
...(0, utils_1.initAST)(schema, ctx),
|
|
12
12
|
type: type_1.ASTType.ARRAY,
|
|
@@ -4,8 +4,13 @@ exports.default = {
|
|
|
4
4
|
is(schema) {
|
|
5
5
|
// 判断是否为元组类型
|
|
6
6
|
// 1. type 为 'array'
|
|
7
|
-
// 2. 且 items
|
|
8
|
-
|
|
7
|
+
// 2. 且 items 是数组(老版 JSON Schema 元组语法)或存在 prefixItems(OpenAPI 3.1 / JSON Schema 2020-12 元组语法)
|
|
8
|
+
if (!schema)
|
|
9
|
+
return false;
|
|
10
|
+
const tuple = schema;
|
|
11
|
+
const isItemsArray = Array.isArray(tuple.items) && tuple.items.length > 0;
|
|
12
|
+
const isPrefixItems = Array.isArray(tuple.prefixItems) && tuple.prefixItems.length > 0;
|
|
13
|
+
return schema.type === 'array' && (isItemsArray || isPrefixItems);
|
|
9
14
|
},
|
|
10
15
|
to: 'tuple',
|
|
11
16
|
};
|
|
@@ -19,6 +19,10 @@ function stringTypeParser(schema, init) {
|
|
|
19
19
|
}
|
|
20
20
|
function simpleTypeParser(schema, ctx) {
|
|
21
21
|
const result = (0, utils_1.initAST)(schema, ctx);
|
|
22
|
+
if (!schema) {
|
|
23
|
+
result.type = type_1.ASTType.NULL;
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
22
26
|
switch (schema.type) {
|
|
23
27
|
case 'boolean':
|
|
24
28
|
result.type = type_1.ASTType.BOOLEAN;
|
|
@@ -5,13 +5,26 @@ const helper_1 = require("../../../../helper");
|
|
|
5
5
|
const type_1 = require("../../../../type");
|
|
6
6
|
const utils_1 = require("./utils");
|
|
7
7
|
function tupleTypeParser(schema, ctx) {
|
|
8
|
-
|
|
8
|
+
// OpenAPI 3.1 / JSON Schema 2020-12 用 prefixItems 表示固定位置元素,兼容老版 items 数组写法
|
|
9
|
+
const elementSchemas = (Array.isArray(schema.prefixItems)
|
|
10
|
+
? schema.prefixItems
|
|
11
|
+
: (Array.isArray(schema.items) ? schema.items : []));
|
|
12
|
+
const params = elementSchemas.map(item => ctx.next(item, ctx.options));
|
|
13
|
+
// 2020-12:单数 items 表示"剩余项"类型;items:false / additionalItems:false 表示禁止额外项
|
|
14
|
+
let spreadParam;
|
|
15
|
+
if (schema.items && !Array.isArray(schema.items)) {
|
|
16
|
+
spreadParam = ctx.next(schema.items, ctx.options);
|
|
17
|
+
}
|
|
18
|
+
else if (schema.additionalItems === false || schema.items === false) {
|
|
19
|
+
spreadParam = { type: type_1.ASTType.NEVER };
|
|
20
|
+
}
|
|
9
21
|
const result = {
|
|
10
22
|
...(0, utils_1.initAST)(schema, ctx),
|
|
11
23
|
type: type_1.ASTType.TUPLE,
|
|
12
24
|
minItems: schema.minItems,
|
|
13
25
|
maxItems: schema.maxItems,
|
|
14
26
|
params,
|
|
27
|
+
spreadParam,
|
|
15
28
|
};
|
|
16
29
|
const deepCommenter = helper_1.CommentHelper.load({
|
|
17
30
|
type: ctx.options.commentType,
|
|
@@ -12,6 +12,9 @@ const forward_1 = require("./forward");
|
|
|
12
12
|
exports.SchemaPrimitive = ['boolean', 'object', 'number', 'string', 'integer', 'array', 'null'];
|
|
13
13
|
function getCommentBySchema(schema, options) {
|
|
14
14
|
const commenter = helper_1.CommentHelper.load(options);
|
|
15
|
+
if (!schema) {
|
|
16
|
+
return commenter.end();
|
|
17
|
+
}
|
|
15
18
|
if (!(0, utils_1.isReferenceObject)(schema) && schema.title) {
|
|
16
19
|
commenter.add('[title]', schema.title);
|
|
17
20
|
}
|
|
@@ -30,7 +33,7 @@ function initAST(schema, ctx) {
|
|
|
30
33
|
type: ctx.options.commentType,
|
|
31
34
|
}),
|
|
32
35
|
keyName: ctx.keyName,
|
|
33
|
-
deprecated: (0, utils_1.isReferenceObject)(schema) ?
|
|
36
|
+
deprecated: schema && !(0, utils_1.isReferenceObject)(schema) ? schema.deprecated : false,
|
|
34
37
|
};
|
|
35
38
|
ctx.keyName = '';
|
|
36
39
|
return result;
|
|
@@ -44,6 +47,9 @@ function parse(schema, options) {
|
|
|
44
47
|
return null;
|
|
45
48
|
}
|
|
46
49
|
function getParserSchemaType(schema) {
|
|
50
|
+
if (!schema) {
|
|
51
|
+
return 'null';
|
|
52
|
+
}
|
|
47
53
|
if ((0, utils_1.isReferenceObject)(schema)) {
|
|
48
54
|
return 'reference';
|
|
49
55
|
}
|
|
@@ -55,6 +55,7 @@ node_worker_threads_1.parentPort?.on('message', async (batch) => {
|
|
|
55
55
|
node_worker_threads_1.parentPort?.postMessage({ type: 'result', data: allResults });
|
|
56
56
|
}
|
|
57
57
|
catch (err) {
|
|
58
|
-
|
|
58
|
+
const error = err;
|
|
59
|
+
node_worker_threads_1.parentPort?.postMessage({ type: 'error', message: error.message });
|
|
59
60
|
}
|
|
60
61
|
});
|