ts-openapi-codegen 2.0.0-beta.9 → 2.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.
- package/README.md +88 -18
- package/README.rus.md +88 -21
- package/dist/cli/checkAndUpdateConfig/utils/validateAndMigrateConfigData.d.ts.map +1 -1
- package/dist/cli/checkAndUpdateConfig/utils/validateAndMigrateConfigData.js +5 -0
- package/dist/cli/generateOpenApiClient/generateOpenApiClient.d.ts.map +1 -1
- package/dist/cli/generateOpenApiClient/generateOpenApiClient.js +20 -8
- package/dist/cli/index.js +5 -3
- package/dist/cli/previewChanges/previewChanges.d.ts.map +1 -1
- package/dist/cli/previewChanges/previewChanges.js +116 -32
- package/dist/cli/previewChanges/utils/updateOutputPaths.d.ts.map +1 -1
- package/dist/cli/previewChanges/utils/updateOutputPaths.js +6 -0
- package/dist/common/Consts.d.ts.map +1 -1
- package/dist/common/Consts.js +4 -1
- package/dist/common/LoggerMessages.d.ts +1 -0
- package/dist/common/LoggerMessages.d.ts.map +1 -1
- package/dist/common/LoggerMessages.js +1 -0
- package/dist/common/VersionedSchema/AllVersionedSchemas/AllMigrationPlans.d.ts +1 -1
- package/dist/common/VersionedSchema/AllVersionedSchemas/AllMigrationPlans.d.ts.map +1 -1
- package/dist/common/VersionedSchema/AllVersionedSchemas/AllMigrationPlans.js +14 -4
- package/dist/common/VersionedSchema/AllVersionedSchemas/UnifiedOptionsSchemaV1.d.ts +2 -2
- package/dist/common/VersionedSchema/AllVersionedSchemas/UnifiedOptionsSchemaV2.d.ts +2 -2
- package/dist/common/VersionedSchema/AllVersionedSchemas/UnifiedOptionsSchemaV3.d.ts +2 -2
- package/dist/common/VersionedSchema/AllVersionedSchemas/UnifiedOptionsSchemaV4.d.ts +42 -0
- package/dist/common/VersionedSchema/AllVersionedSchemas/UnifiedOptionsSchemaV4.d.ts.map +1 -0
- package/dist/common/VersionedSchema/AllVersionedSchemas/UnifiedOptionsSchemaV4.js +38 -0
- package/dist/common/VersionedSchema/AllVersionedSchemas/UnifiedVersionedSchemas.d.ts +155 -28
- package/dist/common/VersionedSchema/AllVersionedSchemas/UnifiedVersionedSchemas.d.ts.map +1 -1
- package/dist/common/VersionedSchema/AllVersionedSchemas/UnifiedVersionedSchemas.js +13 -17
- package/dist/common/VersionedSchema/CommonSchemas.d.ts +12 -4
- package/dist/common/VersionedSchema/CommonSchemas.d.ts.map +1 -1
- package/dist/common/VersionedSchema/CommonSchemas.js +15 -7
- package/dist/common/VersionedSchema/MultiOptionsVersioned/MultiOptionsSchemaV1.d.ts +2 -2
- package/dist/common/VersionedSchema/MultiOptionsVersioned/MultiOptionsSchemaV4.d.ts +2 -2
- package/dist/common/VersionedSchema/MultiOptionsVersioned/MultiOptionsSchemaV5.d.ts +2 -2
- package/dist/common/VersionedSchema/OptionsVersioned/OptionsSchemaV1.d.ts +2 -2
- package/dist/common/VersionedSchema/OptionsVersioned/OptionsSchemaV4.d.ts +2 -2
- package/dist/common/VersionedSchema/Utils/__mocks__/compatibilityCases.d.ts.map +1 -1
- package/dist/common/VersionedSchema/Utils/__mocks__/compatibilityCases.js +19 -0
- package/dist/common/VersionedSchema/Utils/__tests__/allUtils.test.d.ts +2 -0
- package/dist/common/VersionedSchema/Utils/__tests__/allUtils.test.d.ts.map +1 -0
- package/dist/common/VersionedSchema/Utils/__tests__/allUtils.test.js +185 -0
- package/dist/common/VersionedSchema/Utils/__tests__/compareShapes.test.js +20 -3
- package/dist/common/VersionedSchema/Utils/__tests__/migrateDataToLatestSchemaVersion.test.js +19 -1
- package/dist/common/VersionedSchema/Utils/determineBestMatchingSchemaVersion.d.ts +4 -0
- package/dist/common/VersionedSchema/Utils/determineBestMatchingSchemaVersion.d.ts.map +1 -1
- package/dist/common/VersionedSchema/Utils/determineBestMatchingSchemaVersion.js +68 -23
- package/dist/common/VersionedSchema/Utils/getUniqueKeysFromSchemas.d.ts +1 -1
- package/dist/common/VersionedSchema/Utils/getUniqueKeysFromSchemas.js +1 -1
- package/dist/common/VersionedSchema/Utils/migrateDataToLatestSchemaVersion.d.ts +10 -0
- package/dist/common/VersionedSchema/Utils/migrateDataToLatestSchemaVersion.d.ts.map +1 -1
- package/dist/common/VersionedSchema/Utils/migrateDataToLatestSchemaVersion.js +33 -15
- package/dist/common/utils/__tests__/convertArrayToObject.test.js +64 -169
- package/dist/common/utils/convertArrayToObject.d.ts.map +1 -1
- package/dist/common/utils/convertArrayToObject.js +51 -14
- package/dist/core/OpenApiClient.d.ts.map +1 -1
- package/dist/core/OpenApiClient.js +12 -1
- package/dist/core/WriteClient.d.ts +8 -1
- package/dist/core/WriteClient.d.ts.map +1 -1
- package/dist/core/WriteClient.js +48 -7
- package/dist/core/__tests__/WriteClient.test.js +2 -0
- package/dist/core/api/v2/parser/getType.d.ts.map +1 -1
- package/dist/core/api/v2/parser/getType.js +2 -1
- package/dist/core/api/v3/parser/getType.d.ts.map +1 -1
- package/dist/core/api/v3/parser/getType.js +2 -1
- package/dist/core/types/base/Templates.model.d.ts +1 -1
- package/dist/core/types/base/Templates.model.d.ts.map +1 -1
- package/dist/core/types/enums/EmptySchemaStrategy.enum.d.ts +6 -0
- package/dist/core/types/enums/EmptySchemaStrategy.enum.d.ts.map +1 -0
- package/dist/core/types/enums/EmptySchemaStrategy.enum.js +9 -0
- package/dist/core/utils/__tests__/getRelativeModelPath.test.d.ts +2 -0
- package/dist/core/utils/__tests__/getRelativeModelPath.test.d.ts.map +1 -0
- package/dist/core/utils/__tests__/getRelativeModelPath.test.js +27 -0
- package/dist/core/utils/__tests__/modelHelpers.test.d.ts +2 -0
- package/dist/core/utils/__tests__/modelHelpers.test.d.ts.map +1 -0
- package/dist/core/utils/__tests__/modelHelpers.test.js +151 -0
- package/dist/core/utils/__tests__/postProcessModelImports.test.d.ts +2 -0
- package/dist/core/utils/__tests__/postProcessModelImports.test.d.ts.map +1 -0
- package/dist/core/utils/__tests__/postProcessModelImports.test.js +67 -0
- package/dist/core/utils/__tests__/postProcessServiceImports.test.d.ts +2 -0
- package/dist/core/utils/__tests__/postProcessServiceImports.test.d.ts.map +1 -0
- package/dist/core/utils/__tests__/postProcessServiceImports.test.js +26 -0
- package/dist/core/utils/__tests__/resolveRefPath.test.d.ts +2 -0
- package/dist/core/utils/__tests__/resolveRefPath.test.d.ts.map +1 -0
- package/dist/core/utils/__tests__/resolveRefPath.test.js +22 -0
- package/dist/core/utils/__tests__/serviceHelpers.test.d.ts +2 -0
- package/dist/core/utils/__tests__/serviceHelpers.test.d.ts.map +1 -0
- package/dist/core/utils/__tests__/serviceHelpers.test.js +121 -0
- package/dist/core/utils/__tests__/writeClientExecutor.test.d.ts +2 -0
- package/dist/core/utils/__tests__/writeClientExecutor.test.d.ts.map +1 -0
- package/dist/core/utils/__tests__/writeClientExecutor.test.js +20 -0
- package/dist/core/utils/__tests__/writeClientSchemas.test.js +2 -0
- package/dist/core/utils/getOpenApiSpec.d.ts.map +1 -1
- package/dist/core/utils/getOpenApiSpec.js +14 -1
- package/dist/core/utils/getRelativeModelPath.d.ts +9 -0
- package/dist/core/utils/getRelativeModelPath.d.ts.map +1 -0
- package/dist/core/utils/getRelativeModelPath.js +37 -0
- package/dist/core/utils/modelHelpers.d.ts.map +1 -1
- package/dist/core/utils/modelHelpers.js +37 -3
- package/dist/core/utils/postProcessModelImports.d.ts.map +1 -1
- package/dist/core/utils/postProcessModelImports.js +14 -1
- package/dist/core/utils/postProcessServiceImports.d.ts.map +1 -1
- package/dist/core/utils/postProcessServiceImports.js +1 -2
- package/dist/core/utils/precompileTemplates.js +1 -0
- package/dist/core/utils/registerHandlebarHelpers.d.ts.map +1 -1
- package/dist/core/utils/registerHandlebarHelpers.js +25 -0
- package/dist/core/utils/registerHandlebarTemplates.d.ts.map +1 -1
- package/dist/core/utils/registerHandlebarTemplates.js +42 -41
- package/dist/core/utils/resolveRefPath.d.ts.map +1 -1
- package/dist/core/utils/resolveRefPath.js +1 -4
- package/dist/core/utils/writeClientExecutor.d.ts +4 -1
- package/dist/core/utils/writeClientExecutor.d.ts.map +1 -1
- package/dist/core/utils/writeClientExecutor.js +17 -2
- package/dist/core/utils/writeClientSchemas.d.ts +4 -1
- package/dist/core/utils/writeClientSchemas.d.ts.map +1 -1
- package/dist/core/utils/writeClientSchemas.js +11 -2
- package/dist/templatesCompiled/client/core/ApiError.js +1 -1
- package/dist/templatesCompiled/client/core/ApiResult.js +1 -1
- package/dist/templatesCompiled/client/core/axios/request.js +3 -3
- package/dist/templatesCompiled/client/core/executor/createExecutorAdapter.d.ts +2 -0
- package/dist/templatesCompiled/client/core/executor/createExecutorAdapter.d.ts.map +1 -1
- package/dist/templatesCompiled/client/core/executor/createExecutorAdapter.js +13 -8
- package/dist/templatesCompiled/client/core/executor/requestExecutor.js +1 -1
- package/dist/templatesCompiled/client/core/fetch/request.js +3 -3
- package/dist/templatesCompiled/client/core/functions/catchErrors.js +1 -1
- package/dist/templatesCompiled/client/core/interceptors/withInterceptors.js +1 -1
- package/dist/templatesCompiled/client/core/node/request.js +3 -3
- package/dist/templatesCompiled/client/core/xhr/request.js +3 -3
- package/dist/templatesCompiled/client/exportClient.d.ts +4 -0
- package/dist/templatesCompiled/client/exportClient.d.ts.map +1 -1
- package/dist/templatesCompiled/client/exportClient.js +51 -13
- package/dist/templatesCompiled/client/exportSchema.d.ts +5 -1
- package/dist/templatesCompiled/client/exportSchema.d.ts.map +1 -1
- package/dist/templatesCompiled/client/exportSchema.js +37 -7
- package/dist/templatesCompiled/client/exportService.d.ts +2 -0
- package/dist/templatesCompiled/client/exportService.d.ts.map +1 -1
- package/dist/templatesCompiled/client/exportService.js +70 -30
- package/dist/templatesCompiled/client/indexFull.d.ts +1 -0
- package/dist/templatesCompiled/client/indexFull.d.ts.map +1 -1
- package/dist/templatesCompiled/client/indexFull.js +4 -1
- package/dist/templatesCompiled/client/indexSimple.d.ts +1 -0
- package/dist/templatesCompiled/client/indexSimple.d.ts.map +1 -1
- package/dist/templatesCompiled/client/indexSimple.js +4 -1
- package/dist/templatesCompiled/client/joi/exportSchema.js +5 -5
- package/dist/templatesCompiled/client/joi/partials/joiSchema.js +7 -7
- package/dist/templatesCompiled/client/joi/partials/joiSchemaComposition.js +4 -4
- package/dist/templatesCompiled/client/joi/partials/joiSchemaInterface.d.ts +5 -3
- package/dist/templatesCompiled/client/joi/partials/joiSchemaInterface.d.ts.map +1 -1
- package/dist/templatesCompiled/client/joi/partials/joiSchemaInterface.js +39 -12
- package/dist/templatesCompiled/client/jsonschema/exportSchema.js +5 -5
- package/dist/templatesCompiled/client/jsonschema/partials/jsonschemaSchema.js +7 -7
- package/dist/templatesCompiled/client/jsonschema/partials/jsonschemaSchemaArray.js +5 -5
- package/dist/templatesCompiled/client/jsonschema/partials/jsonschemaSchemaComposition.js +2 -2
- package/dist/templatesCompiled/client/jsonschema/partials/jsonschemaSchemaDictionary.js +5 -5
- package/dist/templatesCompiled/client/jsonschema/partials/jsonschemaSchemaInterface.d.ts +3 -1
- package/dist/templatesCompiled/client/jsonschema/partials/jsonschemaSchemaInterface.d.ts.map +1 -1
- package/dist/templatesCompiled/client/jsonschema/partials/jsonschemaSchemaInterface.js +45 -15
- package/dist/templatesCompiled/client/yup/exportSchema.js +3 -3
- package/dist/templatesCompiled/client/yup/partials/yupSchema.js +7 -7
- package/dist/templatesCompiled/client/yup/partials/yupSchemaComposition.js +4 -4
- package/dist/templatesCompiled/client/yup/partials/yupSchemaInterface.d.ts +5 -3
- package/dist/templatesCompiled/client/yup/partials/yupSchemaInterface.d.ts.map +1 -1
- package/dist/templatesCompiled/client/yup/partials/yupSchemaInterface.js +39 -12
- package/dist/templatesCompiled/client/zod/exportSchema.js +3 -3
- package/dist/templatesCompiled/client/zod/partials/zodSchema.js +7 -7
- package/dist/templatesCompiled/client/zod/partials/zodSchemaArray.d.ts +5 -6
- package/dist/templatesCompiled/client/zod/partials/zodSchemaArray.d.ts.map +1 -1
- package/dist/templatesCompiled/client/zod/partials/zodSchemaArray.js +14 -28
- package/dist/templatesCompiled/client/zod/partials/zodSchemaComposition.d.ts +3 -0
- package/dist/templatesCompiled/client/zod/partials/zodSchemaComposition.d.ts.map +1 -1
- package/dist/templatesCompiled/client/zod/partials/zodSchemaComposition.js +32 -8
- package/dist/templatesCompiled/client/zod/partials/zodSchemaDictionary.d.ts +3 -4
- package/dist/templatesCompiled/client/zod/partials/zodSchemaDictionary.d.ts.map +1 -1
- package/dist/templatesCompiled/client/zod/partials/zodSchemaDictionary.js +9 -23
- package/dist/templatesCompiled/client/zod/partials/zodSchemaInterface.d.ts +4 -2
- package/dist/templatesCompiled/client/zod/partials/zodSchemaInterface.d.ts.map +1 -1
- package/dist/templatesCompiled/client/zod/partials/zodSchemaInterface.js +18 -7
- package/package.json +2 -2
- package/dist/common/defaultOptions.d.ts +0 -3
- package/dist/common/defaultOptions.d.ts.map +0 -1
- package/dist/common/defaultOptions.js +0 -28
- package/dist/core/utils/advancedDeduplicatePath.d.ts +0 -5
- package/dist/core/utils/advancedDeduplicatePath.d.ts.map +0 -1
- package/dist/core/utils/advancedDeduplicatePath.js +0 -29
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
/* eslint-disable simple-import-sort/imports */
|
|
7
|
+
const node_assert_1 = __importDefault(require("node:assert"));
|
|
8
|
+
const node_test_1 = require("node:test");
|
|
9
|
+
const zod_1 = require("zod");
|
|
10
|
+
const Enums_1 = require("../../Enums");
|
|
11
|
+
const buildVersionedSchema_1 = require("../buildVersionedSchema");
|
|
12
|
+
const createDefaultFieldsMigration_1 = require("../createDefaultFieldsMigration");
|
|
13
|
+
const createFieldTransformationMigration_1 = require("../createFieldTransformationMigration");
|
|
14
|
+
const createTrivialMigration_1 = require("../createTrivialMigration");
|
|
15
|
+
const determineBestMatchingSchemaVersion_1 = require("../determineBestMatchingSchemaVersion");
|
|
16
|
+
const generateKeyMappingForInvalidKeys_1 = require("../generateKeyMappingForInvalidKeys");
|
|
17
|
+
const getCurrentErrorMessage_1 = require("../getCurrentErrorMessage");
|
|
18
|
+
const getKeyByMapValue_1 = require("../getKeyByMapValue");
|
|
19
|
+
const getLatestVersionFromMigrationPlans_1 = require("../getLatestVersionFromMigrationPlans");
|
|
20
|
+
const getUniqueKeysFromSchemas_1 = require("../getUniqueKeysFromSchemas");
|
|
21
|
+
const getUniqueObjectKeys_1 = require("../getUniqueObjectKeys");
|
|
22
|
+
const replaceInvalidKeysWithMappedNames_1 = require("../replaceInvalidKeysWithMappedNames");
|
|
23
|
+
const validateAndSuggestKeyCorrections_1 = require("../validateAndSuggestKeyCorrections");
|
|
24
|
+
(0, node_test_1.describe)('@unit: buildVersionedSchema', () => {
|
|
25
|
+
(0, node_test_1.test)('builds schema metadata without refine', () => {
|
|
26
|
+
const base = zod_1.z.object({ input: zod_1.z.string() });
|
|
27
|
+
const result = (0, buildVersionedSchema_1.buildVersionedSchema)({
|
|
28
|
+
base,
|
|
29
|
+
version: 'v1',
|
|
30
|
+
type: Enums_1.EVersionedSchemaType.OPTIONS,
|
|
31
|
+
});
|
|
32
|
+
node_assert_1.default.strictEqual(result.baseSchema, base);
|
|
33
|
+
node_assert_1.default.strictEqual(result.version, 'v1');
|
|
34
|
+
node_assert_1.default.strictEqual(result.type, Enums_1.EVersionedSchemaType.OPTIONS);
|
|
35
|
+
node_assert_1.default.deepStrictEqual(result.schema.parse({ input: 'ok' }), { input: 'ok' });
|
|
36
|
+
});
|
|
37
|
+
(0, node_test_1.test)('applies refine when provided', () => {
|
|
38
|
+
const base = zod_1.z.object({ input: zod_1.z.string(), output: zod_1.z.string() });
|
|
39
|
+
const schema = (0, buildVersionedSchema_1.buildVersionedSchema)({
|
|
40
|
+
base,
|
|
41
|
+
version: 'v2',
|
|
42
|
+
type: Enums_1.EVersionedSchemaType.OPTIONS,
|
|
43
|
+
refine: (data, ctx) => {
|
|
44
|
+
if (data.input === data.output) {
|
|
45
|
+
ctx.addIssue({
|
|
46
|
+
code: 'custom',
|
|
47
|
+
message: 'input and output must be different',
|
|
48
|
+
path: ['output'],
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
}).schema;
|
|
53
|
+
const result = schema.safeParse({ input: 'same', output: 'same' });
|
|
54
|
+
node_assert_1.default.strictEqual(result.success, false);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
(0, node_test_1.describe)('@unit: migration plan helpers', () => {
|
|
58
|
+
(0, node_test_1.test)('createDefaultFieldsMigration merges default values', () => {
|
|
59
|
+
const plan = (0, createDefaultFieldsMigration_1.createDefaultFieldsMigration)('v1', 'v2', { enabled: true });
|
|
60
|
+
node_assert_1.default.strictEqual(plan.fromVersion, 'v1');
|
|
61
|
+
node_assert_1.default.strictEqual(plan.toVersion, 'v2');
|
|
62
|
+
node_assert_1.default.deepStrictEqual(plan.migrate({ name: 'demo' }), { name: 'demo', enabled: true });
|
|
63
|
+
node_assert_1.default.match(plan.description ?? '', /enabled/);
|
|
64
|
+
});
|
|
65
|
+
(0, node_test_1.test)('createFieldTransformationMigration keeps custom migrate and custom description', () => {
|
|
66
|
+
const plan = (0, createFieldTransformationMigration_1.createFieldTransformationMigration)('v1', 'v2', ({ oldField, ...rest }) => ({ ...rest, newField: oldField }), 'rename oldField');
|
|
67
|
+
node_assert_1.default.strictEqual(plan.fromVersion, 'v1');
|
|
68
|
+
node_assert_1.default.strictEqual(plan.toVersion, 'v2');
|
|
69
|
+
node_assert_1.default.strictEqual(plan.description, 'rename oldField');
|
|
70
|
+
node_assert_1.default.deepStrictEqual(plan.migrate({ oldField: 'x', other: 1 }), { other: 1, newField: 'x' });
|
|
71
|
+
});
|
|
72
|
+
(0, node_test_1.test)('createTrivialMigration copies object as-is', () => {
|
|
73
|
+
const plan = (0, createTrivialMigration_1.createTrivialMigration)('v3', 'v4');
|
|
74
|
+
const source = { a: 1 };
|
|
75
|
+
const migrated = plan.migrate(source);
|
|
76
|
+
node_assert_1.default.deepStrictEqual(migrated, source);
|
|
77
|
+
node_assert_1.default.notStrictEqual(migrated, source);
|
|
78
|
+
node_assert_1.default.match(plan.description ?? '', /Trivial migration/);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
(0, node_test_1.describe)('@unit: determineBestMatchingSchemaVersion', () => {
|
|
82
|
+
const mk = (schema, version) => ({
|
|
83
|
+
schema,
|
|
84
|
+
baseSchema: zod_1.z.object({}),
|
|
85
|
+
version,
|
|
86
|
+
type: Enums_1.EVersionedSchemaType.OPTIONS,
|
|
87
|
+
});
|
|
88
|
+
(0, node_test_1.test)('throws on empty schema list', () => {
|
|
89
|
+
node_assert_1.default.throws(() => (0, determineBestMatchingSchemaVersion_1.determineBestMatchingSchemaVersion)({}, []), /cannot be empty/i);
|
|
90
|
+
});
|
|
91
|
+
(0, node_test_1.test)('returns latest from multiple valid matches', () => {
|
|
92
|
+
const versionedSchemas = [
|
|
93
|
+
mk(zod_1.z.object({ a: zod_1.z.string() }), 'v1'),
|
|
94
|
+
mk(zod_1.z.object({ a: zod_1.z.string(), b: zod_1.z.string().optional() }), 'v2'),
|
|
95
|
+
];
|
|
96
|
+
const result = (0, determineBestMatchingSchemaVersion_1.determineBestMatchingSchemaVersion)({ a: 'ok' }, versionedSchemas);
|
|
97
|
+
node_assert_1.default.deepStrictEqual(result, {
|
|
98
|
+
lastVersionIndex: 1,
|
|
99
|
+
latestVersion: 'v2',
|
|
100
|
+
firstVersion: 'v1',
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
(0, node_test_1.test)('falls back to the best key/error balance when no full match', () => {
|
|
104
|
+
const versionedSchemas = [
|
|
105
|
+
mk(zod_1.z.object({ a: zod_1.z.string() }), 'v1'),
|
|
106
|
+
mk(zod_1.z.object({ b: zod_1.z.string() }), 'v2'),
|
|
107
|
+
];
|
|
108
|
+
const result = (0, determineBestMatchingSchemaVersion_1.determineBestMatchingSchemaVersion)({ a: 1 }, versionedSchemas);
|
|
109
|
+
node_assert_1.default.deepStrictEqual(result, {
|
|
110
|
+
lastVersionIndex: 0,
|
|
111
|
+
latestVersion: 'v1',
|
|
112
|
+
firstVersion: 'v1',
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
(0, node_test_1.describe)('@unit: key mapping and validation helpers', () => {
|
|
117
|
+
(0, node_test_1.test)('generateKeyMappingForInvalidKeys maps close invalid keys', () => {
|
|
118
|
+
const mapping = (0, generateKeyMappingForInvalidKeys_1.generateKeyMappingForInvalidKeys)(['nmae', 'title', 'unknown'], new Set(['name', 'title']));
|
|
119
|
+
node_assert_1.default.deepStrictEqual(Array.from(mapping.entries()), [['nmae', 'name']]);
|
|
120
|
+
});
|
|
121
|
+
(0, node_test_1.test)('validateAndSuggestKeyCorrections throws with suggestion', () => {
|
|
122
|
+
node_assert_1.default.throws(() => (0, validateAndSuggestKeyCorrections_1.validateAndSuggestKeyCorrections)(['nmae'], new Set(['name'])), /Perhaps you meant "name"/);
|
|
123
|
+
});
|
|
124
|
+
(0, node_test_1.test)('validateAndSuggestKeyCorrections does not throw for valid keys', () => {
|
|
125
|
+
node_assert_1.default.doesNotThrow(() => (0, validateAndSuggestKeyCorrections_1.validateAndSuggestKeyCorrections)(['name'], new Set(['name'])));
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
(0, node_test_1.describe)('@unit: getCurrentErrorMessage and getKeyByMapValue', () => {
|
|
129
|
+
(0, node_test_1.test)('getKeyByMapValue finds key by value', () => {
|
|
130
|
+
const key = (0, getKeyByMapValue_1.getKeyByMapValue)(new Map([['nmae', '--name']]), '--name');
|
|
131
|
+
node_assert_1.default.strictEqual(key, 'nmae');
|
|
132
|
+
});
|
|
133
|
+
(0, node_test_1.test)('getCurrentErrorMessage does nothing when no issues', () => {
|
|
134
|
+
node_assert_1.default.doesNotThrow(() => {
|
|
135
|
+
(0, getCurrentErrorMessage_1.getCurrentErrorMessage)({ issues: [], message: '' }, new Map());
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
(0, node_test_1.test)('getCurrentErrorMessage throws deduplicated transformed message', () => {
|
|
139
|
+
const error = {
|
|
140
|
+
message: '--name is invalid',
|
|
141
|
+
issues: [
|
|
142
|
+
{ path: ['name'], message: 'wrong value' },
|
|
143
|
+
{ path: ['name'], message: 'wrong value' },
|
|
144
|
+
],
|
|
145
|
+
};
|
|
146
|
+
node_assert_1.default.throws(() => (0, getCurrentErrorMessage_1.getCurrentErrorMessage)(error, new Map([['nmae', '--name']])), /nmae is invalid/);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
(0, node_test_1.describe)('@unit: collection helpers', () => {
|
|
150
|
+
(0, node_test_1.test)('getLatestVersionFromMigrationPlans returns toVersion of last plan', () => {
|
|
151
|
+
const version = (0, getLatestVersionFromMigrationPlans_1.getLatestVersionFromMigrationPlans)([
|
|
152
|
+
(0, createTrivialMigration_1.createTrivialMigration)('v1', 'v2'),
|
|
153
|
+
(0, createTrivialMigration_1.createTrivialMigration)('v2', 'v3'),
|
|
154
|
+
]);
|
|
155
|
+
node_assert_1.default.strictEqual(version, 'v3');
|
|
156
|
+
});
|
|
157
|
+
(0, node_test_1.test)('getLatestVersionFromMigrationPlans throws for empty list', () => {
|
|
158
|
+
node_assert_1.default.throws(() => (0, getLatestVersionFromMigrationPlans_1.getLatestVersionFromMigrationPlans)([]), /empty/i);
|
|
159
|
+
});
|
|
160
|
+
(0, node_test_1.test)('getUniqueKeysFromSchemas returns keys from root and nested objects', () => {
|
|
161
|
+
const keys = (0, getUniqueKeysFromSchemas_1.getUniqueKeysFromSchemas)([
|
|
162
|
+
zod_1.z.object({
|
|
163
|
+
a: zod_1.z.string(),
|
|
164
|
+
nested: zod_1.z.object({ inner: zod_1.z.number() }),
|
|
165
|
+
}),
|
|
166
|
+
zod_1.z.object({ b: zod_1.z.boolean() }),
|
|
167
|
+
]);
|
|
168
|
+
node_assert_1.default.deepStrictEqual(Array.from(keys).sort(), ['a', 'b', 'inner', 'nested']);
|
|
169
|
+
});
|
|
170
|
+
(0, node_test_1.test)('getUniqueObjectKeys returns unique keys for nested object and arrays', () => {
|
|
171
|
+
const keys = (0, getUniqueObjectKeys_1.getUniqueObjectKeys)({
|
|
172
|
+
a: 1,
|
|
173
|
+
nested: { b: 2 },
|
|
174
|
+
items: [{ c: 3 }, { a: 4 }],
|
|
175
|
+
});
|
|
176
|
+
node_assert_1.default.deepStrictEqual(keys, ['a', 'nested', 'b', 'items', 'c']);
|
|
177
|
+
});
|
|
178
|
+
(0, node_test_1.test)('replaceInvalidKeysWithMappedNames replaces keys recursively for arrays', () => {
|
|
179
|
+
const replaced = (0, replaceInvalidKeysWithMappedNames_1.replaceInvalidKeysWithMappedNames)([{ nmae: 'a' }, { nmae: 'b', extra: [{ nmae: 'c' }] }], new Map([['nmae', 'name']]));
|
|
180
|
+
node_assert_1.default.deepStrictEqual(replaced, [
|
|
181
|
+
{ name: 'a' },
|
|
182
|
+
{ name: 'b', extra: [{ name: 'c' }] },
|
|
183
|
+
]);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
@@ -9,14 +9,31 @@ const node_assert_1 = __importDefault(require("node:assert"));
|
|
|
9
9
|
const compareShapes_1 = require("../compareShapes");
|
|
10
10
|
const AllVersionedSchemas_1 = require("../../../VersionedSchema/AllVersionedSchemas/AllVersionedSchemas");
|
|
11
11
|
const compatibilityCases_1 = require("../__mocks__/compatibilityCases");
|
|
12
|
+
(0, node_test_1.default)('@unit: schema versions compatibility cases must cover all adjacent transitions', () => {
|
|
13
|
+
const cases = new Set(compatibilityCases_1.compatibilityCases.map(testCase => `${testCase.type}:${testCase.from}->${testCase.to}`));
|
|
14
|
+
const transitions = new Set();
|
|
15
|
+
for (let idx = 0; idx < AllVersionedSchemas_1.allVersionedSchemas.length - 1; idx++) {
|
|
16
|
+
const current = AllVersionedSchemas_1.allVersionedSchemas[idx];
|
|
17
|
+
const next = AllVersionedSchemas_1.allVersionedSchemas[idx + 1];
|
|
18
|
+
if (current.type !== next.type) {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
const from = current.version.split('_').at(-1);
|
|
22
|
+
const to = next.version.split('_').at(-1);
|
|
23
|
+
if (!from || !to) {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
transitions.add(`${current.type}:${from}->${to}`);
|
|
27
|
+
}
|
|
28
|
+
node_assert_1.default.deepStrictEqual([...cases].sort(), [...transitions].sort(), 'compatibilityCases must include all adjacent schema transitions');
|
|
29
|
+
});
|
|
12
30
|
(0, node_test_1.default)('@unit: schema versions compatibility', () => {
|
|
13
31
|
for (const testCase of compatibilityCases_1.compatibilityCases) {
|
|
14
32
|
const { type, from, to, expect } = testCase;
|
|
15
33
|
const prev = AllVersionedSchemas_1.allVersionedSchemas.find(vs => vs.type === type && vs.version === `${type}_${from}`)?.baseSchema;
|
|
16
34
|
const next = AllVersionedSchemas_1.allVersionedSchemas.find(vs => vs.type === type && vs.version === `${type}_${to}`)?.baseSchema;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
35
|
+
node_assert_1.default.ok(prev, `Previous schema was not found for ${type} ${from}`);
|
|
36
|
+
node_assert_1.default.ok(next, `Next schema was not found for ${type} ${to}`);
|
|
20
37
|
const issues = (0, compareShapes_1.compareShapes)(prev, next);
|
|
21
38
|
node_assert_1.default.deepStrictEqual(issues, expect, `${type} ${from} → ${to} mismatch`);
|
|
22
39
|
}
|
package/dist/common/VersionedSchema/Utils/__tests__/migrateDataToLatestSchemaVersion.test.js
CHANGED
|
@@ -69,7 +69,25 @@ const migrateDataToLatestSchemaVersion_1 = require("../migrateDataToLatestSchema
|
|
|
69
69
|
});
|
|
70
70
|
node_assert_1.default.ok(result);
|
|
71
71
|
node_assert_1.default.strictEqual(result?.schemaType, Enums_2.EVersionedSchemaType.UNIFIED_OPTIONS);
|
|
72
|
-
node_assert_1.default.strictEqual(result?.schemaVersion, '
|
|
72
|
+
node_assert_1.default.strictEqual(result?.schemaVersion, 'UNIFIED_OPTIONS_v4');
|
|
73
73
|
node_assert_1.default.strictEqual(result?.value.httpClient, 'fetch');
|
|
74
74
|
node_assert_1.default.strictEqual(result?.value.validationLibrary, ValidationLibrary_enum_1.ValidationLibrary.NONE);
|
|
75
75
|
});
|
|
76
|
+
(0, node_test_1.default)('@unit: migrateDataToLatestSchemaVersion (all schemas: chooses old options branch before unified)', () => {
|
|
77
|
+
const rawInput = {
|
|
78
|
+
input: './spec.json',
|
|
79
|
+
output: './dist',
|
|
80
|
+
httpClient: 'fetch',
|
|
81
|
+
exportCore: true,
|
|
82
|
+
};
|
|
83
|
+
const result = (0, migrateDataToLatestSchemaVersion_1.migrateDataToLatestSchemaVersion)({
|
|
84
|
+
rawInput,
|
|
85
|
+
migrationPlans: AllMigrationPlans_1.allMigrationPlans,
|
|
86
|
+
versionedSchemas: AllVersionedSchemas_1.allVersionedSchemas,
|
|
87
|
+
migrationMode: Enums_1.EMigrationMode.VALIDATE_CONFIG,
|
|
88
|
+
});
|
|
89
|
+
node_assert_1.default.ok(result);
|
|
90
|
+
node_assert_1.default.strictEqual(result?.schemaType, Enums_2.EVersionedSchemaType.UNIFIED_OPTIONS);
|
|
91
|
+
node_assert_1.default.strictEqual(result?.schemaVersion, 'UNIFIED_OPTIONS_v4');
|
|
92
|
+
node_assert_1.default.ok(result?.guessedVersion.latestVersion.startsWith('OPTIONS_'));
|
|
93
|
+
});
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { VersionedSchema, VersionMatchResult } from '../Types';
|
|
3
|
+
/**
|
|
4
|
+
* Determines the best matching schema version for input data.
|
|
5
|
+
* `invalid_value` issues are intentionally ignored to allow migration from older enum/value variants.
|
|
6
|
+
*/
|
|
3
7
|
export declare function determineBestMatchingSchemaVersion(inputData: Record<string, any>, versionedSchemas: VersionedSchema<z.ZodTypeAny>[]): VersionMatchResult;
|
|
4
8
|
//# sourceMappingURL=determineBestMatchingSchemaVersion.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"determineBestMatchingSchemaVersion.d.ts","sourceRoot":"","sources":["../../../../src/common/VersionedSchema/Utils/determineBestMatchingSchemaVersion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"determineBestMatchingSchemaVersion.d.ts","sourceRoot":"","sources":["../../../../src/common/VersionedSchema/Utils/determineBestMatchingSchemaVersion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAiE/D;;;GAGG;AACH,wBAAgB,kCAAkC,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,gBAAgB,EAAE,eAAe,CAAC,CAAC,CAAC,UAAU,CAAC,EAAE,GAAG,kBAAkB,CAgDxJ"}
|
|
@@ -4,38 +4,83 @@ exports.determineBestMatchingSchemaVersion = determineBestMatchingSchemaVersion;
|
|
|
4
4
|
const validateZodOptions_1 = require("../../Validation/validateZodOptions");
|
|
5
5
|
const getUniqueKeysFromSchemas_1 = require("./getUniqueKeysFromSchemas");
|
|
6
6
|
const getUniqueObjectKeys_1 = require("./getUniqueObjectKeys");
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Counts unknown keys using a strict schema variant.
|
|
9
|
+
* The strict check is used only for ranking candidates, not for final validation.
|
|
10
|
+
*/
|
|
11
|
+
function getStrictUnrecognizedKeyCount(schema, inputData) {
|
|
12
|
+
const strictResult = (0, validateZodOptions_1.validateZodOptionsRaw)(schema.strict(), inputData);
|
|
13
|
+
if (strictResult.success) {
|
|
14
|
+
return 0;
|
|
15
|
+
}
|
|
16
|
+
return strictResult.error.issues.reduce((count, issue) => {
|
|
17
|
+
if (issue.code !== 'unrecognized_keys' || !('keys' in issue)) {
|
|
18
|
+
return count;
|
|
19
|
+
}
|
|
20
|
+
return count + issue.keys.length;
|
|
21
|
+
}, 0);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Compares two schema candidates and returns the better one.
|
|
25
|
+
* Priority:
|
|
26
|
+
* 1) fewer structural errors;
|
|
27
|
+
* 2) fewer strict-mode unknown keys;
|
|
28
|
+
* 3) more matching keys;
|
|
29
|
+
* 4) newer version inside the same schema family;
|
|
30
|
+
* 5) lower index when schema families differ.
|
|
31
|
+
*/
|
|
32
|
+
function compareCandidates(best, current) {
|
|
33
|
+
if (current.structuralErrorCount !== best.structuralErrorCount) {
|
|
34
|
+
return current.structuralErrorCount < best.structuralErrorCount ? current : best;
|
|
35
|
+
}
|
|
36
|
+
if (current.strictUnrecognizedKeyCount !== best.strictUnrecognizedKeyCount) {
|
|
37
|
+
return current.strictUnrecognizedKeyCount < best.strictUnrecognizedKeyCount ? current : best;
|
|
38
|
+
}
|
|
39
|
+
if (current.matchingKeysCount !== best.matchingKeysCount) {
|
|
40
|
+
return current.matchingKeysCount > best.matchingKeysCount ? current : best;
|
|
41
|
+
}
|
|
42
|
+
if (current.type === best.type) {
|
|
43
|
+
return current.index > best.index ? current : best;
|
|
44
|
+
}
|
|
45
|
+
return current.index < best.index ? current : best;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Determines the best matching schema version for input data.
|
|
49
|
+
* `invalid_value` issues are intentionally ignored to allow migration from older enum/value variants.
|
|
50
|
+
*/
|
|
8
51
|
function determineBestMatchingSchemaVersion(inputData, versionedSchemas) {
|
|
9
52
|
if (!versionedSchemas.length) {
|
|
10
53
|
throw new Error('The list of schemes cannot be empty');
|
|
11
54
|
}
|
|
12
55
|
const inputKeys = (0, getUniqueObjectKeys_1.getUniqueObjectKeys)(inputData);
|
|
13
|
-
const
|
|
14
|
-
|
|
56
|
+
const schemaMatches = versionedSchemas.map(({ schema, baseSchema, version, type }, idx) => {
|
|
57
|
+
const schemaKeys = (0, getUniqueKeysFromSchemas_1.getUniqueKeysFromSchemas)([schema]);
|
|
58
|
+
const matchingKeysCount = inputKeys.filter(key => schemaKeys.has(key)).length;
|
|
59
|
+
const strictUnrecognizedKeyCount = getStrictUnrecognizedKeyCount(baseSchema, inputData);
|
|
15
60
|
const validationResult = (0, validateZodOptions_1.validateZodOptionsRaw)(schema, inputData);
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
.filter((match) => match !== null);
|
|
20
|
-
if (matchingSchemas.length > 0) {
|
|
21
|
-
const latestMatch = matchingSchemas.reduce((prev, curr) => (curr.index > prev.index ? curr : prev));
|
|
61
|
+
const structuralErrorCount = !validationResult.success
|
|
62
|
+
? validationResult.error.issues.filter(issue => issue.code !== 'invalid_value').length
|
|
63
|
+
: 0;
|
|
22
64
|
return {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
65
|
+
index: idx,
|
|
66
|
+
version,
|
|
67
|
+
type,
|
|
68
|
+
structuralErrorCount,
|
|
69
|
+
strictUnrecognizedKeyCount,
|
|
70
|
+
matchingKeysCount,
|
|
26
71
|
};
|
|
27
|
-
}
|
|
28
|
-
const schemaMatches = versionedSchemas.map(({ schema, version }, idx) => {
|
|
29
|
-
const schemaKeys = (0, getUniqueKeysFromSchemas_1.getUniqueKeysFromSchemas)([schema]);
|
|
30
|
-
const validationResult = (0, validateZodOptions_1.validateZodOptionsRaw)(schema, inputData);
|
|
31
|
-
const errorFields = !validationResult.success ? validationResult.error.issues.filter(err => err.code !== 'invalid_value') : [];
|
|
32
|
-
const matchingKeys = inputKeys.filter(key => schemaKeys.has(key));
|
|
33
|
-
return { index: idx, version, errorFields, matchingKeys };
|
|
34
72
|
});
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
73
|
+
const structurallyValidMatches = schemaMatches.filter(match => match.structuralErrorCount === 0);
|
|
74
|
+
if (structurallyValidMatches.length > 0) {
|
|
75
|
+
const bestStructurallyValidMatch = structurallyValidMatches.reduce(compareCandidates);
|
|
76
|
+
const firstVersion = structurallyValidMatches.reduce((earliest, current) => (current.index < earliest.index ? current : earliest)).version;
|
|
77
|
+
return {
|
|
78
|
+
lastVersionIndex: bestStructurallyValidMatch.index,
|
|
79
|
+
latestVersion: bestStructurallyValidMatch.version,
|
|
80
|
+
firstVersion,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
const bestMatch = schemaMatches.reduce(compareCandidates);
|
|
39
84
|
return {
|
|
40
85
|
lastVersionIndex: bestMatch.index,
|
|
41
86
|
latestVersion: bestMatch.version,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
/**
|
|
3
3
|
* Get all the unique parameter keys of all Joi schemes in the array
|
|
4
|
-
* @param schemas array of
|
|
4
|
+
* @param schemas array of zod schemes
|
|
5
5
|
* @returns All the unique parameter keys of all Joi schemes in the array
|
|
6
6
|
*/
|
|
7
7
|
export declare function getUniqueKeysFromSchemas(schemas: z.ZodTypeAny[]): Set<string>;
|
|
@@ -18,7 +18,7 @@ function collectKeys(schema, result) {
|
|
|
18
18
|
}
|
|
19
19
|
/**
|
|
20
20
|
* Get all the unique parameter keys of all Joi schemes in the array
|
|
21
|
-
* @param schemas array of
|
|
21
|
+
* @param schemas array of zod schemes
|
|
22
22
|
* @returns All the unique parameter keys of all Joi schemes in the array
|
|
23
23
|
*/
|
|
24
24
|
function getUniqueKeysFromSchemas(schemas) {
|
|
@@ -2,18 +2,28 @@ import { z } from 'zod';
|
|
|
2
2
|
import { EMigrationMode } from '../../Enums';
|
|
3
3
|
import { EVersionedSchemaType } from '../Enums';
|
|
4
4
|
import { SchemaMigrationPlan, VersionedSchema, VersionMatchResult } from '../Types';
|
|
5
|
+
/**
|
|
6
|
+
* Input arguments for migrating arbitrary config data to the latest schema version.
|
|
7
|
+
*/
|
|
5
8
|
type MigrateToLatestProps = {
|
|
6
9
|
rawInput: Record<string, any>;
|
|
7
10
|
migrationPlans: SchemaMigrationPlan<Record<string, any>, Record<string, any>>[];
|
|
8
11
|
versionedSchemas: VersionedSchema<z.ZodTypeAny>[];
|
|
9
12
|
migrationMode: EMigrationMode;
|
|
10
13
|
};
|
|
14
|
+
/**
|
|
15
|
+
* Result of successful migration to the latest schema.
|
|
16
|
+
*/
|
|
11
17
|
type MigrateToLatestResult = {
|
|
12
18
|
value: Record<string, any>;
|
|
13
19
|
guessedVersion: VersionMatchResult;
|
|
14
20
|
schemaVersion: string;
|
|
15
21
|
schemaType: EVersionedSchemaType;
|
|
16
22
|
};
|
|
23
|
+
/**
|
|
24
|
+
* Migrates raw input through the migration graph to the latest schema version.
|
|
25
|
+
* Migration path is resolved by `fromVersion -> toVersion` links from `migrationPlans`.
|
|
26
|
+
*/
|
|
17
27
|
export declare function migrateDataToLatestSchemaVersion({ rawInput, migrationPlans, versionedSchemas, migrationMode }: MigrateToLatestProps): MigrateToLatestResult | null;
|
|
18
28
|
export {};
|
|
19
29
|
//# sourceMappingURL=migrateDataToLatestSchemaVersion.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migrateDataToLatestSchemaVersion.d.ts","sourceRoot":"","sources":["../../../../src/common/VersionedSchema/Utils/migrateDataToLatestSchemaVersion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"migrateDataToLatestSchemaVersion.d.ts","sourceRoot":"","sources":["../../../../src/common/VersionedSchema/Utils/migrateDataToLatestSchemaVersion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C,OAAO,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAChD,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AASpF;;GAEG;AACH,KAAK,oBAAoB,GAAG;IACxB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC9B,cAAc,EAAE,mBAAmB,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC;IAChF,gBAAgB,EAAE,eAAe,CAAC,CAAC,CAAC,UAAU,CAAC,EAAE,CAAC;IAClD,aAAa,EAAE,cAAc,CAAC;CACjC,CAAC;AAEF;;GAEG;AACH,KAAK,qBAAqB,GAAG;IACzB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC3B,cAAc,EAAE,kBAAkB,CAAC;IACnC,aAAa,EAAE,MAAM,CAAC;IACtB,UAAU,EAAE,oBAAoB,CAAC;CACpC,CAAC;AAEF;;;GAGG;AACH,wBAAgB,gCAAgC,CAAC,EAAE,QAAQ,EAAE,cAAc,EAAE,gBAAgB,EAAE,aAAa,EAAE,EAAE,oBAAoB,GAAG,qBAAqB,GAAG,IAAI,CAoFlK"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.migrateDataToLatestSchemaVersion = migrateDataToLatestSchemaVersion;
|
|
4
|
+
const Consts_1 = require("../../Consts");
|
|
4
5
|
const Enums_1 = require("../../Enums");
|
|
5
6
|
const validateZodOptions_1 = require("../../Validation/validateZodOptions");
|
|
6
7
|
const determineBestMatchingSchemaVersion_1 = require("./determineBestMatchingSchemaVersion");
|
|
@@ -10,6 +11,10 @@ const getUniqueKeysFromSchemas_1 = require("./getUniqueKeysFromSchemas");
|
|
|
10
11
|
const getUniqueObjectKeys_1 = require("./getUniqueObjectKeys");
|
|
11
12
|
const replaceInvalidKeysWithMappedNames_1 = require("./replaceInvalidKeysWithMappedNames");
|
|
12
13
|
const validateAndSuggestKeyCorrections_1 = require("./validateAndSuggestKeyCorrections");
|
|
14
|
+
/**
|
|
15
|
+
* Migrates raw input through the migration graph to the latest schema version.
|
|
16
|
+
* Migration path is resolved by `fromVersion -> toVersion` links from `migrationPlans`.
|
|
17
|
+
*/
|
|
13
18
|
function migrateDataToLatestSchemaVersion({ rawInput, migrationPlans, versionedSchemas, migrationMode }) {
|
|
14
19
|
const schemas = versionedSchemas.map(el => el.schema);
|
|
15
20
|
const allUniqueKeysFromSchemas = (0, getUniqueKeysFromSchemas_1.getUniqueKeysFromSchemas)(schemas);
|
|
@@ -20,34 +25,47 @@ function migrateDataToLatestSchemaVersion({ rawInput, migrationPlans, versionedS
|
|
|
20
25
|
const schemaPossibleKeys = (0, getUniqueKeysFromSchemas_1.getUniqueKeysFromSchemas)([guessedValidationSchema]);
|
|
21
26
|
const replacingKeysMap = (0, generateKeyMappingForInvalidKeys_1.generateKeyMappingForInvalidKeys)(allUniqueKeysFromRawInput, schemaPossibleKeys);
|
|
22
27
|
let currentData = replacingKeysMap.size ? (0, replaceInvalidKeysWithMappedNames_1.replaceInvalidKeysWithMappedNames)(rawInput, replacingKeysMap) : rawInput;
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
const actualSchema = versionedSchemas[versionedSchemas.length - 1];
|
|
29
|
+
const schemasByVersion = new Map(versionedSchemas.map(schemaInfo => [schemaInfo.version, schemaInfo.schema]));
|
|
30
|
+
const migrationPlanByFromVersion = new Map(migrationPlans.map(plan => [plan.fromVersion, plan]));
|
|
31
|
+
let currentVersion = guessedVersion.latestVersion;
|
|
32
|
+
const visitedVersions = new Set([currentVersion]);
|
|
33
|
+
while (currentVersion !== actualSchema.version) {
|
|
34
|
+
const currentVersionSchema = schemasByVersion.get(currentVersion);
|
|
35
|
+
if (!currentVersionSchema) {
|
|
36
|
+
throw new Error(`No schema found for version ${currentVersion}.`);
|
|
37
|
+
}
|
|
38
|
+
const firstValidationResult = (0, validateZodOptions_1.validateZodOptionsRaw)(currentVersionSchema, currentData);
|
|
28
39
|
if (!firstValidationResult.success) {
|
|
29
40
|
(0, getCurrentErrorMessage_1.getCurrentErrorMessage)(firstValidationResult.error, replacingKeysMap);
|
|
30
41
|
}
|
|
31
|
-
const
|
|
32
|
-
const migrationPlan = migrationPlans.find(p => p.fromVersion === fromVersion);
|
|
42
|
+
const migrationPlan = migrationPlanByFromVersion.get(currentVersion);
|
|
33
43
|
if (!migrationPlan) {
|
|
34
|
-
const availableVersions =
|
|
35
|
-
throw new Error(`No migration plan from ${
|
|
44
|
+
const availableVersions = Array.from(migrationPlanByFromVersion.keys()).join(', ');
|
|
45
|
+
throw new Error(`No migration plan from ${currentVersion}. ` +
|
|
36
46
|
`Available migration plans: ${availableVersions}. ` +
|
|
37
47
|
`This usually means the migration chain is incomplete.`);
|
|
38
48
|
}
|
|
39
|
-
const
|
|
49
|
+
const nextVersion = migrationPlan.toVersion;
|
|
50
|
+
const nextSchema = schemasByVersion.get(nextVersion);
|
|
51
|
+
if (!nextSchema) {
|
|
52
|
+
throw new Error(`Migration plan from ${migrationPlan.fromVersion} points to unknown schema version ${nextVersion}.`);
|
|
53
|
+
}
|
|
40
54
|
const migratedRaw = migrationPlan.migrate(currentData);
|
|
41
|
-
const validationResult = (0, validateZodOptions_1.validateZodOptions)(
|
|
55
|
+
const validationResult = (0, validateZodOptions_1.validateZodOptions)(nextSchema, migratedRaw);
|
|
42
56
|
if (!validationResult.success) {
|
|
43
|
-
throw new Error(`Migration from ${
|
|
57
|
+
throw new Error(`Migration from ${currentVersion} to ${nextVersion} failed validation. ` +
|
|
44
58
|
`Error: ${validationResult.errors.join('\n')}. ` +
|
|
45
59
|
`Migration description: ${migrationPlan.description || 'No description provided'}.`);
|
|
46
60
|
}
|
|
47
61
|
currentData = { ...migratedRaw };
|
|
48
|
-
|
|
49
|
-
if (
|
|
50
|
-
|
|
62
|
+
currentVersion = nextVersion;
|
|
63
|
+
if (visitedVersions.has(currentVersion)) {
|
|
64
|
+
throw new Error(`Migration loop detected at version ${currentVersion}.`);
|
|
65
|
+
}
|
|
66
|
+
visitedVersions.add(currentVersion);
|
|
67
|
+
if (currentVersion === actualSchema.version && migrationMode === Enums_1.EMigrationMode.GENERATE_OPENAPI) {
|
|
68
|
+
Consts_1.APP_LOGGER.warn('To perform OpenAPI generation, it was necessary to migrate the schema of your data to the current one. To update the configuration in the file, use the command `npm openapi-codegen-cli update-config`');
|
|
51
69
|
}
|
|
52
70
|
}
|
|
53
71
|
const validationResult = (0, validateZodOptions_1.validateZodOptionsRaw)(actualSchema.schema, currentData);
|