ondc-code-generator 0.0.2 → 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/package.json +3 -3
- package/src/constants/operations.ts +0 -19
- package/src/constants/syntax.ts +0 -81
- package/src/example.ts +0 -25
- 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
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
{{#actions}}
|
|
4
|
-
import { {{{action}}} } from './{{{action}}}'
|
|
5
|
-
{{/actions}}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export default function getSchema(action : string){
|
|
9
|
-
switch(action){
|
|
10
|
-
{{#actions}}
|
|
11
|
-
case '{{{action}}}':
|
|
12
|
-
return {{{action}}};
|
|
13
|
-
{{/actions}}
|
|
14
|
-
default:
|
|
15
|
-
throw new Error('Action not found');
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
export interface ValidationConfig {
|
|
3
|
-
runAllValidations: boolean;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export type validationOutput = {
|
|
7
|
-
valid: boolean;
|
|
8
|
-
code: number;
|
|
9
|
-
description?: string;
|
|
10
|
-
}[];
|
|
11
|
-
|
|
12
|
-
/*
|
|
13
|
-
{% comment %} export type ExternalData = {
|
|
14
|
-
{{#externalData}}
|
|
15
|
-
{{name}}?: string[];
|
|
16
|
-
{{/externalData}}
|
|
17
|
-
}; {% endcomment %}
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
export type ExternalData = {}
|
|
21
|
-
|
|
22
|
-
export type validationInput = {
|
|
23
|
-
payload: any;
|
|
24
|
-
externalData: ExternalData;
|
|
25
|
-
config: ValidationConfig
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export type testFunctionArray = Array<(input: validationInput) => validationOutput>;
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
function {{name}}(input: validationInput): validationOutput {
|
|
2
|
-
const scope = payloadUtils.getJsonPath(input.payload,"{{{scopePath}}}");
|
|
3
|
-
let subResults: validationOutput = [];
|
|
4
|
-
let valid = true;
|
|
5
|
-
for(const testObj of scope){
|
|
6
|
-
testObj._EXTERNAL = input.externalData;
|
|
7
|
-
{{#variables}}
|
|
8
|
-
const {{name}} = {{{value}}}
|
|
9
|
-
{{/variables}}
|
|
10
|
-
|
|
11
|
-
{{#hasContinue}}
|
|
12
|
-
const skipCheck = {{{skipCheckStatement}}};
|
|
13
|
-
if(skipCheck) continue;
|
|
14
|
-
{{/hasContinue}}
|
|
15
|
-
|
|
16
|
-
{{{validationCode}}}
|
|
17
|
-
delete testObj._EXTERNAL;
|
|
18
|
-
}
|
|
19
|
-
return [{valid: valid,code: {{successCode}} },...subResults];
|
|
20
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
{{#isNested}}
|
|
3
|
-
|
|
4
|
-
{{{nestedFunctions}}}
|
|
5
|
-
|
|
6
|
-
const testFunctions: testFunctionArray = [
|
|
7
|
-
{{#names}}
|
|
8
|
-
{{name}},
|
|
9
|
-
{{/names}}
|
|
10
|
-
];
|
|
11
|
-
|
|
12
|
-
let invalidResults: validationOutput = [];
|
|
13
|
-
for (const fn of testFunctions) {
|
|
14
|
-
const subResult = fn(input);
|
|
15
|
-
// .filter(r => !r.valid);
|
|
16
|
-
invalidResults = [...invalidResults, ...subResult];
|
|
17
|
-
if(!input.config.runAllValidations && invalidResults.length > 0) {
|
|
18
|
-
return invalidResults;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
if(invalidResults.length > 0) {
|
|
22
|
-
// return invalidResults;
|
|
23
|
-
subResults = invalidResults;
|
|
24
|
-
valid = subResults.every(r => r.valid);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
{{/isNested}}
|
|
28
|
-
|
|
29
|
-
{{^isNested}}
|
|
30
|
-
const validate = {{{returnStatement}}}
|
|
31
|
-
|
|
32
|
-
if(!validate){
|
|
33
|
-
return [{
|
|
34
|
-
valid: false,
|
|
35
|
-
code: {{errorCode}},
|
|
36
|
-
description: `{{{errorDescription}}}`
|
|
37
|
-
}]
|
|
38
|
-
}
|
|
39
|
-
{{/isNested}}
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
function areUnique(operand: string[]) {
|
|
2
|
-
const valuesSet = new Set(operand);
|
|
3
|
-
return valuesSet.size === operand.length;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
function arePresent(operand: string[]) {
|
|
7
|
-
|
|
8
|
-
return noneIn(operand, ["", "null", "undefined"]) && operand.length > 0;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function allIn(left: string[], right: string[]) {
|
|
12
|
-
return left.every((v) => right.includes(v));
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function anyIn(left: string[], right: string[]) {
|
|
16
|
-
return left.some((v) => right.includes(v));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function noneIn(left: string[], right: string[]) {
|
|
20
|
-
return left.every((v) => !right.includes(v));
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function equalTo(left: string[], right: string[]) {
|
|
24
|
-
if (left.length !== right.length) return false;
|
|
25
|
-
return left.every((v, i) => v === right[i]);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function greaterThan(left: string[], right: string[]): boolean {
|
|
29
|
-
// Check if all elements in an array are ISO 8601 dates
|
|
30
|
-
const areAllISO = (arr: string[]) => arr.every((v) => isISO8601(v));
|
|
31
|
-
// Check if all elements in an array are numbers
|
|
32
|
-
const areAllNumbers = (arr: string[]) =>
|
|
33
|
-
arr.every((v) => !isNaN(Number(v)));
|
|
34
|
-
|
|
35
|
-
if (areAllISO(left) && areAllISO(right)) {
|
|
36
|
-
// Compare ISO 8601 dates
|
|
37
|
-
const leftDates = left.map((date) => new Date(date).getTime());
|
|
38
|
-
const rightDates = right.map((date) => new Date(date).getTime());
|
|
39
|
-
return leftDates.every(
|
|
40
|
-
(ld, index) => ld > rightDates[index] || index >= rightDates.length,
|
|
41
|
-
);
|
|
42
|
-
} else if (areAllNumbers(left) && areAllNumbers(right)) {
|
|
43
|
-
// Compare numbers
|
|
44
|
-
const leftNumbers = left.map(Number);
|
|
45
|
-
const rightNumbers = right.map(Number);
|
|
46
|
-
return leftNumbers.every(
|
|
47
|
-
(ln, index) =>
|
|
48
|
-
ln > rightNumbers[index] || index >= rightNumbers.length,
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// If neither condition is met, return false
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function lessThan(left: string[], right: string[]): boolean {
|
|
57
|
-
// Check if all elements in an array are ISO 8601 dates
|
|
58
|
-
const areAllISO = (arr: string[]) => arr.every((v) => isISO8601(v));
|
|
59
|
-
// Check if all elements in an array are numbers
|
|
60
|
-
const areAllNumbers = (arr: string[]) =>
|
|
61
|
-
arr.every((v) => !isNaN(Number(v)));
|
|
62
|
-
|
|
63
|
-
if (areAllISO(left) && areAllISO(right)) {
|
|
64
|
-
// Compare ISO 8601 dates
|
|
65
|
-
const leftDates = left.map((date) => new Date(date).getTime());
|
|
66
|
-
const rightDates = right.map((date) => new Date(date).getTime());
|
|
67
|
-
return leftDates.every(
|
|
68
|
-
(ld, index) => ld < rightDates[index] || index >= rightDates.length,
|
|
69
|
-
);
|
|
70
|
-
} else if (areAllNumbers(left) && areAllNumbers(right)) {
|
|
71
|
-
// Compare numbers
|
|
72
|
-
const leftNumbers = left.map(Number);
|
|
73
|
-
const rightNumbers = right.map(Number);
|
|
74
|
-
return leftNumbers.every(
|
|
75
|
-
(ln, index) =>
|
|
76
|
-
ln < rightNumbers[index] || index >= rightNumbers.length,
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// If neither condition is met, return false
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function followRegex(left: string[], regexArray: string[]) {
|
|
85
|
-
for (const regex of regexArray) {
|
|
86
|
-
const re = new RegExp(regex);
|
|
87
|
-
if (left.some((v) => !re.test(v))) return false;
|
|
88
|
-
}
|
|
89
|
-
return true;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function isISO8601(str: string): boolean {
|
|
93
|
-
// Regular expression to match ISO 8601 format
|
|
94
|
-
const iso8601Regex =
|
|
95
|
-
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;
|
|
96
|
-
|
|
97
|
-
// Check if the string matches the ISO 8601 format
|
|
98
|
-
if (!iso8601Regex.test(str)) {
|
|
99
|
-
return false;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// Check if the string is a valid date
|
|
103
|
-
const date = new Date(str);
|
|
104
|
-
return !isNaN(date.getTime());
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export default {
|
|
108
|
-
areUnique,
|
|
109
|
-
arePresent,
|
|
110
|
-
allIn,
|
|
111
|
-
anyIn,
|
|
112
|
-
noneIn,
|
|
113
|
-
equalTo,
|
|
114
|
-
followRegex,
|
|
115
|
-
greaterThan,
|
|
116
|
-
lessThan,
|
|
117
|
-
};
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AstNode,
|
|
3
|
-
BinaryOperatorNode,
|
|
4
|
-
CustomBinaryFunction,
|
|
5
|
-
CustomUniaryFunction,
|
|
6
|
-
NotOperatorNode,
|
|
7
|
-
ReturnStatementNode,
|
|
8
|
-
} from "../../../services/return-complier/ast.js";
|
|
9
|
-
import { buildAstFromInput } from "../../../services/return-complier/combined.js";
|
|
10
|
-
import { parseReturnInput } from "../../../services/return-complier/parser.js";
|
|
11
|
-
import {
|
|
12
|
-
AllIn,
|
|
13
|
-
AnyIn,
|
|
14
|
-
AreUnique,
|
|
15
|
-
EqualTo,
|
|
16
|
-
FollowRegex,
|
|
17
|
-
GreaterThan,
|
|
18
|
-
LessThan,
|
|
19
|
-
NoneIn,
|
|
20
|
-
ArePresent,
|
|
21
|
-
} from "../../../services/return-complier/tokens.js";
|
|
22
|
-
|
|
23
|
-
const uniaryFunction = {
|
|
24
|
-
[AreUnique.LABEL ?? "are unique"]: "areUnique",
|
|
25
|
-
[ArePresent.LABEL ?? "are present"]: "arePresent",
|
|
26
|
-
};
|
|
27
|
-
const binaryFunction = {
|
|
28
|
-
[AllIn.LABEL ?? "all in"]: "allIn",
|
|
29
|
-
[AnyIn.LABEL ?? "any in"]: "anyIn",
|
|
30
|
-
[FollowRegex.LABEL ?? "follow regex"]: "followRegex",
|
|
31
|
-
[NoneIn.LABEL ?? "none in"]: "noneIn",
|
|
32
|
-
[EqualTo.LABEL ?? "equal to"]: "equalTo",
|
|
33
|
-
[GreaterThan.LABEL ?? "greater than"]: "greaterThan",
|
|
34
|
-
[LessThan.LABEL ?? "less than"]: "lessThan",
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
function compileToTs(node: AstNode): string {
|
|
38
|
-
if (node.type === "returnStatement") {
|
|
39
|
-
const returnNode = node as ReturnStatementNode;
|
|
40
|
-
return compileToTs(returnNode.expression);
|
|
41
|
-
}
|
|
42
|
-
if (node.type === "binaryOperator") {
|
|
43
|
-
const binaryNode = node as BinaryOperatorNode;
|
|
44
|
-
const lhs = compileToTs(binaryNode.lhs);
|
|
45
|
-
const rhs = compileToTs(binaryNode.rhs);
|
|
46
|
-
return `(${lhs}) ${binaryNode.operator} (${rhs})`;
|
|
47
|
-
}
|
|
48
|
-
if (node.type === "notOperator") {
|
|
49
|
-
const notNode = node as NotOperatorNode;
|
|
50
|
-
const expression = compileToTs(notNode.expression);
|
|
51
|
-
return `!(${expression})`;
|
|
52
|
-
}
|
|
53
|
-
if (node.type === "customUniaryFunction") {
|
|
54
|
-
const unary = node as CustomUniaryFunction;
|
|
55
|
-
const func = uniaryFunction[unary.customFunction];
|
|
56
|
-
const varName = unary.expression.name;
|
|
57
|
-
return `validations.${func}(${varName})`;
|
|
58
|
-
}
|
|
59
|
-
if (node.type === "customBinaryFunction") {
|
|
60
|
-
const binary = node as CustomBinaryFunction;
|
|
61
|
-
const func = binaryFunction[binary.customFunction];
|
|
62
|
-
const lhs = binary.lhs.name;
|
|
63
|
-
const rhs = binary.rhs.name;
|
|
64
|
-
return `validations.${func}(${lhs}, ${rhs})`;
|
|
65
|
-
}
|
|
66
|
-
throw new Error("Unknown node type");
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export const compileInputToTs = (input: string) => {
|
|
70
|
-
const ast = buildAstFromInput(input);
|
|
71
|
-
return compileToTs(ast);
|
|
72
|
-
};
|
|
@@ -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
|
-
}
|