apify-schema-tools 3.1.0 → 3.2.1
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/.node-version +1 -1
- package/CHANGELOG.md +7 -1
- package/biome.json +8 -2
- package/dist/apify-schema-tools.js +12 -9
- package/dist/apify-schema-tools.js.map +1 -1
- package/dist/apify.d.ts +1 -1
- package/dist/apify.d.ts.map +1 -1
- package/dist/apify.js +19 -5
- package/dist/apify.js.map +1 -1
- package/dist/cli/check.d.ts +5 -0
- package/dist/cli/check.d.ts.map +1 -0
- package/dist/cli/check.js +86 -0
- package/dist/cli/check.js.map +1 -0
- package/dist/cli/init.d.ts +5 -0
- package/dist/cli/init.d.ts.map +1 -0
- package/dist/cli/init.js +92 -0
- package/dist/cli/init.js.map +1 -0
- package/dist/cli/sync.d.ts +5 -0
- package/dist/cli/sync.d.ts.map +1 -0
- package/dist/cli/sync.js +112 -0
- package/dist/cli/sync.js.map +1 -0
- package/dist/configuration.d.ts +14 -5
- package/dist/configuration.d.ts.map +1 -1
- package/dist/configuration.js +9 -5
- package/dist/configuration.js.map +1 -1
- package/dist/main.d.ts +4 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +19 -0
- package/dist/main.js.map +1 -0
- package/dist/middle-schema/compare-schemas.d.ts +3 -0
- package/dist/middle-schema/compare-schemas.d.ts.map +1 -0
- package/dist/middle-schema/compare-schemas.js +90 -0
- package/dist/middle-schema/compare-schemas.js.map +1 -0
- package/dist/middle-schema/generate-typescript.d.ts +7 -0
- package/dist/middle-schema/generate-typescript.d.ts.map +1 -0
- package/dist/middle-schema/generate-typescript.js +70 -0
- package/dist/middle-schema/generate-typescript.js.map +1 -0
- package/dist/middle-schema/parse-json-schema.d.ts +4 -0
- package/dist/middle-schema/parse-json-schema.d.ts.map +1 -0
- package/dist/middle-schema/parse-json-schema.js +65 -0
- package/dist/middle-schema/parse-json-schema.js.map +1 -0
- package/dist/middle-schema/parse-typescript.d.ts +4 -0
- package/dist/middle-schema/parse-typescript.d.ts.map +1 -0
- package/dist/middle-schema/parse-typescript.js +199 -0
- package/dist/middle-schema/parse-typescript.js.map +1 -0
- package/dist/middle-schema/schema-types.d.ts +24 -0
- package/dist/middle-schema/schema-types.d.ts.map +1 -0
- package/dist/middle-schema/schema-types.js +14 -0
- package/dist/middle-schema/schema-types.js.map +1 -0
- package/dist/middle-schema/schema.d.ts +24 -0
- package/dist/middle-schema/schema.d.ts.map +1 -0
- package/dist/middle-schema/schema.js +14 -0
- package/dist/middle-schema/schema.js.map +1 -0
- package/dist/schema/entities/abstract-entity.d.ts +5 -0
- package/dist/schema/entities/abstract-entity.d.ts.map +1 -0
- package/dist/schema/entities/abstract-entity.js +3 -0
- package/dist/schema/entities/abstract-entity.js.map +1 -0
- package/dist/schema/entities/primitive-union.d.ts +12 -0
- package/dist/schema/entities/primitive-union.d.ts.map +1 -0
- package/dist/schema/entities/primitive-union.js +74 -0
- package/dist/schema/entities/primitive-union.js.map +1 -0
- package/dist/schema/entities/primitive.d.ts +15 -0
- package/dist/schema/entities/primitive.d.ts.map +1 -0
- package/dist/schema/entities/primitive.js +54 -0
- package/dist/schema/entities/primitive.js.map +1 -0
- package/dist/schema/parsers/json-schema.d.ts +4 -0
- package/dist/schema/parsers/json-schema.d.ts.map +1 -0
- package/dist/schema/parsers/json-schema.js +12 -0
- package/dist/schema/parsers/json-schema.js.map +1 -0
- package/dist/schema/parsers/typescript.d.ts +3 -0
- package/dist/schema/parsers/typescript.d.ts.map +1 -0
- package/dist/schema/parsers/typescript.js +24 -0
- package/dist/schema/parsers/typescript.js.map +1 -0
- package/dist/schemas/input.d.ts +840 -0
- package/dist/schemas/input.d.ts.map +1 -0
- package/dist/schemas/input.js +349 -0
- package/dist/schemas/input.js.map +1 -0
- package/dist/utils/filesystem.d.ts +8 -0
- package/dist/utils/filesystem.d.ts.map +1 -0
- package/dist/utils/filesystem.js +16 -0
- package/dist/utils/filesystem.js.map +1 -0
- package/dist/utils/json-schemas-interactive-conflict.d.ts +16 -0
- package/dist/utils/json-schemas-interactive-conflict.d.ts.map +1 -0
- package/dist/utils/json-schemas-interactive-conflict.js +165 -0
- package/dist/utils/json-schemas-interactive-conflict.js.map +1 -0
- package/dist/utils/json-schemas.d.ts +42 -0
- package/dist/utils/json-schemas.d.ts.map +1 -0
- package/dist/utils/json-schemas.js +162 -0
- package/dist/utils/json-schemas.js.map +1 -0
- package/dist/zod/schemas/input.d.ts +840 -0
- package/dist/zod/schemas/input.d.ts.map +1 -0
- package/dist/zod/schemas/input.js +393 -0
- package/dist/zod/schemas/input.js.map +1 -0
- package/package.json +12 -12
- package/samples/all-defaults/.actor/input_schema.json +32 -3
- package/samples/all-defaults/src-schemas/input.json +2 -1
- package/samples/deep-merged-schemas/.actor/input_schema.json +36 -3
- package/samples/merged-schemas/.actor/input_schema.json +27 -3
- package/samples/package-json-config/.actor/input_schema.json +32 -3
- package/samples/package-json-config-merged/.actor/input_schema.json +36 -3
- package/src/apify.ts +21 -6
- package/src/cli/check.ts +114 -0
- package/src/cli/init.ts +125 -0
- package/src/cli/sync.ts +164 -0
- package/src/configuration.ts +17 -7
- package/src/main.ts +25 -0
- package/src/middle-schema/compare-schemas.ts +113 -0
- package/src/middle-schema/generate-typescript.ts +88 -0
- package/src/middle-schema/parse-json-schema.ts +104 -0
- package/src/middle-schema/parse-typescript.ts +239 -0
- package/src/middle-schema/schema-types.ts +40 -0
- package/test/apify.test.ts +410 -2
- package/test/cli/check.test.ts +1571 -0
- package/test/cli/init.test.ts +459 -0
- package/test/cli/sync.test.ts +341 -0
- package/test/common.ts +68 -0
- package/test/configuration.test.ts +8 -8
- package/test/middle-schema/compare-schemas.test.ts +585 -0
- package/test/middle-schema/generate-typescript.test.ts +191 -0
- package/test/middle-schema/parse-json-schema.test.ts +178 -0
- package/test/middle-schema/parse-typescript.test.ts +143 -0
- package/test/{json-schema-conflicts.test.ts → utils/json-schemas-interactive-conflict.test.ts} +2 -2
- package/test/{json-schemas.test.ts → utils/json-schemas.test.ts} +3 -3
- package/src/apify-schema-tools.ts +0 -420
- package/src/typescript.ts +0 -563
- package/test/apify-schema-tools.test.ts +0 -2216
- package/test/typescript.test.ts +0 -1079
- /package/src/{filesystem.ts → utils/filesystem.ts} +0 -0
- /package/src/{json-schema-conflicts.ts → utils/json-schemas-interactive-conflict.ts} +0 -0
- /package/src/{json-schemas.ts → utils/json-schemas.ts} +0 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
3
|
+
import {
|
|
4
|
+
readGeneratedTypeScriptFile,
|
|
5
|
+
serializeMiddleObjectToTypeScript,
|
|
6
|
+
writeTypeScriptFile,
|
|
7
|
+
} from "../../src/middle-schema/generate-typescript.js";
|
|
8
|
+
import type { AllowedType, MiddleObject } from "../../src/middle-schema/schema.js";
|
|
9
|
+
import { readFile } from "../../src/utils/filesystem.js";
|
|
10
|
+
import { cleanupTestDirectory, getTestDir, setupTestDirectory } from "../common.js";
|
|
11
|
+
|
|
12
|
+
const TEST_DIR = getTestDir("middle-schema-generate-typescript");
|
|
13
|
+
|
|
14
|
+
describe("Generate-TypeScript utilities", () => {
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
cleanupTestDirectory(TEST_DIR);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe("writeTypeScriptFile", () => {
|
|
20
|
+
it("should add a header to the TypeScript file", () => {
|
|
21
|
+
setupTestDirectory(TEST_DIR);
|
|
22
|
+
|
|
23
|
+
const content = "export const foo = 'bar';";
|
|
24
|
+
const filePath = path.join(TEST_DIR, "test.ts");
|
|
25
|
+
writeTypeScriptFile(filePath, content);
|
|
26
|
+
|
|
27
|
+
const fullContent = readFile(filePath);
|
|
28
|
+
expect(fullContent).toContain("generated by apify-schema-tools.");
|
|
29
|
+
|
|
30
|
+
const parsedContent = readGeneratedTypeScriptFile(filePath);
|
|
31
|
+
expect(parsedContent).toBe(content);
|
|
32
|
+
expect(parsedContent).not.toContain("generated by apify-schema-tools.");
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe("serializeMiddleObjectToTypeScript", () => {
|
|
37
|
+
it("should serialize MiddleObject to string", () => {
|
|
38
|
+
const schema = {
|
|
39
|
+
doc: "Test schema",
|
|
40
|
+
isRequired: true,
|
|
41
|
+
isArray: false,
|
|
42
|
+
properties: {
|
|
43
|
+
name: {
|
|
44
|
+
doc: undefined,
|
|
45
|
+
isRequired: true,
|
|
46
|
+
isArray: false,
|
|
47
|
+
type: "string" as AllowedType,
|
|
48
|
+
},
|
|
49
|
+
colors: {
|
|
50
|
+
doc: undefined,
|
|
51
|
+
isRequired: false,
|
|
52
|
+
isArray: true,
|
|
53
|
+
properties: {
|
|
54
|
+
name: {
|
|
55
|
+
doc: undefined,
|
|
56
|
+
isRequired: true,
|
|
57
|
+
isArray: false,
|
|
58
|
+
enum: ["red", "green", "blue"],
|
|
59
|
+
},
|
|
60
|
+
value: {
|
|
61
|
+
doc: "The hex color value",
|
|
62
|
+
isRequired: false,
|
|
63
|
+
isArray: false,
|
|
64
|
+
type: ["number", "string"] as AllowedType[],
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
additionalProperties: {
|
|
68
|
+
doc: undefined,
|
|
69
|
+
isRequired: true,
|
|
70
|
+
isArray: false,
|
|
71
|
+
type: "boolean" as AllowedType,
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
features: {
|
|
75
|
+
doc: undefined,
|
|
76
|
+
isRequired: true,
|
|
77
|
+
isArray: true,
|
|
78
|
+
enum: ["dotted", "striped", "solid"],
|
|
79
|
+
},
|
|
80
|
+
isNice: {
|
|
81
|
+
doc: "Whether the shape is nice",
|
|
82
|
+
isRequired: true,
|
|
83
|
+
isArray: false,
|
|
84
|
+
type: ["boolean", "null"] as AllowedType[],
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const serialized = serializeMiddleObjectToTypeScript("Shape", schema);
|
|
90
|
+
expect(serialized).toBe(`\
|
|
91
|
+
/**
|
|
92
|
+
* Test schema
|
|
93
|
+
*/
|
|
94
|
+
export interface Shape {
|
|
95
|
+
name: string;
|
|
96
|
+
colors?: {
|
|
97
|
+
name: "red" | "green" | "blue";
|
|
98
|
+
/**
|
|
99
|
+
* The hex color value
|
|
100
|
+
*/
|
|
101
|
+
value?: number | string;
|
|
102
|
+
[key: string]: boolean;
|
|
103
|
+
}[];
|
|
104
|
+
features: ("dotted" | "striped" | "solid")[];
|
|
105
|
+
/**
|
|
106
|
+
* Whether the shape is nice
|
|
107
|
+
*/
|
|
108
|
+
isNice: boolean | null;
|
|
109
|
+
}`);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("should serialize complex additionalProperties", () => {
|
|
113
|
+
const schema: MiddleObject = {
|
|
114
|
+
doc: "Test schema with additional properties",
|
|
115
|
+
isRequired: true,
|
|
116
|
+
isArray: false,
|
|
117
|
+
properties: {
|
|
118
|
+
name: {
|
|
119
|
+
doc: undefined,
|
|
120
|
+
isRequired: true,
|
|
121
|
+
isArray: false,
|
|
122
|
+
type: "string",
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
additionalProperties: {
|
|
126
|
+
doc: "Additional properties of any type",
|
|
127
|
+
isRequired: true,
|
|
128
|
+
isArray: true,
|
|
129
|
+
properties: {
|
|
130
|
+
key: {
|
|
131
|
+
doc: undefined,
|
|
132
|
+
isRequired: true,
|
|
133
|
+
isArray: false,
|
|
134
|
+
type: "string",
|
|
135
|
+
},
|
|
136
|
+
value: {
|
|
137
|
+
doc: undefined,
|
|
138
|
+
isRequired: true,
|
|
139
|
+
isArray: false,
|
|
140
|
+
type: "number",
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
expect(serializeMiddleObjectToTypeScript("Shape", schema)).toBe(`\
|
|
147
|
+
/**
|
|
148
|
+
* Test schema with additional properties
|
|
149
|
+
*/
|
|
150
|
+
export interface Shape {
|
|
151
|
+
name: string;
|
|
152
|
+
[key: string]: {
|
|
153
|
+
key: string;
|
|
154
|
+
value: number;
|
|
155
|
+
}[];
|
|
156
|
+
}`);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("should serialize a MiddleObject with a property with only additionalProperties", () => {
|
|
160
|
+
const schema: MiddleObject = {
|
|
161
|
+
doc: "Test schema with extensible properties",
|
|
162
|
+
isRequired: true,
|
|
163
|
+
isArray: false,
|
|
164
|
+
properties: {
|
|
165
|
+
extensible: {
|
|
166
|
+
doc: undefined,
|
|
167
|
+
isRequired: true,
|
|
168
|
+
isArray: false,
|
|
169
|
+
properties: {},
|
|
170
|
+
additionalProperties: {
|
|
171
|
+
doc: undefined,
|
|
172
|
+
isRequired: true,
|
|
173
|
+
isArray: false,
|
|
174
|
+
type: "unknown",
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
expect(serializeMiddleObjectToTypeScript("Extensible", schema)).toBe(`\
|
|
181
|
+
/**
|
|
182
|
+
* Test schema with extensible properties
|
|
183
|
+
*/
|
|
184
|
+
export interface Extensible {
|
|
185
|
+
extensible: {
|
|
186
|
+
[key: string]: unknown;
|
|
187
|
+
};
|
|
188
|
+
}`);
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
});
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
2
|
+
import { jsonSchemaToMiddleObject } from "../../src/middle-schema/parse-json-schema.js";
|
|
3
|
+
import type { ObjectSchema } from "../../src/utils/json-schemas.js";
|
|
4
|
+
import { cleanupTestDirectory, getTestDir } from "../common.js";
|
|
5
|
+
|
|
6
|
+
const TEST_DIR = getTestDir("middle-schema-parse-json-schema");
|
|
7
|
+
|
|
8
|
+
describe("Parse-JSON-schema utilities", () => {
|
|
9
|
+
afterEach(() => {
|
|
10
|
+
cleanupTestDirectory(TEST_DIR);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe("jsonSchemaToMiddleSchema", () => {
|
|
14
|
+
it("should convert JSON schema to MiddleObject", () => {
|
|
15
|
+
const schema: ObjectSchema = {
|
|
16
|
+
description: "Test schema",
|
|
17
|
+
type: "object",
|
|
18
|
+
properties: {
|
|
19
|
+
name: { type: "string" },
|
|
20
|
+
colors: {
|
|
21
|
+
type: "array",
|
|
22
|
+
items: {
|
|
23
|
+
type: "object",
|
|
24
|
+
properties: {
|
|
25
|
+
name: {
|
|
26
|
+
type: "string",
|
|
27
|
+
enum: ["red", "green", "blue"],
|
|
28
|
+
},
|
|
29
|
+
value: {
|
|
30
|
+
type: ["integer", "string"],
|
|
31
|
+
description: "The hex color value",
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
required: ["name"],
|
|
35
|
+
additionalProperties: {
|
|
36
|
+
type: "array",
|
|
37
|
+
items: {
|
|
38
|
+
type: "object",
|
|
39
|
+
properties: {
|
|
40
|
+
key: {
|
|
41
|
+
type: "string",
|
|
42
|
+
},
|
|
43
|
+
value: {
|
|
44
|
+
type: "number",
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
required: ["key", "value"],
|
|
48
|
+
additionalProperties: false,
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
features: {
|
|
54
|
+
type: "array",
|
|
55
|
+
items: {
|
|
56
|
+
type: "string",
|
|
57
|
+
enum: ["dotted", "striped", "solid"],
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
isNice: {
|
|
61
|
+
type: "boolean",
|
|
62
|
+
description: "Whether the shape is nice",
|
|
63
|
+
nullable: true,
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
required: ["name", "features", "isNice"],
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const tsInterface = jsonSchemaToMiddleObject(schema);
|
|
70
|
+
|
|
71
|
+
expect(tsInterface).toEqual({
|
|
72
|
+
doc: "Test schema",
|
|
73
|
+
isRequired: true,
|
|
74
|
+
isArray: false,
|
|
75
|
+
properties: {
|
|
76
|
+
name: {
|
|
77
|
+
doc: undefined,
|
|
78
|
+
isRequired: true,
|
|
79
|
+
isArray: false,
|
|
80
|
+
type: "string",
|
|
81
|
+
},
|
|
82
|
+
colors: {
|
|
83
|
+
doc: undefined,
|
|
84
|
+
isRequired: false,
|
|
85
|
+
isArray: true,
|
|
86
|
+
properties: {
|
|
87
|
+
name: {
|
|
88
|
+
doc: undefined,
|
|
89
|
+
isRequired: true,
|
|
90
|
+
isArray: false,
|
|
91
|
+
enum: ["red", "green", "blue"],
|
|
92
|
+
},
|
|
93
|
+
value: {
|
|
94
|
+
doc: "The hex color value",
|
|
95
|
+
isRequired: false,
|
|
96
|
+
isArray: false,
|
|
97
|
+
type: ["number", "string"],
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
additionalProperties: {
|
|
101
|
+
doc: undefined,
|
|
102
|
+
isRequired: true,
|
|
103
|
+
isArray: true,
|
|
104
|
+
properties: {
|
|
105
|
+
key: {
|
|
106
|
+
doc: undefined,
|
|
107
|
+
isRequired: true,
|
|
108
|
+
isArray: false,
|
|
109
|
+
type: "string",
|
|
110
|
+
},
|
|
111
|
+
value: {
|
|
112
|
+
doc: undefined,
|
|
113
|
+
isRequired: true,
|
|
114
|
+
isArray: false,
|
|
115
|
+
type: "number",
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
features: {
|
|
121
|
+
doc: undefined,
|
|
122
|
+
isRequired: true,
|
|
123
|
+
isArray: true,
|
|
124
|
+
enum: ["dotted", "striped", "solid"],
|
|
125
|
+
},
|
|
126
|
+
isNice: {
|
|
127
|
+
doc: "Whether the shape is nice",
|
|
128
|
+
isRequired: true,
|
|
129
|
+
isArray: false,
|
|
130
|
+
type: ["boolean", "null"],
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
additionalProperties: {
|
|
134
|
+
doc: undefined,
|
|
135
|
+
isRequired: true,
|
|
136
|
+
isArray: false,
|
|
137
|
+
type: "unknown",
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("should convert a JSON schema with a property with only additionalProperties", () => {
|
|
143
|
+
const schema: ObjectSchema = {
|
|
144
|
+
type: "object",
|
|
145
|
+
properties: {
|
|
146
|
+
extensible: {
|
|
147
|
+
type: "object",
|
|
148
|
+
additionalProperties: true,
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
required: ["extensible"],
|
|
152
|
+
additionalProperties: false,
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const tsInterface = jsonSchemaToMiddleObject(schema);
|
|
156
|
+
expect(tsInterface).toEqual({
|
|
157
|
+
doc: undefined,
|
|
158
|
+
isRequired: true,
|
|
159
|
+
isArray: false,
|
|
160
|
+
properties: {
|
|
161
|
+
extensible: {
|
|
162
|
+
doc: undefined,
|
|
163
|
+
isRequired: true,
|
|
164
|
+
isArray: false,
|
|
165
|
+
properties: {},
|
|
166
|
+
additionalProperties: {
|
|
167
|
+
doc: undefined,
|
|
168
|
+
isRequired: true,
|
|
169
|
+
isArray: false,
|
|
170
|
+
type: "unknown",
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
additionalProperties: undefined,
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
});
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { parseTypeScriptInterface } from "../../src/middle-schema/parse-typescript.js";
|
|
4
|
+
|
|
5
|
+
describe("Parse-TypeScript utilities", () => {
|
|
6
|
+
describe("parseTypeScriptInterface", () => {
|
|
7
|
+
it("should parse TypeScript interface from string", () => {
|
|
8
|
+
const tsInterface = `\
|
|
9
|
+
/**
|
|
10
|
+
* Test schema
|
|
11
|
+
*/
|
|
12
|
+
export interface Shape {
|
|
13
|
+
name: string;
|
|
14
|
+
colors?: {
|
|
15
|
+
name: "red" | "green" | "blue";
|
|
16
|
+
/**
|
|
17
|
+
* The hex color value
|
|
18
|
+
*/
|
|
19
|
+
value?: number | string;
|
|
20
|
+
[key: string]: {
|
|
21
|
+
key: string;
|
|
22
|
+
value: number;
|
|
23
|
+
}[];
|
|
24
|
+
}[];
|
|
25
|
+
features: ("dotted" | "striped" | "solid")[];
|
|
26
|
+
/**
|
|
27
|
+
* Whether the shape is nice
|
|
28
|
+
*/
|
|
29
|
+
isNice: boolean | null;
|
|
30
|
+
}`;
|
|
31
|
+
const parsed = parseTypeScriptInterface(tsInterface);
|
|
32
|
+
expect(parsed).toEqual({
|
|
33
|
+
doc: "Test schema",
|
|
34
|
+
isRequired: true,
|
|
35
|
+
isArray: false,
|
|
36
|
+
properties: {
|
|
37
|
+
name: {
|
|
38
|
+
doc: undefined,
|
|
39
|
+
isRequired: true,
|
|
40
|
+
isArray: false,
|
|
41
|
+
type: "string",
|
|
42
|
+
},
|
|
43
|
+
colors: {
|
|
44
|
+
doc: undefined,
|
|
45
|
+
isRequired: false,
|
|
46
|
+
isArray: true,
|
|
47
|
+
properties: {
|
|
48
|
+
name: {
|
|
49
|
+
doc: undefined,
|
|
50
|
+
isRequired: true,
|
|
51
|
+
isArray: false,
|
|
52
|
+
enum: ["red", "green", "blue"],
|
|
53
|
+
},
|
|
54
|
+
value: {
|
|
55
|
+
doc: "The hex color value",
|
|
56
|
+
isRequired: false,
|
|
57
|
+
isArray: false,
|
|
58
|
+
type: ["number", "string"],
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
additionalProperties: {
|
|
62
|
+
doc: undefined,
|
|
63
|
+
isRequired: true,
|
|
64
|
+
isArray: true,
|
|
65
|
+
properties: {
|
|
66
|
+
key: {
|
|
67
|
+
doc: undefined,
|
|
68
|
+
isRequired: true,
|
|
69
|
+
isArray: false,
|
|
70
|
+
type: "string",
|
|
71
|
+
},
|
|
72
|
+
value: {
|
|
73
|
+
doc: undefined,
|
|
74
|
+
isRequired: true,
|
|
75
|
+
isArray: false,
|
|
76
|
+
type: "number",
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
additionalProperties: undefined,
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
features: {
|
|
83
|
+
doc: undefined,
|
|
84
|
+
isRequired: true,
|
|
85
|
+
isArray: true,
|
|
86
|
+
enum: ["dotted", "striped", "solid"],
|
|
87
|
+
},
|
|
88
|
+
isNice: {
|
|
89
|
+
doc: "Whether the shape is nice",
|
|
90
|
+
isRequired: true,
|
|
91
|
+
isArray: false,
|
|
92
|
+
type: ["boolean", "null"],
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
additionalProperties: undefined,
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("parses a TS interface where a property can be only a single literal", () => {
|
|
100
|
+
const tsInterface = `\
|
|
101
|
+
export interface TestInterface {
|
|
102
|
+
singleValue: "onlyValue";
|
|
103
|
+
}`;
|
|
104
|
+
const parsed = parseTypeScriptInterface(tsInterface);
|
|
105
|
+
expect(parsed).toEqual({
|
|
106
|
+
doc: undefined,
|
|
107
|
+
isRequired: true,
|
|
108
|
+
isArray: false,
|
|
109
|
+
properties: {
|
|
110
|
+
singleValue: {
|
|
111
|
+
doc: undefined,
|
|
112
|
+
isRequired: true,
|
|
113
|
+
isArray: false,
|
|
114
|
+
enum: ["onlyValue"],
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
additionalProperties: undefined,
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("parses a TS interface where a property can be only a single number", () => {
|
|
122
|
+
const tsInterface = `\
|
|
123
|
+
export interface TestInterface {
|
|
124
|
+
singleValue: 42;
|
|
125
|
+
}`;
|
|
126
|
+
const parsed = parseTypeScriptInterface(tsInterface);
|
|
127
|
+
expect(parsed).toEqual({
|
|
128
|
+
doc: undefined,
|
|
129
|
+
isRequired: true,
|
|
130
|
+
isArray: false,
|
|
131
|
+
properties: {
|
|
132
|
+
singleValue: {
|
|
133
|
+
doc: undefined,
|
|
134
|
+
isRequired: true,
|
|
135
|
+
isArray: false,
|
|
136
|
+
enum: [42],
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
additionalProperties: undefined,
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
});
|
package/test/{json-schema-conflicts.test.ts → utils/json-schemas-interactive-conflict.test.ts}
RENAMED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** biome-ignore-all lint/performance/noAwaitInLoops: testing different configurations */
|
|
2
2
|
|
|
3
3
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
4
|
-
import type { ObjectSchema, OmissibleField } from "
|
|
4
|
+
import type { ObjectSchema, OmissibleField } from "../../src/utils/json-schemas.js";
|
|
5
5
|
|
|
6
6
|
const mockSelect = vi.fn();
|
|
7
7
|
|
|
@@ -11,7 +11,7 @@ vi.mock("@inquirer/prompts", () => ({
|
|
|
11
11
|
}));
|
|
12
12
|
|
|
13
13
|
// Import after mocking
|
|
14
|
-
const { checkConflicts } = await import("
|
|
14
|
+
const { checkConflicts } = await import("../../src/utils/json-schemas-interactive-conflict.js");
|
|
15
15
|
|
|
16
16
|
describe("checkConflicts", () => {
|
|
17
17
|
const baseSchema: ObjectSchema = {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { join } from "node:path";
|
|
2
2
|
import { afterEach, describe, expect, it } from "vitest";
|
|
3
3
|
|
|
4
|
-
import { writeFile } from "
|
|
4
|
+
import { writeFile } from "../../src/utils/filesystem.js";
|
|
5
5
|
import {
|
|
6
6
|
compareSchemas,
|
|
7
7
|
filterValidSchemaProperties,
|
|
@@ -11,8 +11,8 @@ import {
|
|
|
11
11
|
readJsonSchema,
|
|
12
12
|
readJsonSchemaField,
|
|
13
13
|
writeSchemaToField,
|
|
14
|
-
} from "
|
|
15
|
-
import { cleanupTestDirectory, getTestDir, setupTestDirectory } from "
|
|
14
|
+
} from "../../src/utils/json-schemas.js";
|
|
15
|
+
import { cleanupTestDirectory, getTestDir, setupTestDirectory } from "../common.js";
|
|
16
16
|
|
|
17
17
|
const DESCRIPTION_FIELDS: OmissibleField[] = ["title", "description"];
|
|
18
18
|
const TEST_DIR = getTestDir("json-schemas");
|