ondc-code-generator 0.2.9 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/generator/config-compiler.d.ts +2 -1
- package/dist/generator/config-compiler.js +18 -2
- package/dist/generator/generators/typescript/templates/json-path-utils.mustache +24 -6
- package/dist/generator/validators/abstract-validator.d.ts +1 -0
- package/dist/generator/validators/config-validator.d.ts +6 -1
- package/dist/generator/validators/config-validator.js +10 -1
- package/dist/generator/validators/tests-config/sub-validations.d.ts +6 -3
- package/dist/generator/validators/tests-config/sub-validations.js +15 -3
- package/dist/generator/validators/tests-config/test-validator.js +3 -3
- package/dist/index.js +18 -21
- package/package.json +1 -1
|
@@ -19,7 +19,8 @@ export declare class ConfigCompiler {
|
|
|
19
19
|
constructor(language: SupportedLanguages);
|
|
20
20
|
initialize: (buildYaml: string, generatorConfig?: Partial<CodeGeneratorConfig>) => Promise<void>;
|
|
21
21
|
performValidations: (valConfig: ValidationConfig) => Promise<void>;
|
|
22
|
-
|
|
22
|
+
withMinimalValidations: (valConfig: ValidationConfig) => Promise<void>;
|
|
23
|
+
generateCode: (valConfig: ValidationConfig, codeName?: string, minimal?: boolean) => Promise<void>;
|
|
23
24
|
generateL0Schema: () => Promise<void>;
|
|
24
25
|
generateValidPaths: () => Promise<Record<string, string[]>>;
|
|
25
26
|
}
|
|
@@ -46,13 +46,29 @@ export class ConfigCompiler {
|
|
|
46
46
|
throw new Error("Validation failed");
|
|
47
47
|
}
|
|
48
48
|
};
|
|
49
|
-
this.
|
|
49
|
+
this.withMinimalValidations = async (valConfig) => {
|
|
50
|
+
try {
|
|
51
|
+
await new ConfigValidator("", valConfig, {}, [], {
|
|
52
|
+
minimal: true,
|
|
53
|
+
}).validate();
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
logger.error(e);
|
|
57
|
+
throw new Error("validation failed");
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
this.generateCode = async (valConfig, codeName = "L1-Validations", minimal = false) => {
|
|
50
61
|
valConfig = JSON.parse(JSON.stringify(valConfig));
|
|
51
62
|
if (this.generatorConfig?.duplicateVariablesInChildren) {
|
|
52
63
|
console.log("Duplicating variables");
|
|
53
64
|
valConfig = duplicateVariablesInChildren(valConfig);
|
|
54
65
|
}
|
|
55
|
-
|
|
66
|
+
if (minimal) {
|
|
67
|
+
await this.withMinimalValidations(valConfig);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
await this.performValidations(valConfig);
|
|
71
|
+
}
|
|
56
72
|
// Generate code based on the language
|
|
57
73
|
switch (this.language) {
|
|
58
74
|
case SupportedLanguages.Typescript:
|
|
@@ -1,17 +1,35 @@
|
|
|
1
1
|
import jsonpath from "jsonpath";
|
|
2
|
+
|
|
3
|
+
function isPrimitive(val: any): boolean {
|
|
4
|
+
return val === null || ['string', 'number', 'boolean'].includes(typeof val);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function isListOfStringsOrNull(arr: any[]): boolean {
|
|
8
|
+
return Array.isArray(arr) && arr.every((item) => typeof item === 'string' || item === null);
|
|
9
|
+
}
|
|
10
|
+
|
|
2
11
|
function getJsonPath(payload: any, path: string) {
|
|
3
12
|
let output = jsonpath.query(payload, path);
|
|
13
|
+
|
|
14
|
+
if (!Array.isArray(output)) {
|
|
15
|
+
throw new Error(`Expected output to be an array, got ${typeof output}`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Check if all items are primitive
|
|
19
|
+
const allPrimitives = output.every(isPrimitive);
|
|
20
|
+
|
|
21
|
+
if (!allPrimitives) {
|
|
22
|
+
throw new Error(`Expected output to be a list of primitives, but found complex types.`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Handle nulls
|
|
4
26
|
if (isListOfStringsOrNull(output)) {
|
|
5
27
|
output = output.map((o) => (o === null ? "null" : o));
|
|
6
28
|
}
|
|
29
|
+
|
|
7
30
|
return output.length === 0 ? [] : output;
|
|
8
31
|
}
|
|
9
|
-
|
|
10
|
-
return (
|
|
11
|
-
Array.isArray(variable) &&
|
|
12
|
-
variable.every((item) => item === null || typeof item === "string")
|
|
13
|
-
);
|
|
14
|
-
}
|
|
32
|
+
|
|
15
33
|
export default {
|
|
16
34
|
getJsonPath,
|
|
17
35
|
};
|
|
@@ -6,6 +6,11 @@ export declare class ConfigValidator implements IValidator {
|
|
|
6
6
|
config: ValidationConfig;
|
|
7
7
|
stringJsonPaths: Record<string, string[]>;
|
|
8
8
|
errorDefinitions: ErrorDefinition[];
|
|
9
|
-
|
|
9
|
+
validatorSettings: {
|
|
10
|
+
minimal: boolean;
|
|
11
|
+
};
|
|
12
|
+
constructor(validationPath: string, config: ValidationConfig, stringJsonPaths: Record<string, string[]>, errorDefinitions: ErrorDefinition[], settings?: {
|
|
13
|
+
minimal: boolean;
|
|
14
|
+
});
|
|
10
15
|
validate: () => Promise<void>;
|
|
11
16
|
}
|
|
@@ -3,7 +3,7 @@ import { getExternalVariables } from "../../utils/general-utils/validation-utils
|
|
|
3
3
|
import { SessionDataValidator } from "./session-data-config/session-data-validator.js";
|
|
4
4
|
import { TestsValidator } from "./tests-config/test-list-validator.js";
|
|
5
5
|
export class ConfigValidator {
|
|
6
|
-
constructor(validationPath, config, stringJsonPaths, errorDefinitions) {
|
|
6
|
+
constructor(validationPath, config, stringJsonPaths, errorDefinitions, settings) {
|
|
7
7
|
this.validate = async () => {
|
|
8
8
|
if (!this.config[ConfigSyntax.Tests])
|
|
9
9
|
throw new Error(`Tests not found in config`);
|
|
@@ -20,6 +20,7 @@ export class ConfigValidator {
|
|
|
20
20
|
stringJsonPaths: this.stringJsonPaths[api],
|
|
21
21
|
errorDefinitions: this.errorDefinitions,
|
|
22
22
|
externalVariables: externalVariables,
|
|
23
|
+
minimal: this.validatorSettings.minimal,
|
|
23
24
|
};
|
|
24
25
|
await new TestsValidator(testList, path, dependencies).validate();
|
|
25
26
|
}
|
|
@@ -28,5 +29,13 @@ export class ConfigValidator {
|
|
|
28
29
|
this.config = config;
|
|
29
30
|
this.stringJsonPaths = stringJsonPaths;
|
|
30
31
|
this.errorDefinitions = errorDefinitions;
|
|
32
|
+
if (settings) {
|
|
33
|
+
this.validatorSettings = settings;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
this.validatorSettings = {
|
|
37
|
+
minimal: false,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
31
40
|
}
|
|
32
41
|
}
|
|
@@ -9,18 +9,21 @@ export declare class NameValidator extends TestObjectValidator {
|
|
|
9
9
|
}
|
|
10
10
|
export declare class ScopeValidator extends TestObjectValidator {
|
|
11
11
|
impossiblePaths: string[];
|
|
12
|
-
|
|
12
|
+
minimal: boolean;
|
|
13
|
+
constructor(testObject: TestObject, path: string, impossiblePaths: string[], minimal?: boolean);
|
|
13
14
|
validate: () => Promise<void>;
|
|
14
15
|
}
|
|
15
16
|
export declare class ErrorCodeValidator extends TestObjectValidator {
|
|
16
17
|
possibleErrorCodes: ErrorDefinition[];
|
|
17
|
-
|
|
18
|
+
minimal: boolean;
|
|
19
|
+
constructor(testObject: TestObject, path: string, possibleErrorCodes: ErrorDefinition[], minimal?: boolean);
|
|
18
20
|
validate: () => Promise<void>;
|
|
19
21
|
}
|
|
20
22
|
export declare class VariableValidator extends TestObjectValidator {
|
|
21
23
|
possibleJsonPaths: string[];
|
|
22
24
|
externalVariables: string[];
|
|
23
|
-
|
|
25
|
+
minimal: boolean;
|
|
26
|
+
constructor(testObject: TestObject, path: string, posibleJsonPaths: string[], externalVariables: string[], minimal?: boolean);
|
|
24
27
|
validate: () => Promise<void>;
|
|
25
28
|
validateKey(key: string): void;
|
|
26
29
|
validateExternalData(path: string, definedExternalValues: string[]): void;
|
|
@@ -41,8 +41,9 @@ export class NameValidator extends TestObjectValidator {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
export class ScopeValidator extends TestObjectValidator {
|
|
44
|
-
constructor(testObject, path, impossiblePaths) {
|
|
44
|
+
constructor(testObject, path, impossiblePaths, minimal = false) {
|
|
45
45
|
super(testObject, path);
|
|
46
|
+
this.minimal = false;
|
|
46
47
|
this.validate = async () => {
|
|
47
48
|
const path = this.targetObject[TestObjectSyntax.Scope];
|
|
48
49
|
if (typeof path !== "string") {
|
|
@@ -54,16 +55,20 @@ export class ScopeValidator extends TestObjectValidator {
|
|
|
54
55
|
if (!path.startsWith(`$.`)) {
|
|
55
56
|
throw new Error(`${TestObjectSyntax.Scope} json path should start with $. at ${this.validationPath}`);
|
|
56
57
|
}
|
|
58
|
+
if (this.minimal)
|
|
59
|
+
return;
|
|
57
60
|
if (this.impossiblePaths.includes(replaceBracketsWithAsteriskNested(path))) {
|
|
58
61
|
throw new Error(`${TestObjectSyntax.Scope} can't be a path that returns a array of string it must be a json path which returns a array of objects at path ${this.validationPath}`);
|
|
59
62
|
}
|
|
60
63
|
};
|
|
61
64
|
this.impossiblePaths = impossiblePaths;
|
|
65
|
+
this.minimal = minimal;
|
|
62
66
|
}
|
|
63
67
|
}
|
|
64
68
|
export class ErrorCodeValidator extends TestObjectValidator {
|
|
65
|
-
constructor(testObject, path, possibleErrorCodes) {
|
|
69
|
+
constructor(testObject, path, possibleErrorCodes, minimal = false) {
|
|
66
70
|
super(testObject, path);
|
|
71
|
+
this.minimal = false;
|
|
67
72
|
this.validate = async () => {
|
|
68
73
|
if (!this.targetObject[TestObjectSyntax.ErrorCode]) {
|
|
69
74
|
return;
|
|
@@ -74,6 +79,8 @@ export class ErrorCodeValidator extends TestObjectValidator {
|
|
|
74
79
|
if (typeof this.targetObject[TestObjectSyntax.ErrorCode] !== "number") {
|
|
75
80
|
throw new Error(`${TestObjectSyntax.ErrorCode} should be a number at path ${this.validationPath}`);
|
|
76
81
|
}
|
|
82
|
+
if (this.minimal)
|
|
83
|
+
return;
|
|
77
84
|
const errorCode = this.targetObject[TestObjectSyntax.ErrorCode];
|
|
78
85
|
if (!this.possibleErrorCodes.some((code) => code.code === errorCode)) {
|
|
79
86
|
throw new Error(`${TestObjectSyntax.ErrorCode} don't exist in error codes at path ${this.validationPath}`);
|
|
@@ -86,11 +93,13 @@ export class ErrorCodeValidator extends TestObjectValidator {
|
|
|
86
93
|
}
|
|
87
94
|
};
|
|
88
95
|
this.possibleErrorCodes = possibleErrorCodes;
|
|
96
|
+
this.minimal = minimal;
|
|
89
97
|
}
|
|
90
98
|
}
|
|
91
99
|
export class VariableValidator extends TestObjectValidator {
|
|
92
|
-
constructor(testObject, path, posibleJsonPaths, externalVariables) {
|
|
100
|
+
constructor(testObject, path, posibleJsonPaths, externalVariables, minimal = false) {
|
|
93
101
|
super(testObject, path);
|
|
102
|
+
this.minimal = false;
|
|
94
103
|
this.validate = async () => {
|
|
95
104
|
for (const key in this.targetObject) {
|
|
96
105
|
if (Object.values(TestObjectSyntax).includes(key)) {
|
|
@@ -119,6 +128,8 @@ export class VariableValidator extends TestObjectValidator {
|
|
|
119
128
|
path = `${scope}.${pathWithoutDollar}`;
|
|
120
129
|
}
|
|
121
130
|
const replaced = replaceBracketsWithAsteriskNested(path);
|
|
131
|
+
if (this.minimal)
|
|
132
|
+
return;
|
|
122
133
|
if (!this.possibleJsonPaths.includes(replaced)) {
|
|
123
134
|
throw new Error(`Variable: ${key} should be a jsonPath that returns a array of strings or the path don't exist in the schema, at ${this.validationPath} found original ${path} replaces: ${replaced}`);
|
|
124
135
|
}
|
|
@@ -127,6 +138,7 @@ export class VariableValidator extends TestObjectValidator {
|
|
|
127
138
|
};
|
|
128
139
|
this.externalVariables = externalVariables;
|
|
129
140
|
this.possibleJsonPaths = posibleJsonPaths;
|
|
141
|
+
this.minimal = minimal;
|
|
130
142
|
}
|
|
131
143
|
validateKey(key) {
|
|
132
144
|
if (nodeReservedKeywords.has(key)) {
|
|
@@ -8,12 +8,12 @@ export class CompleteTestObjectValidator extends TestObjectValidator {
|
|
|
8
8
|
await new RequiredFieldsValidator(this.targetObject, this.validationPath).validate();
|
|
9
9
|
await new NameValidator(this.targetObject, this.validationPath).validate();
|
|
10
10
|
if (this.targetObject[TestObjectSyntax.Scope]) {
|
|
11
|
-
await new ScopeValidator(this.targetObject, this.validationPath, this.dependencies.stringJsonPaths).validate();
|
|
11
|
+
await new ScopeValidator(this.targetObject, this.validationPath, this.dependencies.stringJsonPaths, this.dependencies.minimal).validate();
|
|
12
12
|
}
|
|
13
13
|
if (this.targetObject[TestObjectSyntax.ErrorCode]) {
|
|
14
|
-
await new ErrorCodeValidator(this.targetObject, this.validationPath, this.dependencies.errorDefinitions).validate();
|
|
14
|
+
await new ErrorCodeValidator(this.targetObject, this.validationPath, this.dependencies.errorDefinitions, this.dependencies.minimal).validate();
|
|
15
15
|
}
|
|
16
|
-
await new VariableValidator(this.targetObject, this.validationPath, this.dependencies.stringJsonPaths, this.dependencies.externalVariables).validate();
|
|
16
|
+
await new VariableValidator(this.targetObject, this.validationPath, this.dependencies.stringJsonPaths, this.dependencies.externalVariables, this.dependencies.minimal).validate();
|
|
17
17
|
if (this.targetObject[TestObjectSyntax.Continue]) {
|
|
18
18
|
await new ContinueValidator(this.targetObject, this.validationPath).validate();
|
|
19
19
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,23 +1,20 @@
|
|
|
1
1
|
import { ConfigCompiler } from "./generator/config-compiler.js";
|
|
2
2
|
export { ConfigCompiler };
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
// (async () => {
|
|
22
|
-
// await main();
|
|
23
|
-
// })();
|
|
3
|
+
import { readFileSync } from "fs";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { fileURLToPath } from "url";
|
|
6
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
7
|
+
const __dirname = path.dirname(__filename);
|
|
8
|
+
import { SupportedLanguages } from "./types/compiler-types.js";
|
|
9
|
+
const main = async () => {
|
|
10
|
+
const compiler = new ConfigCompiler(SupportedLanguages.Typescript);
|
|
11
|
+
const buildPath = path.resolve(__dirname, "../samples/build.yaml");
|
|
12
|
+
const valConfigPath = path.resolve(__dirname, "../samples/validation-config.json");
|
|
13
|
+
const buildYaml = readFileSync(buildPath, "utf-8");
|
|
14
|
+
const valConfig = JSON.parse(readFileSync(valConfigPath, "utf-8"));
|
|
15
|
+
await compiler.initialize(buildYaml);
|
|
16
|
+
await compiler.generateCode(valConfig, "L1-validations", true);
|
|
17
|
+
};
|
|
18
|
+
(async () => {
|
|
19
|
+
await main();
|
|
20
|
+
})();
|