ondc-code-generator 0.0.1 → 0.0.3
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/constants/syntax.js +2 -0
- package/{src/example.ts → dist/example.js} +1 -4
- package/dist/{Generator → generator}/config-compiler.js +25 -4
- package/dist/{Generator → generator}/generators/documentation/markdown-message-generator.js +0 -13
- package/dist/{Generator → generator}/generators/typescript/ts-generator.js +3 -4
- package/dist/{Generator → generator}/validators/abstract-validator.js +1 -1
- package/dist/{Generator → generator}/validators/config-validator.js +4 -4
- package/dist/{Generator → generator}/validators/session-data-config/session-data-validator.js +2 -2
- package/dist/{Generator → generator}/validators/tests-config/sub-validations.js +38 -33
- package/dist/{Generator → generator}/validators/tests-config/test-list-validator.js +3 -3
- package/dist/{Generator → generator}/validators/tests-config/test-validator.js +7 -7
- package/dist/index.js +1 -38
- package/dist/services/return-complier/tokens.js +3 -0
- package/dist/utils/general-utils/string-utils.js +10 -2
- package/package.json +6 -4
- package/dist/Generator/config-validator.js +0 -21
- package/dist/Generator/generators/markdown-message-generator.js +0 -25
- package/dist/Generator/pipline.js +0 -1
- package/dist/Generator/validators/validation-error.js +0 -9
- package/dist/services/rename-later/main.js +0 -1
- package/dist/services/rename-later/parser.js +0 -22
- package/dist/services/rename-later/tokens.js +0 -32
- package/dist/services/rename-later/tokens1.js +0 -33
- package/dist/utils/file-system.js +0 -1
- package/src/constants/operations.ts +0 -19
- package/src/constants/syntax.ts +0 -81
- package/src/generator/config-compiler.ts +0 -122
- package/src/generator/generators/classes/abstract-generator.ts +0 -29
- package/src/generator/generators/documentation/markdown-message-generator.ts +0 -43
- package/src/generator/generators/documentation/md-generator.ts +0 -76
- package/src/generator/generators/documentation/templates/index.mustache +0 -36
- package/src/generator/generators/documentation/templates/style.css +0 -204
- package/src/generator/generators/python/py-generator.ts +0 -0
- package/src/generator/generators/typescript/templates/api-test.mustache +0 -7
- package/src/generator/generators/typescript/templates/json-path-utils.ts +0 -17
- package/src/generator/generators/typescript/templates/schema-template.mustache +0 -18
- package/src/generator/generators/typescript/templates/test-config.mustache +0 -28
- package/src/generator/generators/typescript/templates/test-object.mustache +0 -20
- package/src/generator/generators/typescript/templates/validation-code.mustache +0 -39
- package/src/generator/generators/typescript/templates/validation-utils.ts +0 -117
- package/src/generator/generators/typescript/ts-ast.ts +0 -72
- package/src/generator/generators/typescript/ts-generator.ts +0 -275
- package/src/generator/validators/abstract-validator.ts +0 -23
- package/src/generator/validators/config-validator.ts +0 -55
- package/src/generator/validators/session-data-config/session-data-validator.ts +0 -58
- package/src/generator/validators/tests-config/sub-validations.ts +0 -302
- package/src/generator/validators/tests-config/test-list-validator.ts +0 -59
- package/src/generator/validators/tests-config/test-validator.ts +0 -69
- package/src/index.ts +0 -2
- package/src/services/return-complier/ast-functions/compile-to-markdown.ts +0 -152
- package/src/services/return-complier/ast-functions/semantic-validations.ts +0 -44
- package/src/services/return-complier/ast.ts +0 -147
- package/src/services/return-complier/combined.ts +0 -8
- package/src/services/return-complier/parser.ts +0 -128
- package/src/services/return-complier/tokens.ts +0 -184
- package/src/services/schema-service.ts +0 -42
- package/src/types/build.ts +0 -51
- package/src/types/compiler-types.ts +0 -3
- package/src/types/config-types.ts +0 -27
- package/src/types/error-codes.ts +0 -6
- package/src/types/general-types.ts +0 -2
- package/src/utils/config-utils/json-schema-utils.ts +0 -150
- package/src/utils/config-utils/yaml.ts +0 -17
- package/src/utils/fs-utils.ts +0 -32
- package/src/utils/general-utils/string-utils.ts +0 -76
- package/src/utils/general-utils/test-object-utils.ts +0 -14
- package/src/utils/general-utils/validation-utils.ts +0 -30
- package/src/utils/json-path-utils/extract-string-paths.ts +0 -139
- package/src/utils/json-path-utils/paths.ts +0 -44
- package/src/utils/logger.ts +0 -53
- package/tsconfig.json +0 -17
- /package/dist/{Generator → generator}/generators/classes/abstract-generator.js +0 -0
- /package/dist/{Generator → generator}/generators/documentation/md-generator.js +0 -0
- /package/dist/{Generator → generator}/generators/python/py-generator.js +0 -0
- /package/dist/{Generator → generator}/generators/typescript/templates/json-path-utils.js +0 -0
- /package/dist/{Generator → generator}/generators/typescript/templates/validation-utils.js +0 -0
- /package/dist/{Generator → generator}/generators/typescript/ts-ast.js +0 -0
|
@@ -1,275 +0,0 @@
|
|
|
1
|
-
import { readFileSync } from "fs";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { fileURLToPath } from "url";
|
|
4
|
-
import { ConfigSyntax, TestObjectSyntax } from "../../../constants/syntax.js";
|
|
5
|
-
import Mustache from "mustache";
|
|
6
|
-
import { markdownMessageGenerator } from "../documentation/markdown-message-generator.js";
|
|
7
|
-
import { getVariablesFromTest as extractVariablesFromText } from "../../../utils/general-utils/test-object-utils.js";
|
|
8
|
-
import { ConfigVariable, TestObject } from "../../../types/config-types.js";
|
|
9
|
-
import { ConvertArrayToString } from "../../../utils/general-utils/string-utils.js";
|
|
10
|
-
import { compileInputToTs } from "./ts-ast.js";
|
|
11
|
-
import { CodeGenerator } from "../classes/abstract-generator.js";
|
|
12
|
-
import { writeAndFormatCode } from "../../../utils/fs-utils.js";
|
|
13
|
-
import { ErrorDefinition } from "../../../types/error-codes.js";
|
|
14
|
-
import { MarkdownDocGenerator } from "../documentation/md-generator.js";
|
|
15
|
-
|
|
16
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
17
|
-
const __dirname = path.dirname(__filename);
|
|
18
|
-
|
|
19
|
-
export class TypescriptGenerator extends CodeGenerator {
|
|
20
|
-
generateSessionDataCode = async () => {
|
|
21
|
-
throw new Error("Method not implemented.");
|
|
22
|
-
};
|
|
23
|
-
generateValidationCode = async () => {
|
|
24
|
-
const testConfig = this.validationConfig[ConfigSyntax.Tests];
|
|
25
|
-
for (const key in testConfig) {
|
|
26
|
-
const testObjects = testConfig[key];
|
|
27
|
-
const betaConfig = {
|
|
28
|
-
[TestObjectSyntax.Name]: key,
|
|
29
|
-
[TestObjectSyntax.Return]: testObjects,
|
|
30
|
-
};
|
|
31
|
-
const testFunction = await this.generateTestFunction(betaConfig);
|
|
32
|
-
const apiTestTemplate = readFileSync(
|
|
33
|
-
path.resolve(__dirname, "./templates/api-test.mustache"),
|
|
34
|
-
"utf-8"
|
|
35
|
-
);
|
|
36
|
-
const finalCode = Mustache.render(apiTestTemplate, {
|
|
37
|
-
functionCode: testFunction.code,
|
|
38
|
-
});
|
|
39
|
-
await writeAndFormatCode(
|
|
40
|
-
this.rootPath,
|
|
41
|
-
`./api-tests/${key}.ts`,
|
|
42
|
-
finalCode,
|
|
43
|
-
"typescript"
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
generateCode = async () => {
|
|
48
|
-
const jsonPathUtilsCode = readFileSync(
|
|
49
|
-
path.resolve(__dirname, "./templates/json-path-utils.ts"),
|
|
50
|
-
"utf-8"
|
|
51
|
-
);
|
|
52
|
-
const validtionUtils = readFileSync(
|
|
53
|
-
path.resolve(__dirname, "./templates/validation-utils.ts"),
|
|
54
|
-
"utf-8"
|
|
55
|
-
);
|
|
56
|
-
const typesTemplate = readFileSync(
|
|
57
|
-
path.resolve(__dirname, "./templates/test-config.mustache"),
|
|
58
|
-
"utf-8"
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
const typesCode = Mustache.render(typesTemplate, {
|
|
62
|
-
externalData: this.getExternalKeys(),
|
|
63
|
-
});
|
|
64
|
-
writeAndFormatCode(
|
|
65
|
-
this.rootPath,
|
|
66
|
-
"./utils/json-path-utils.ts",
|
|
67
|
-
jsonPathUtilsCode,
|
|
68
|
-
"typescript"
|
|
69
|
-
);
|
|
70
|
-
writeAndFormatCode(
|
|
71
|
-
this.rootPath,
|
|
72
|
-
"./utils/validation-utils.ts",
|
|
73
|
-
validtionUtils,
|
|
74
|
-
"typescript"
|
|
75
|
-
);
|
|
76
|
-
writeAndFormatCode(
|
|
77
|
-
this.rootPath,
|
|
78
|
-
"./types/test-config.ts",
|
|
79
|
-
typesCode,
|
|
80
|
-
"typescript"
|
|
81
|
-
);
|
|
82
|
-
await this.generateValidationCode();
|
|
83
|
-
await writeAndFormatCode(
|
|
84
|
-
this.rootPath,
|
|
85
|
-
"error.ts",
|
|
86
|
-
this.generateErrorFile(this.errorCodes),
|
|
87
|
-
"typescript"
|
|
88
|
-
);
|
|
89
|
-
await writeAndFormatCode(
|
|
90
|
-
this.rootPath,
|
|
91
|
-
"index.ts",
|
|
92
|
-
this.generateIndexFile(
|
|
93
|
-
Object.keys(this.validationConfig[ConfigSyntax.Tests])
|
|
94
|
-
),
|
|
95
|
-
"typescript"
|
|
96
|
-
);
|
|
97
|
-
await new MarkdownDocGenerator(
|
|
98
|
-
this.validationConfig,
|
|
99
|
-
this.errorCodes,
|
|
100
|
-
this.rootPath
|
|
101
|
-
).generateCode();
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
generateTestFunction = async (testObject: TestObject) => {
|
|
105
|
-
const template = readFileSync(
|
|
106
|
-
path.resolve(__dirname, "./templates/test-object.mustache"),
|
|
107
|
-
"utf-8"
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
const view: mustachRequirements = {
|
|
111
|
-
name: testObject[TestObjectSyntax.Name],
|
|
112
|
-
scopePath: testObject[TestObjectSyntax.Scope] ?? "$",
|
|
113
|
-
variables: this.createVariablesCode(testObject),
|
|
114
|
-
hasContinue: testObject[TestObjectSyntax.Continue] ? true : false,
|
|
115
|
-
skipCheckStatement: testObject[TestObjectSyntax.Continue]
|
|
116
|
-
? compileInputToTs(testObject[TestObjectSyntax.Continue])
|
|
117
|
-
: undefined,
|
|
118
|
-
validationCode: await this.createValidationLogicCode(testObject),
|
|
119
|
-
successCode: testObject[TestObjectSyntax.SuccessCode] ?? 200,
|
|
120
|
-
};
|
|
121
|
-
return {
|
|
122
|
-
funcName: testObject[TestObjectSyntax.Name],
|
|
123
|
-
code: Mustache.render(template, view),
|
|
124
|
-
};
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
private CreateErrorMarkdown(
|
|
128
|
-
testObject: TestObject,
|
|
129
|
-
skipList: string[] | undefined
|
|
130
|
-
) {
|
|
131
|
-
return markdownMessageGenerator(
|
|
132
|
-
testObject[TestObjectSyntax.Return] as string,
|
|
133
|
-
testObject,
|
|
134
|
-
testObject[TestObjectSyntax.Name],
|
|
135
|
-
skipList
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
private createVariablesCode(testObject: TestObject) {
|
|
140
|
-
const variables: { name: string; value: string }[] = [];
|
|
141
|
-
const varNames = extractVariablesFromText(testObject);
|
|
142
|
-
for (const name of varNames) {
|
|
143
|
-
const value = testObject[name] as ConfigVariable;
|
|
144
|
-
const final =
|
|
145
|
-
typeof value === "string"
|
|
146
|
-
? `payloadUtils.getJsonPath(testObj, "${value}")`
|
|
147
|
-
: ConvertArrayToString(value);
|
|
148
|
-
variables.push({
|
|
149
|
-
name: name,
|
|
150
|
-
value: final,
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
return variables;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
private createValidationLogicCode = async (testObject: TestObject) => {
|
|
157
|
-
const template = readFileSync(
|
|
158
|
-
path.resolve(__dirname, "./templates/validation-code.mustache"),
|
|
159
|
-
"utf-8"
|
|
160
|
-
);
|
|
161
|
-
const skip = testObject[TestObjectSyntax.Continue];
|
|
162
|
-
const skipList = skip ? [skip] : undefined;
|
|
163
|
-
if (typeof testObject[TestObjectSyntax.Return] === "string") {
|
|
164
|
-
const returnStatement = compileInputToTs(
|
|
165
|
-
testObject[TestObjectSyntax.Return]
|
|
166
|
-
);
|
|
167
|
-
return Mustache.render(template, {
|
|
168
|
-
isNested: false,
|
|
169
|
-
returnStatement: returnStatement,
|
|
170
|
-
errorCode: testObject[TestObjectSyntax.ErrorCode] ?? 30000,
|
|
171
|
-
errorDescription: this.CreateErrorMarkdown(testObject, skipList),
|
|
172
|
-
});
|
|
173
|
-
} else {
|
|
174
|
-
const subObjects = testObject[TestObjectSyntax.Return];
|
|
175
|
-
const functionCodes: {
|
|
176
|
-
funcName: string;
|
|
177
|
-
code: string;
|
|
178
|
-
}[] = [];
|
|
179
|
-
for (const subObject of subObjects) {
|
|
180
|
-
const func = await this.generateTestFunction(subObject);
|
|
181
|
-
functionCodes.push(func);
|
|
182
|
-
}
|
|
183
|
-
const names = functionCodes.map((f) => {
|
|
184
|
-
return { name: f.funcName };
|
|
185
|
-
});
|
|
186
|
-
return Mustache.render(template, {
|
|
187
|
-
isNested: true,
|
|
188
|
-
nestedFunctions: functionCodes.map((f) => f.code).join("\n"),
|
|
189
|
-
names: names,
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
private generateErrorFile(errors: ErrorDefinition[]): string {
|
|
195
|
-
console.log(errors);
|
|
196
|
-
const allCodes = errors.map((error) => error.code);
|
|
197
|
-
if (allCodes.length !== new Set(allCodes).size) {
|
|
198
|
-
throw new Error("Duplicate error codes found");
|
|
199
|
-
}
|
|
200
|
-
errors.push({
|
|
201
|
-
code: 20006,
|
|
202
|
-
Description: "Invalid response does not meet API contract specifications",
|
|
203
|
-
});
|
|
204
|
-
errors.push({
|
|
205
|
-
code: 30000,
|
|
206
|
-
Description: "Invalid request does not meet API contract specifications",
|
|
207
|
-
});
|
|
208
|
-
const errorConstant = `
|
|
209
|
-
const errors = [
|
|
210
|
-
${errors.map((error) => `{code: ${error.code}, message: "${error.Description}"}`).join(",\n")}
|
|
211
|
-
];
|
|
212
|
-
`;
|
|
213
|
-
const errorFunction = `export function getError(code: number) {
|
|
214
|
-
const error = errors.find((error) => error.code === code);
|
|
215
|
-
if (!error) {
|
|
216
|
-
throw new Error(\`Error code \${code} not found\`);
|
|
217
|
-
}
|
|
218
|
-
return error;
|
|
219
|
-
}`;
|
|
220
|
-
return `${errorConstant}\n${errorFunction}`;
|
|
221
|
-
}
|
|
222
|
-
private getExternalKeys() {
|
|
223
|
-
const apis = Object.keys(this.validationConfig[ConfigSyntax.SessionData]);
|
|
224
|
-
const result: { name: string }[] = [];
|
|
225
|
-
for (const api of apis) {
|
|
226
|
-
const keys = Object.keys(
|
|
227
|
-
this.validationConfig[ConfigSyntax.SessionData][api]
|
|
228
|
-
);
|
|
229
|
-
for (const key of keys) {
|
|
230
|
-
result.push({ name: key });
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
return result;
|
|
234
|
-
}
|
|
235
|
-
private generateIndexFile(apis: string[]) {
|
|
236
|
-
const importsCode = apis
|
|
237
|
-
.map((api) => `import ${api} from "./api-tests/${api}";`)
|
|
238
|
-
.join("\n");
|
|
239
|
-
const masterFunction = `
|
|
240
|
-
export function performL1Validations(action: string, payload: any,allErrors = false, externalData = {}) {
|
|
241
|
-
switch (action) {
|
|
242
|
-
${apis
|
|
243
|
-
.map(
|
|
244
|
-
(api) => `case "${api}": return ${api}({
|
|
245
|
-
payload: payload,
|
|
246
|
-
externalData: externalData,
|
|
247
|
-
config: {
|
|
248
|
-
runAllValidations: allErrors,
|
|
249
|
-
},
|
|
250
|
-
});`
|
|
251
|
-
)
|
|
252
|
-
.join("\n")}
|
|
253
|
-
default:
|
|
254
|
-
throw new Error("Action not found");
|
|
255
|
-
}
|
|
256
|
-
}`;
|
|
257
|
-
return `
|
|
258
|
-
${importsCode}
|
|
259
|
-
${masterFunction}
|
|
260
|
-
`;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
interface mustachRequirements {
|
|
265
|
-
name: string;
|
|
266
|
-
scopePath: string;
|
|
267
|
-
variables: {
|
|
268
|
-
name: string;
|
|
269
|
-
value: string;
|
|
270
|
-
}[];
|
|
271
|
-
hasContinue: boolean;
|
|
272
|
-
skipCheckStatement?: string;
|
|
273
|
-
validationCode: string;
|
|
274
|
-
successCode: number;
|
|
275
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { TestObject } from "../../types/config-types";
|
|
2
|
-
import { ErrorDefinition } from "../../types/error-codes";
|
|
3
|
-
|
|
4
|
-
export interface IValidator {
|
|
5
|
-
validationPath: string;
|
|
6
|
-
validate(): Promise<void>;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export abstract class TestObjectValidator implements IValidator {
|
|
10
|
-
targetObject: TestObject;
|
|
11
|
-
validationPath: string;
|
|
12
|
-
abstract validate(): Promise<void>;
|
|
13
|
-
constructor(testObject: TestObject, path: string) {
|
|
14
|
-
this.targetObject = testObject;
|
|
15
|
-
this.validationPath = path;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface TestsValidatorDependencies {
|
|
20
|
-
stringJsonPaths: string[];
|
|
21
|
-
errorDefinitions: ErrorDefinition[];
|
|
22
|
-
externalVariables: string[];
|
|
23
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { ConfigSyntax } from "../../constants/syntax.js";
|
|
2
|
-
import { ValidationConfig } from "../../types/config-types.js";
|
|
3
|
-
import { ErrorDefinition } from "../../types/error-codes.js";
|
|
4
|
-
import { getExternalVariables } from "../../utils/general-utils/validation-utils.js";
|
|
5
|
-
import {
|
|
6
|
-
IValidator,
|
|
7
|
-
TestsValidatorDependencies,
|
|
8
|
-
} from "./abstract-validator.js";
|
|
9
|
-
import { SessionDataValidator } from "./session-data-config/session-data-validator.js";
|
|
10
|
-
import { TestsValidator } from "./tests-config/test-list-validator.js";
|
|
11
|
-
|
|
12
|
-
export class ConfigValidator implements IValidator {
|
|
13
|
-
validationPath: string;
|
|
14
|
-
config: ValidationConfig;
|
|
15
|
-
stringJsonPaths: Record<string, string[]>;
|
|
16
|
-
errorDefinitions: ErrorDefinition[];
|
|
17
|
-
constructor(
|
|
18
|
-
validationPath: string,
|
|
19
|
-
config: ValidationConfig,
|
|
20
|
-
stringJsonPaths: Record<string, string[]>,
|
|
21
|
-
errorDefinitions: ErrorDefinition[]
|
|
22
|
-
) {
|
|
23
|
-
this.validationPath = validationPath;
|
|
24
|
-
this.config = config;
|
|
25
|
-
this.stringJsonPaths = stringJsonPaths;
|
|
26
|
-
this.errorDefinitions = errorDefinitions;
|
|
27
|
-
}
|
|
28
|
-
validate = async () => {
|
|
29
|
-
if (!this.config[ConfigSyntax.Tests])
|
|
30
|
-
throw new Error(`Tests not found in config`);
|
|
31
|
-
if (!this.config[ConfigSyntax.SessionData])
|
|
32
|
-
throw new Error(`SessionData not found in config`);
|
|
33
|
-
|
|
34
|
-
const sessionData = this.config[ConfigSyntax.SessionData];
|
|
35
|
-
const tests = this.config[ConfigSyntax.Tests];
|
|
36
|
-
|
|
37
|
-
await new SessionDataValidator(
|
|
38
|
-
`${this.validationPath}/${ConfigSyntax.SessionData}`,
|
|
39
|
-
sessionData
|
|
40
|
-
).validate();
|
|
41
|
-
|
|
42
|
-
const externalVariables = getExternalVariables(sessionData);
|
|
43
|
-
|
|
44
|
-
for (const api in tests) {
|
|
45
|
-
const testList = tests[api];
|
|
46
|
-
const path = `${this.validationPath}/${ConfigSyntax.Tests}/${api}`;
|
|
47
|
-
const dependencies: TestsValidatorDependencies = {
|
|
48
|
-
stringJsonPaths: this.stringJsonPaths[api],
|
|
49
|
-
errorDefinitions: this.errorDefinitions,
|
|
50
|
-
externalVariables: externalVariables,
|
|
51
|
-
};
|
|
52
|
-
await new TestsValidator(testList, path, dependencies).validate();
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { SessionDataApi } from "../../../types/config-types.js";
|
|
2
|
-
import { isPrimitive } from "../../../utils/general-utils/validation-utils.js";
|
|
3
|
-
|
|
4
|
-
import { IValidator } from "../abstract-validator.js";
|
|
5
|
-
|
|
6
|
-
export class SessionDataValidator implements IValidator {
|
|
7
|
-
validationPath: string;
|
|
8
|
-
sessionData: Record<string, SessionDataApi>;
|
|
9
|
-
constructor(
|
|
10
|
-
validtionPath: string,
|
|
11
|
-
sessionData: Record<string, SessionDataApi>
|
|
12
|
-
) {
|
|
13
|
-
this.validationPath = validtionPath;
|
|
14
|
-
this.sessionData = sessionData;
|
|
15
|
-
}
|
|
16
|
-
validate = async () => {
|
|
17
|
-
Object.entries(this.sessionData).forEach(([key, value]) => {
|
|
18
|
-
const newPath = `${this.validationPath}/${key}`;
|
|
19
|
-
this.validateSessionData(value, newPath);
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
private validateSessionData(variable: any, path: string): void {
|
|
24
|
-
if (typeof variable !== "object" || variable === null) {
|
|
25
|
-
throw new Error(
|
|
26
|
-
`Invalid SessionData at ${path}: Expected an object, got ${typeof variable}`
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
Object.entries(variable).forEach(([key, value]) => {
|
|
31
|
-
const currentPath = `${path}.${key}`;
|
|
32
|
-
|
|
33
|
-
if (typeof key !== "string") {
|
|
34
|
-
throw new Error(
|
|
35
|
-
`Invalid key at ${currentPath}: Expected a string key, got ${typeof key}`
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (
|
|
40
|
-
value !== null &&
|
|
41
|
-
typeof value !== "string" &&
|
|
42
|
-
(!Array.isArray(value) ||
|
|
43
|
-
!value.every((item, index) => {
|
|
44
|
-
if (!isPrimitive(item)) {
|
|
45
|
-
throw new Error(
|
|
46
|
-
`Invalid array value at ${currentPath}[${index}]: Expected a primitive, got ${typeof item}`
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
return true;
|
|
50
|
-
}))
|
|
51
|
-
) {
|
|
52
|
-
throw new Error(
|
|
53
|
-
`Invalid value at ${currentPath}: ${JSON.stringify(value)}`
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
}
|