ts-openapi-codegen 1.0.0-beta.3 → 1.0.0-beta.4
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/cli/generate/runGenerateOpenApi.js +3 -3
- package/dist/common/VersionedSchema/Types.d.ts +1 -1
- package/dist/common/VersionedSchema/Types.d.ts.map +1 -1
- package/dist/common/VersionedSchema/Utils/determineBestMatchingSchemaVersion.d.ts +9 -0
- package/dist/common/VersionedSchema/Utils/determineBestMatchingSchemaVersion.d.ts.map +1 -0
- package/dist/common/VersionedSchema/Utils/determineBestMatchingSchemaVersion.js +44 -0
- package/dist/common/VersionedSchema/Utils/generateKeyMappingForInvalidKeys.d.ts +2 -0
- package/dist/common/VersionedSchema/Utils/generateKeyMappingForInvalidKeys.d.ts.map +1 -0
- package/dist/common/VersionedSchema/Utils/generateKeyMappingForInvalidKeys.js +27 -0
- package/dist/common/VersionedSchema/Utils/getErrorFieldsFromValidation.d.ts +6 -0
- package/dist/common/VersionedSchema/Utils/getErrorFieldsFromValidation.d.ts.map +1 -0
- package/dist/common/VersionedSchema/Utils/getErrorFieldsFromValidation.js +7 -0
- package/dist/common/VersionedSchema/Utils/{getUniqueAllShemasKeys.d.ts → getUniqueKeysFromSchemas.d.ts} +3 -3
- package/dist/common/VersionedSchema/Utils/getUniqueKeysFromSchemas.d.ts.map +1 -0
- package/dist/common/VersionedSchema/Utils/{getUniqueAllShemasKeys.js → getUniqueKeysFromSchemas.js} +4 -4
- package/dist/common/VersionedSchema/Utils/getUniqueObjectKeys.js +2 -2
- package/dist/common/VersionedSchema/Utils/joiHandlers.d.ts +3 -3
- package/dist/common/VersionedSchema/Utils/joiHandlers.d.ts.map +1 -1
- package/dist/common/VersionedSchema/Utils/joiHandlers.js +24 -17
- package/dist/common/VersionedSchema/Utils/{migrateToLatestVersion.d.ts → migrateDataToLatestSchemaVersion.d.ts} +5 -5
- package/dist/common/VersionedSchema/Utils/migrateDataToLatestSchemaVersion.d.ts.map +1 -0
- package/dist/common/VersionedSchema/Utils/migrateDataToLatestSchemaVersion.js +43 -0
- package/dist/common/VersionedSchema/Utils/replaceInvalidKeysWithMappedNames.d.ts +2 -0
- package/dist/common/VersionedSchema/Utils/replaceInvalidKeysWithMappedNames.d.ts.map +1 -0
- package/dist/common/VersionedSchema/Utils/replaceInvalidKeysWithMappedNames.js +18 -0
- package/dist/common/VersionedSchema/Utils/traverseObjectStructure.d.ts +3 -0
- package/dist/common/VersionedSchema/Utils/traverseObjectStructure.d.ts.map +1 -0
- package/dist/common/VersionedSchema/Utils/{traverseGeneric.js → traverseObjectStructure.js} +6 -4
- package/dist/common/VersionedSchema/Utils/validateAndSuggestKeyCorrections.d.ts +8 -0
- package/dist/common/VersionedSchema/Utils/validateAndSuggestKeyCorrections.d.ts.map +1 -0
- package/dist/common/VersionedSchema/Utils/validateAndSuggestKeyCorrections.js +44 -0
- package/dist/common/__tests__/__mock__/mockJoiSchema.d.ts +19 -0
- package/dist/common/__tests__/__mock__/mockJoiSchema.d.ts.map +1 -0
- package/dist/common/__tests__/__mock__/mockJoiSchema.js +14 -0
- package/dist/common/__tests__/determineBestMatchingSchemaVersion.test.d.ts +2 -0
- package/dist/common/__tests__/determineBestMatchingSchemaVersion.test.d.ts.map +1 -0
- package/dist/common/__tests__/determineBestMatchingSchemaVersion.test.js +38 -0
- package/dist/common/__tests__/generateKeyMappingForInvalidKeys.test.d.ts +2 -0
- package/dist/common/__tests__/generateKeyMappingForInvalidKeys.test.d.ts.map +1 -0
- package/dist/common/__tests__/generateKeyMappingForInvalidKeys.test.js +22 -0
- package/dist/common/__tests__/getUniqueKeysFromSchemas.test.d.ts +2 -0
- package/dist/common/__tests__/getUniqueKeysFromSchemas.test.d.ts.map +1 -0
- package/dist/common/__tests__/getUniqueKeysFromSchemas.test.js +20 -0
- package/dist/common/__tests__/getUniqueObjectKeys.test.d.ts +2 -0
- package/dist/common/__tests__/getUniqueObjectKeys.test.d.ts.map +1 -0
- package/dist/common/__tests__/getUniqueObjectKeys.test.js +24 -0
- package/dist/common/__tests__/migrationForMultiOptions.test.js +85 -70
- package/dist/common/__tests__/migrationForOptions.test.js +28 -48
- package/dist/common/__tests__/replaceInvalidKeysWithMappedNames.test.d.ts +2 -0
- package/dist/common/__tests__/replaceInvalidKeysWithMappedNames.test.d.ts.map +1 -0
- package/dist/common/__tests__/replaceInvalidKeysWithMappedNames.test.js +25 -0
- package/dist/common/__tests__/validateAndSuggestKeyCorrections.test.d.ts +2 -0
- package/dist/common/__tests__/validateAndSuggestKeyCorrections.test.d.ts.map +1 -0
- package/dist/common/__tests__/validateAndSuggestKeyCorrections.test.js +25 -0
- package/package.json +1 -1
- package/dist/common/VersionedSchema/Utils/getUniqueAllShemasKeys.d.ts.map +0 -1
- package/dist/common/VersionedSchema/Utils/guessVersion.d.ts +0 -3
- package/dist/common/VersionedSchema/Utils/guessVersion.d.ts.map +0 -1
- package/dist/common/VersionedSchema/Utils/guessVersion.js +0 -16
- package/dist/common/VersionedSchema/Utils/migrateToLatestVersion.d.ts.map +0 -1
- package/dist/common/VersionedSchema/Utils/migrateToLatestVersion.js +0 -34
- package/dist/common/VersionedSchema/Utils/traverseGeneric.d.ts +0 -3
- package/dist/common/VersionedSchema/Utils/traverseGeneric.d.ts.map +0 -1
- package/dist/common/VersionedSchema/Utils/validateUnknownKeys.d.ts +0 -2
- package/dist/common/VersionedSchema/Utils/validateUnknownKeys.d.ts.map +0 -1
- package/dist/common/VersionedSchema/Utils/validateUnknownKeys.js +0 -32
- package/dist/common/__tests__/guessVersion.test.d.ts +0 -2
- package/dist/common/__tests__/guessVersion.test.d.ts.map +0 -1
- package/dist/common/__tests__/guessVersion.test.js +0 -26
|
@@ -42,7 +42,7 @@ const MultiOptionsMigrationPlan_1 = require("../../common/VersionedSchema/MultiO
|
|
|
42
42
|
const MultiOptionsVersionedSchemas_1 = require("../../common/VersionedSchema/MultiOptionsVersionedSchemas");
|
|
43
43
|
const OptionsMigrationPlans_1 = require("../../common/VersionedSchema/OptionsMigrationPlans");
|
|
44
44
|
const OptionsVersionedSchemas_1 = require("../../common/VersionedSchema/OptionsVersionedSchemas");
|
|
45
|
-
const
|
|
45
|
+
const migrateDataToLatestSchemaVersion_1 = require("../../common/VersionedSchema/Utils/migrateDataToLatestSchemaVersion");
|
|
46
46
|
const OpenAPI = __importStar(require("../../core"));
|
|
47
47
|
const isInstanceOfMultiOptions_1 = require("../../core/utils/isInstanceOfMultiOptions");
|
|
48
48
|
/**
|
|
@@ -71,12 +71,12 @@ async function runGenerateOpenApi(options) {
|
|
|
71
71
|
const preparedOptions = (0, Utils_1.convertArrayToObject)(configData);
|
|
72
72
|
const isMultiOptions = (0, isInstanceOfMultiOptions_1.isInstanceOfMultioptions)(preparedOptions);
|
|
73
73
|
const migratedOptions = isMultiOptions
|
|
74
|
-
? (0,
|
|
74
|
+
? (0, migrateDataToLatestSchemaVersion_1.migrateDataToLatestSchemaVersion)({
|
|
75
75
|
rawInput: preparedOptions,
|
|
76
76
|
migrationPlans: MultiOptionsMigrationPlan_1.multiOptionsMigrationPlan,
|
|
77
77
|
versionedSchemas: MultiOptionsVersionedSchemas_1.multiOptionsVersionedSchema,
|
|
78
78
|
})
|
|
79
|
-
: (0,
|
|
79
|
+
: (0, migrateDataToLatestSchemaVersion_1.migrateDataToLatestSchemaVersion)({
|
|
80
80
|
rawInput: preparedOptions,
|
|
81
81
|
migrationPlans: OptionsMigrationPlans_1.optionsMigrationPlans,
|
|
82
82
|
versionedSchemas: OptionsVersionedSchemas_1.optionsVersionedSchemas,
|
|
@@ -14,5 +14,5 @@ export type GuessVersion = {
|
|
|
14
14
|
version: string;
|
|
15
15
|
index: number;
|
|
16
16
|
};
|
|
17
|
-
export type TraverseHandler = (value: any, recurse: (v: any) => void, result:
|
|
17
|
+
export type TraverseHandler<T> = (value: any, recurse: (v: any) => void, result: T) => boolean;
|
|
18
18
|
//# sourceMappingURL=Types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Types.d.ts","sourceRoot":"","sources":["../../../src/common/VersionedSchema/Types.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,OAAO,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAE/C,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI;IAC7B,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IACtB,IAAI,EAAE,oBAAoB,CAAC;IAC3B,OAAO,EAAE,MAAM,CAAC;CACnB,CAAA;AAED,MAAM,MAAM,mBAAmB,CAAC,IAAI,EAAE,EAAE,IAAI;IACxC,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,CAAC,KAAK,EAAE,IAAI,KAAK,EAAE,CAAC;CAChC,CAAA;AAED,MAAM,MAAM,YAAY,GAAG;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;CACjB,CAAA;
|
|
1
|
+
{"version":3,"file":"Types.d.ts","sourceRoot":"","sources":["../../../src/common/VersionedSchema/Types.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,OAAO,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAE/C,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI;IAC7B,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IACtB,IAAI,EAAE,oBAAoB,CAAC;IAC3B,OAAO,EAAE,MAAM,CAAC;CACnB,CAAA;AAED,MAAM,MAAM,mBAAmB,CAAC,IAAI,EAAE,EAAE,IAAI;IACxC,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,CAAC,KAAK,EAAE,IAAI,KAAK,EAAE,CAAC;CAChC,CAAA;AAED,MAAM,MAAM,YAAY,GAAG;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;CACjB,CAAA;AAGD,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,IAAI,EAAE,MAAM,EAAE,CAAC,KAAK,OAAO,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { VersionedSchema } from '../Types';
|
|
2
|
+
type VersionMatchResult = {
|
|
3
|
+
lastVersionIndex: number;
|
|
4
|
+
latestVersion: string;
|
|
5
|
+
firstVersion: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function determineBestMatchingSchemaVersion(inputData: Record<string, any>, versionedSchemas: VersionedSchema<Record<string, any>>[]): VersionMatchResult;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=determineBestMatchingSchemaVersion.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"determineBestMatchingSchemaVersion.d.ts","sourceRoot":"","sources":["../../../../src/common/VersionedSchema/Utils/determineBestMatchingSchemaVersion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAK3C,KAAK,kBAAkB,GAAG;IACtB,gBAAgB,EAAE,MAAM,CAAC;IACzB,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;CACxB,CAAC;AAGF,wBAAgB,kCAAkC,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,gBAAgB,EAAE,eAAe,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,EAAE,GAAG,kBAAkB,CA4C/J"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.determineBestMatchingSchemaVersion = determineBestMatchingSchemaVersion;
|
|
4
|
+
const getErrorFieldsFromValidation_1 = require("./getErrorFieldsFromValidation");
|
|
5
|
+
const getUniqueKeysFromSchemas_1 = require("./getUniqueKeysFromSchemas");
|
|
6
|
+
const getUniqueObjectKeys_1 = require("./getUniqueObjectKeys");
|
|
7
|
+
// Determining the most appropriate schema version
|
|
8
|
+
function determineBestMatchingSchemaVersion(inputData, versionedSchemas) {
|
|
9
|
+
if (!versionedSchemas.length) {
|
|
10
|
+
throw new Error('The list of schemes cannot be empty');
|
|
11
|
+
}
|
|
12
|
+
const inputKeys = (0, getUniqueObjectKeys_1.getUniqueObjectKeys)(inputData);
|
|
13
|
+
const matchingSchemas = versionedSchemas
|
|
14
|
+
.map(({ schema, version }, idx) => {
|
|
15
|
+
const { error } = schema.validate(inputData, { allowUnknown: true });
|
|
16
|
+
const errorFields = (0, getErrorFieldsFromValidation_1.getErrorFieldsFromValidation)(error).filter(err => err.type !== 'any.only');
|
|
17
|
+
return errorFields.length === 0 ? { version, index: idx } : null;
|
|
18
|
+
})
|
|
19
|
+
.filter((match) => match !== null);
|
|
20
|
+
if (matchingSchemas.length > 0) {
|
|
21
|
+
const latestMatch = matchingSchemas.reduce((prev, curr) => (curr.index > prev.index ? curr : prev));
|
|
22
|
+
return {
|
|
23
|
+
lastVersionIndex: latestMatch.index,
|
|
24
|
+
latestVersion: latestMatch.version,
|
|
25
|
+
firstVersion: matchingSchemas[0].version,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
const schemaMatches = versionedSchemas.map(({ schema, version }, idx) => {
|
|
29
|
+
const schemaKeys = (0, getUniqueKeysFromSchemas_1.getUniqueKeysFromSchemas)([schema]);
|
|
30
|
+
const { error } = schema.validate(inputData, { allowUnknown: true });
|
|
31
|
+
const errorFields = (0, getErrorFieldsFromValidation_1.getErrorFieldsFromValidation)(error).filter(err => err.type !== 'any.only');
|
|
32
|
+
const matchingKeys = inputKeys.filter(key => schemaKeys.has(key));
|
|
33
|
+
return { index: idx, version, errorFields, matchingKeys };
|
|
34
|
+
});
|
|
35
|
+
const bestMatch = schemaMatches.reduce((best, current) => {
|
|
36
|
+
const isBetterMatch = current.matchingKeys.length >= best.matchingKeys.length && current.errorFields.length <= best.errorFields.length;
|
|
37
|
+
return isBetterMatch ? current : best;
|
|
38
|
+
}, schemaMatches[0]);
|
|
39
|
+
return {
|
|
40
|
+
lastVersionIndex: bestMatch.index,
|
|
41
|
+
latestVersion: bestMatch.version,
|
|
42
|
+
firstVersion: bestMatch.version,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateKeyMappingForInvalidKeys.d.ts","sourceRoot":"","sources":["../../../../src/common/VersionedSchema/Utils/generateKeyMappingForInvalidKeys.ts"],"names":[],"mappings":"AAGA,wBAAgB,gCAAgC,CAAC,YAAY,EAAE,MAAM,EAAE,EAAE,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAiBtH"}
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
exports.generateKeyMappingForInvalidKeys = generateKeyMappingForInvalidKeys;
|
|
7
|
+
const leven_1 = __importDefault(require("leven"));
|
|
8
|
+
// Generating a key replacement card
|
|
9
|
+
function generateKeyMappingForInvalidKeys(rawInputKeys, allowedKeys) {
|
|
10
|
+
const result = new Map();
|
|
11
|
+
for (const key of rawInputKeys) {
|
|
12
|
+
if (!allowedKeys.has(key)) {
|
|
13
|
+
let bestMatch = null;
|
|
14
|
+
let bestDistance = Infinity;
|
|
15
|
+
for (const candidate of allowedKeys) {
|
|
16
|
+
const d = (0, leven_1.default)(key, candidate);
|
|
17
|
+
if (d < bestDistance && d <= 3) {
|
|
18
|
+
bestDistance = d;
|
|
19
|
+
bestMatch = candidate;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (bestMatch)
|
|
23
|
+
result.set(key, bestMatch);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getErrorFieldsFromValidation.d.ts","sourceRoot":"","sources":["../../../../src/common/VersionedSchema/Utils/getErrorFieldsFromValidation.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,KAAK,CAAC;AAGtB,wBAAgB,4BAA4B,CAAC,KAAK,EAAE,GAAG,CAAC,eAAe,GAAG,SAAS;;;IAElF"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getErrorFieldsFromValidation = getErrorFieldsFromValidation;
|
|
4
|
+
// Auxiliary function for getting error fields from the Joi validation result
|
|
5
|
+
function getErrorFieldsFromValidation(error) {
|
|
6
|
+
return error?.details.map(detail => ({ path: Array.isArray(detail.path) ? detail.path.join('.') : detail.path, type: detail.type })) ?? [];
|
|
7
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import Joi from
|
|
1
|
+
import Joi from 'joi';
|
|
2
2
|
/**
|
|
3
3
|
* Get all the unique parameter keys of all Joi schemes in the array
|
|
4
4
|
* @param schemas array of joi schemes
|
|
5
5
|
* @returns All the unique parameter keys of all Joi schemes in the array
|
|
6
6
|
*/
|
|
7
|
-
export declare function
|
|
8
|
-
//# sourceMappingURL=
|
|
7
|
+
export declare function getUniqueKeysFromSchemas(schemas: Joi.Schema[]): Set<string>;
|
|
8
|
+
//# sourceMappingURL=getUniqueKeysFromSchemas.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getUniqueKeysFromSchemas.d.ts","sourceRoot":"","sources":["../../../../src/common/VersionedSchema/Utils/getUniqueKeysFromSchemas.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,KAAK,CAAC;AAKtB;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,eAQ7D"}
|
package/dist/common/VersionedSchema/Utils/{getUniqueAllShemasKeys.js → getUniqueKeysFromSchemas.js}
RENAMED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.getUniqueKeysFromSchemas = getUniqueKeysFromSchemas;
|
|
4
4
|
const joiHandlers_1 = require("./joiHandlers");
|
|
5
|
-
const
|
|
5
|
+
const traverseObjectStructure_1 = require("./traverseObjectStructure");
|
|
6
6
|
/**
|
|
7
7
|
* Get all the unique parameter keys of all Joi schemes in the array
|
|
8
8
|
* @param schemas array of joi schemes
|
|
9
9
|
* @returns All the unique parameter keys of all Joi schemes in the array
|
|
10
10
|
*/
|
|
11
|
-
function
|
|
11
|
+
function getUniqueKeysFromSchemas(schemas) {
|
|
12
12
|
const result = new Set();
|
|
13
13
|
for (const schema of schemas) {
|
|
14
|
-
(0,
|
|
14
|
+
(0, traverseObjectStructure_1.traverseObjectStructure)(schema, [joiHandlers_1.handleJoiSchema, joiHandlers_1.handleJoiDescription], result);
|
|
15
15
|
}
|
|
16
16
|
return result;
|
|
17
17
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getUniqueObjectKeys = getUniqueObjectKeys;
|
|
4
|
-
const
|
|
4
|
+
const traverseObjectStructure_1 = require("./traverseObjectStructure");
|
|
5
5
|
/**
|
|
6
6
|
* Get unique keys from object properties (set of options)
|
|
7
7
|
* @param rawInput object properties (set of options)
|
|
@@ -9,6 +9,6 @@ const traverseGeneric_1 = require("./traverseGeneric");
|
|
|
9
9
|
*/
|
|
10
10
|
function getUniqueObjectKeys(rawInput) {
|
|
11
11
|
const result = new Set();
|
|
12
|
-
(0,
|
|
12
|
+
(0, traverseObjectStructure_1.traverseObjectStructure)(rawInput, [], result);
|
|
13
13
|
return Array.from(result);
|
|
14
14
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { TraverseHandler } from
|
|
1
|
+
import { TraverseHandler } from '../Types';
|
|
2
2
|
/** Handler for Joi.Schema: only the launch remains .describe() */
|
|
3
|
-
export declare const
|
|
3
|
+
export declare const handleJoiSchema: TraverseHandler<Set<string>>;
|
|
4
4
|
/** Handler for the result of describe(): pulls out only the real keys of the field */
|
|
5
|
-
export declare const
|
|
5
|
+
export declare const handleJoiDescription: TraverseHandler<Set<string>>;
|
|
6
6
|
//# sourceMappingURL=joiHandlers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"joiHandlers.d.ts","sourceRoot":"","sources":["../../../../src/common/VersionedSchema/Utils/joiHandlers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAE3C,kEAAkE;AAClE,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"joiHandlers.d.ts","sourceRoot":"","sources":["../../../../src/common/VersionedSchema/Utils/joiHandlers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAE3C,kEAAkE;AAClE,eAAO,MAAM,eAAe,EAAE,eAAe,CAAC,GAAG,CAAC,MAAM,CAAC,CAOxD,CAAC;AAEF,sFAAsF;AACtF,eAAO,MAAM,oBAAoB,EAAE,eAAe,CAAC,GAAG,CAAC,MAAM,CAAC,CAgD7D,CAAC"}
|
|
@@ -1,52 +1,59 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.handleJoiDescription = exports.handleJoiSchema = void 0;
|
|
4
4
|
/** Handler for Joi.Schema: only the launch remains .describe() */
|
|
5
|
-
const
|
|
5
|
+
const handleJoiSchema = (value, recurse) => {
|
|
6
6
|
if (value && typeof value.describe === 'function') {
|
|
7
7
|
recurse(value.describe());
|
|
8
|
-
|
|
8
|
+
// then the default bypass to the scheme itself is not needed.
|
|
9
|
+
return true;
|
|
9
10
|
}
|
|
10
11
|
return false;
|
|
11
12
|
};
|
|
12
|
-
exports.
|
|
13
|
+
exports.handleJoiSchema = handleJoiSchema;
|
|
13
14
|
/** Handler for the result of describe(): pulls out only the real keys of the field */
|
|
14
|
-
const
|
|
15
|
+
const handleJoiDescription = (value, recurse, result) => {
|
|
15
16
|
if (!value || typeof value !== 'object') {
|
|
16
17
|
return false;
|
|
17
18
|
}
|
|
18
|
-
let
|
|
19
|
+
let processed = false;
|
|
19
20
|
// only if there are keys/items/matches/patterns, we assume that this description
|
|
20
21
|
if (value.keys && typeof value.keys === 'object') {
|
|
21
|
-
|
|
22
|
+
processed = true;
|
|
22
23
|
for (const key of Object.keys(value.keys)) {
|
|
23
24
|
result.add(key);
|
|
24
25
|
recurse(value.keys[key]);
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
28
|
if (Array.isArray(value.items)) {
|
|
28
|
-
|
|
29
|
-
for (const item of value.items)
|
|
29
|
+
processed = true;
|
|
30
|
+
for (const item of value.items) {
|
|
30
31
|
recurse(item);
|
|
32
|
+
}
|
|
31
33
|
}
|
|
32
34
|
if (Array.isArray(value.matches)) {
|
|
33
|
-
|
|
35
|
+
processed = true;
|
|
34
36
|
for (const m of value.matches) {
|
|
35
|
-
if (m.schema)
|
|
37
|
+
if (m.schema) {
|
|
36
38
|
recurse(m.schema);
|
|
37
|
-
|
|
39
|
+
}
|
|
40
|
+
if (m.then) {
|
|
38
41
|
recurse(m.then);
|
|
39
|
-
|
|
42
|
+
}
|
|
43
|
+
if (m.otherwise) {
|
|
40
44
|
recurse(m.otherwise);
|
|
45
|
+
}
|
|
41
46
|
}
|
|
42
47
|
}
|
|
43
48
|
if (Array.isArray(value.patterns)) {
|
|
44
|
-
|
|
49
|
+
processed = true;
|
|
45
50
|
for (const p of value.patterns) {
|
|
46
|
-
if (p.rule)
|
|
51
|
+
if (p.rule) {
|
|
47
52
|
recurse(p.rule);
|
|
53
|
+
}
|
|
48
54
|
}
|
|
49
55
|
}
|
|
50
|
-
|
|
56
|
+
// if this is the description, the default bypass is not needed.
|
|
57
|
+
return processed;
|
|
51
58
|
};
|
|
52
|
-
exports.
|
|
59
|
+
exports.handleJoiDescription = handleJoiDescription;
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { EVersionedSchemaType } from
|
|
2
|
-
import { SchemaMigrationPlan, VersionedSchema } from
|
|
1
|
+
import { EVersionedSchemaType } from '../Enums';
|
|
2
|
+
import { SchemaMigrationPlan, VersionedSchema } from '../Types';
|
|
3
3
|
type MigrateToLatestProps = {
|
|
4
4
|
rawInput: Record<string, any>;
|
|
5
|
-
versionedSchemas: VersionedSchema<Record<string, any>>[];
|
|
6
5
|
migrationPlans: SchemaMigrationPlan<Record<string, any>, Record<string, any>>[];
|
|
6
|
+
versionedSchemas: VersionedSchema<Record<string, any>>[];
|
|
7
7
|
};
|
|
8
8
|
type MigrateToLatestResult = {
|
|
9
9
|
value: Record<string, any>;
|
|
10
10
|
schemaVersion: string;
|
|
11
11
|
schemaType: EVersionedSchemaType;
|
|
12
12
|
};
|
|
13
|
-
export declare function
|
|
13
|
+
export declare function migrateDataToLatestSchemaVersion({ rawInput, migrationPlans, versionedSchemas }: MigrateToLatestProps): MigrateToLatestResult | null;
|
|
14
14
|
export {};
|
|
15
|
-
//# sourceMappingURL=
|
|
15
|
+
//# sourceMappingURL=migrateDataToLatestSchemaVersion.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrateDataToLatestSchemaVersion.d.ts","sourceRoot":"","sources":["../../../../src/common/VersionedSchema/Utils/migrateDataToLatestSchemaVersion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAChD,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAQhE,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,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC;CAC5D,CAAC;AAEF,KAAK,qBAAqB,GAAG;IACzB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC3B,aAAa,EAAE,MAAM,CAAC;IACtB,UAAU,EAAE,oBAAoB,CAAC;CACpC,CAAC;AAEF,wBAAgB,gCAAgC,CAAC,EAAE,QAAQ,EAAE,cAAc,EAAE,gBAAgB,EAAE,EAAE,oBAAoB,GAAG,qBAAqB,GAAG,IAAI,CAwCnJ"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.migrateDataToLatestSchemaVersion = migrateDataToLatestSchemaVersion;
|
|
4
|
+
const determineBestMatchingSchemaVersion_1 = require("./determineBestMatchingSchemaVersion");
|
|
5
|
+
const generateKeyMappingForInvalidKeys_1 = require("./generateKeyMappingForInvalidKeys");
|
|
6
|
+
const getUniqueKeysFromSchemas_1 = require("./getUniqueKeysFromSchemas");
|
|
7
|
+
const getUniqueObjectKeys_1 = require("./getUniqueObjectKeys");
|
|
8
|
+
const replaceInvalidKeysWithMappedNames_1 = require("./replaceInvalidKeysWithMappedNames");
|
|
9
|
+
const validateAndSuggestKeyCorrections_1 = require("./validateAndSuggestKeyCorrections");
|
|
10
|
+
function migrateDataToLatestSchemaVersion({ rawInput, migrationPlans, versionedSchemas }) {
|
|
11
|
+
const schemas = versionedSchemas.map(el => el.schema);
|
|
12
|
+
const allUniqueKeysFromSchemas = (0, getUniqueKeysFromSchemas_1.getUniqueKeysFromSchemas)(schemas);
|
|
13
|
+
const allUniqueKeysFromRawInput = (0, getUniqueObjectKeys_1.getUniqueObjectKeys)(rawInput);
|
|
14
|
+
(0, validateAndSuggestKeyCorrections_1.validateAndSuggestKeyCorrections)(allUniqueKeysFromRawInput, allUniqueKeysFromSchemas);
|
|
15
|
+
const guessedVersion = (0, determineBestMatchingSchemaVersion_1.determineBestMatchingSchemaVersion)(rawInput, versionedSchemas);
|
|
16
|
+
const guessedValidationSchema = schemas[guessedVersion.lastVersionIndex];
|
|
17
|
+
const schemaPossibleKeys = (0, getUniqueKeysFromSchemas_1.getUniqueKeysFromSchemas)([guessedValidationSchema]);
|
|
18
|
+
const replacingKeysMap = (0, generateKeyMappingForInvalidKeys_1.generateKeyMappingForInvalidKeys)(allUniqueKeysFromRawInput, schemaPossibleKeys);
|
|
19
|
+
let currentData = replacingKeysMap.size ? (0, replaceInvalidKeysWithMappedNames_1.replaceInvalidKeysWithMappedNames)(rawInput, replacingKeysMap) : rawInput;
|
|
20
|
+
for (let idx = guessedVersion.lastVersionIndex; idx < versionedSchemas.length - 1; idx++) {
|
|
21
|
+
const fromVersion = versionedSchemas[idx].version;
|
|
22
|
+
const migrationPlan = migrationPlans.find(p => p.fromVersion === fromVersion);
|
|
23
|
+
if (!migrationPlan) {
|
|
24
|
+
throw new Error(`No migration plan from ${fromVersion}`);
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
const migratedRaw = migrationPlan.migrate(currentData);
|
|
28
|
+
const { error } = versionedSchemas[idx + 1].schema.validate(migratedRaw, { allowUnknown: false });
|
|
29
|
+
if (error) {
|
|
30
|
+
throw new Error(`Error during post-migration validation ${fromVersion}->${migrationPlan.toVersion}: ${error.message}`);
|
|
31
|
+
}
|
|
32
|
+
currentData = { ...migratedRaw };
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
throw new Error(e.message);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const currentSchema = versionedSchemas[versionedSchemas.length - 1];
|
|
39
|
+
const { error, value } = currentSchema.schema.validate(currentData, { allowUnknown: false });
|
|
40
|
+
if (error)
|
|
41
|
+
return null;
|
|
42
|
+
return { value, schemaVersion: currentSchema.version, schemaType: currentSchema.type };
|
|
43
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replaceInvalidKeysWithMappedNames.d.ts","sourceRoot":"","sources":["../../../../src/common/VersionedSchema/Utils/replaceInvalidKeysWithMappedNames.ts"],"names":[],"mappings":"AAEA,wBAAgB,iCAAiC,CAC7C,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,EAClD,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAC5B,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAe7C"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.replaceInvalidKeysWithMappedNames = replaceInvalidKeysWithMappedNames;
|
|
4
|
+
// Replacing incorrect keys
|
|
5
|
+
function replaceInvalidKeysWithMappedNames(input, keyMap) {
|
|
6
|
+
if (!input) {
|
|
7
|
+
throw new Error("The input data cannot be null or undefined.");
|
|
8
|
+
}
|
|
9
|
+
if (Array.isArray(input)) {
|
|
10
|
+
return input.map(item => replaceInvalidKeysWithMappedNames(item, keyMap));
|
|
11
|
+
}
|
|
12
|
+
const replacedKeys = Object.entries(input).map(([key, value]) => {
|
|
13
|
+
const newKey = keyMap.get(key) ?? key;
|
|
14
|
+
const newValue = Array.isArray(value) ? replaceInvalidKeysWithMappedNames(value, keyMap) : value;
|
|
15
|
+
return [newKey, newValue];
|
|
16
|
+
});
|
|
17
|
+
return Object.fromEntries(replacedKeys);
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"traverseObjectStructure.d.ts","sourceRoot":"","sources":["../../../../src/common/VersionedSchema/Utils/traverseObjectStructure.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAE3C,wBAAgB,uBAAuB,CAAC,CAAC,EACvC,IAAI,EAAE,GAAG,EACT,QAAQ,EAAE,eAAe,CAAC,CAAC,CAAC,EAAE,EAC9B,MAAM,EAAE,CAAC,QA6BV"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
function
|
|
5
|
-
const recurse = (v) =>
|
|
3
|
+
exports.traverseObjectStructure = traverseObjectStructure;
|
|
4
|
+
function traverseObjectStructure(root, handlers, result) {
|
|
5
|
+
const recurse = (v) => traverseObjectStructure(v, handlers, result);
|
|
6
6
|
// We try "special" handlers
|
|
7
7
|
let handled = false;
|
|
8
8
|
for (const handler of handlers) {
|
|
@@ -21,7 +21,9 @@ function traverseGeneric(root, handlers, result) {
|
|
|
21
21
|
}
|
|
22
22
|
else {
|
|
23
23
|
for (const key of Object.keys(root)) {
|
|
24
|
-
result
|
|
24
|
+
if (result instanceof Set) {
|
|
25
|
+
result.add(key);
|
|
26
|
+
}
|
|
25
27
|
recurse(root[key]);
|
|
26
28
|
}
|
|
27
29
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optimized unknown key verification function using Levenshtein distance map.
|
|
3
|
+
*
|
|
4
|
+
* @param rawInputKeys Array of raw input keys
|
|
5
|
+
* @param allowedKeys Set of allowed (required) keys
|
|
6
|
+
*/
|
|
7
|
+
export declare function validateAndSuggestKeyCorrections(rawInputKeys: string[], allowedKeys: Set<string>): void;
|
|
8
|
+
//# sourceMappingURL=validateAndSuggestKeyCorrections.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validateAndSuggestKeyCorrections.d.ts","sourceRoot":"","sources":["../../../../src/common/VersionedSchema/Utils/validateAndSuggestKeyCorrections.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,wBAAgB,gCAAgC,CAAC,YAAY,EAAE,MAAM,EAAE,EAAE,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,QAiChG"}
|
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
exports.validateAndSuggestKeyCorrections = validateAndSuggestKeyCorrections;
|
|
7
|
+
const leven_1 = __importDefault(require("leven"));
|
|
8
|
+
/**
|
|
9
|
+
* Optimized unknown key verification function using Levenshtein distance map.
|
|
10
|
+
*
|
|
11
|
+
* @param rawInputKeys Array of raw input keys
|
|
12
|
+
* @param allowedKeys Set of allowed (required) keys
|
|
13
|
+
*/
|
|
14
|
+
function validateAndSuggestKeyCorrections(rawInputKeys, allowedKeys) {
|
|
15
|
+
const errors = [];
|
|
16
|
+
const distanceMap = new Map();
|
|
17
|
+
// Creating a distance map once for all keys
|
|
18
|
+
for (const key of rawInputKeys) {
|
|
19
|
+
if (!allowedKeys.has(key)) {
|
|
20
|
+
let bestMatch = null;
|
|
21
|
+
let bestDistance = Infinity;
|
|
22
|
+
for (const candidate of allowedKeys) {
|
|
23
|
+
const d = (0, leven_1.default)(key, candidate);
|
|
24
|
+
if (d < bestDistance) {
|
|
25
|
+
bestDistance = d;
|
|
26
|
+
bestMatch = candidate;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
distanceMap.set(key, { bestMatch, bestDistance });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
// Generating error messages
|
|
33
|
+
for (const [key, { bestMatch, bestDistance }] of distanceMap) {
|
|
34
|
+
if (bestMatch && bestDistance <= 3) {
|
|
35
|
+
errors.push(`The "${key}" field is not recognized. Perhaps you meant "${bestMatch}".`);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
errors.push(`The "${key}" field is not recognized.`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (errors.length) {
|
|
42
|
+
throw new Error(errors.join('\n'));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare const mockJoiSchema: (keys: string[], isValid: boolean, errors?: {
|
|
2
|
+
path: string;
|
|
3
|
+
type: string;
|
|
4
|
+
}[], isArray?: boolean) => {
|
|
5
|
+
validate: (input: any, options: any) => {
|
|
6
|
+
error: {
|
|
7
|
+
details: {
|
|
8
|
+
path: string;
|
|
9
|
+
type: string;
|
|
10
|
+
}[];
|
|
11
|
+
} | null;
|
|
12
|
+
value: any;
|
|
13
|
+
};
|
|
14
|
+
describe: () => {
|
|
15
|
+
keys: {} | undefined;
|
|
16
|
+
items: {}[] | undefined;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=mockJoiSchema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mockJoiSchema.d.ts","sourceRoot":"","sources":["../../../../src/common/__tests__/__mock__/mockJoiSchema.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,aAAa,GAAI,MAAM,MAAM,EAAE,EAAE,SAAS,OAAO,EAAE,SAAQ;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,EAAO,EAAE,iBAAe;sBACxG,GAAG,WAAW,GAAG;;;sBADyC,MAAM;sBAAQ,MAAM;;;;;;;;;CASlG,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mockJoiSchema = void 0;
|
|
4
|
+
const mockJoiSchema = (keys, isValid, errors = [], isArray = false) => ({
|
|
5
|
+
validate: (input, options) => ({
|
|
6
|
+
error: isValid ? null : { details: errors },
|
|
7
|
+
value: input,
|
|
8
|
+
}),
|
|
9
|
+
describe: () => ({
|
|
10
|
+
keys: isArray ? undefined : keys.reduce((acc, key) => ({ ...acc, [key]: {} }), {}),
|
|
11
|
+
items: isArray ? [{}] : undefined,
|
|
12
|
+
}),
|
|
13
|
+
});
|
|
14
|
+
exports.mockJoiSchema = mockJoiSchema;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"determineBestMatchingSchemaVersion.test.d.ts","sourceRoot":"","sources":["../../../src/common/__tests__/determineBestMatchingSchemaVersion.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
const node_assert_1 = __importDefault(require("node:assert"));
|
|
7
|
+
const node_test_1 = require("node:test");
|
|
8
|
+
const Enums_1 = require("../../common/VersionedSchema/Enums");
|
|
9
|
+
const determineBestMatchingSchemaVersion_1 = require("../VersionedSchema/Utils/determineBestMatchingSchemaVersion");
|
|
10
|
+
const mockJoiSchema_1 = require("./__mock__/mockJoiSchema");
|
|
11
|
+
(0, node_test_1.describe)('determineBestMatchingSchemaVersion', () => {
|
|
12
|
+
(0, node_test_1.test)('@unit: it should return the latest version if there is an exact match.', async () => {
|
|
13
|
+
const schemas = [
|
|
14
|
+
{ schema: (0, mockJoiSchema_1.mockJoiSchema)(['name'], true), version: '1.0', type: Enums_1.EVersionedSchemaType.OPTIONS },
|
|
15
|
+
{ schema: (0, mockJoiSchema_1.mockJoiSchema)(['name', 'age'], true), version: '2.0', type: Enums_1.EVersionedSchemaType.OPTIONS },
|
|
16
|
+
];
|
|
17
|
+
const input = { name: 'John', age: 30 };
|
|
18
|
+
const result = (0, determineBestMatchingSchemaVersion_1.determineBestMatchingSchemaVersion)(input, schemas);
|
|
19
|
+
node_assert_1.default.deepEqual(result, {
|
|
20
|
+
lastVersionIndex: 1,
|
|
21
|
+
latestVersion: '2.0',
|
|
22
|
+
firstVersion: '1.0',
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
(0, node_test_1.test)('@unit: should return the best scheme in the absence of exact matches.', async () => {
|
|
26
|
+
const schemas = [
|
|
27
|
+
{ schema: (0, mockJoiSchema_1.mockJoiSchema)(['name'], false, [{ path: 'age', type: 'string' }]), version: '1.0', type: Enums_1.EVersionedSchemaType.OPTIONS },
|
|
28
|
+
{ schema: (0, mockJoiSchema_1.mockJoiSchema)(['name', 'age'], true), version: '2.0', type: Enums_1.EVersionedSchemaType.OPTIONS },
|
|
29
|
+
];
|
|
30
|
+
const input = { name: 'John', age: 30 };
|
|
31
|
+
const result = (0, determineBestMatchingSchemaVersion_1.determineBestMatchingSchemaVersion)(input, schemas);
|
|
32
|
+
node_assert_1.default.deepEqual(result, {
|
|
33
|
+
lastVersionIndex: 1,
|
|
34
|
+
latestVersion: '2.0',
|
|
35
|
+
firstVersion: '2.0',
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateKeyMappingForInvalidKeys.test.d.ts","sourceRoot":"","sources":["../../../src/common/__tests__/generateKeyMappingForInvalidKeys.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
const node_assert_1 = __importDefault(require("node:assert"));
|
|
7
|
+
const node_test_1 = require("node:test");
|
|
8
|
+
const generateKeyMappingForInvalidKeys_1 = require("../VersionedSchema/Utils/generateKeyMappingForInvalidKeys");
|
|
9
|
+
(0, node_test_1.describe)('generateKeyMappingForInvalidKeys', () => {
|
|
10
|
+
(0, node_test_1.test)('@unit: must create a replacement card for invalid keys', async () => {
|
|
11
|
+
const inputKeys = ['nmae', 'title'];
|
|
12
|
+
const allowedKeys = new Set(['name', 'title']);
|
|
13
|
+
const result = (0, generateKeyMappingForInvalidKeys_1.generateKeyMappingForInvalidKeys)(inputKeys, allowedKeys);
|
|
14
|
+
node_assert_1.default.deepEqual(result, new Map([['nmae', 'name']]));
|
|
15
|
+
});
|
|
16
|
+
(0, node_test_1.test)('@unit: must return an empty card if all keys are valid.', async () => {
|
|
17
|
+
const inputKeys = ['name', 'title'];
|
|
18
|
+
const allowedKeys = new Set(['name', 'title']);
|
|
19
|
+
const result = (0, generateKeyMappingForInvalidKeys_1.generateKeyMappingForInvalidKeys)(inputKeys, allowedKeys);
|
|
20
|
+
node_assert_1.default.deepEqual(result, new Map());
|
|
21
|
+
});
|
|
22
|
+
});
|