ondc-code-generator 0.2.9 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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
- generateCode: (valConfig: ValidationConfig, codeName?: string) => Promise<void>;
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.generateCode = async (valConfig, codeName = "L1-Validations") => {
49
+ this.withMinimalValidations = async (valConfig) => {
50
+ try {
51
+ await new ConfigValidator("", valConfig, {}, [], {
52
+ skipJsonPathTest: 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
- await this.performValidations(valConfig);
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
- function isListOfStringsOrNull(variable: any): boolean {
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
  };
@@ -14,4 +14,5 @@ export interface TestsValidatorDependencies {
14
14
  stringJsonPaths: string[];
15
15
  errorDefinitions: ErrorDefinition[];
16
16
  externalVariables: string[];
17
+ skipJsonPathTest?: boolean;
17
18
  }
@@ -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
- constructor(validationPath: string, config: ValidationConfig, stringJsonPaths: Record<string, string[]>, errorDefinitions: ErrorDefinition[]);
9
+ validatorSettings: {
10
+ skipJsonPathTest: boolean;
11
+ };
12
+ constructor(validationPath: string, config: ValidationConfig, stringJsonPaths: Record<string, string[]>, errorDefinitions: ErrorDefinition[], settings?: {
13
+ skipJsonPathTest: 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`);
@@ -28,5 +28,13 @@ export class ConfigValidator {
28
28
  this.config = config;
29
29
  this.stringJsonPaths = stringJsonPaths;
30
30
  this.errorDefinitions = errorDefinitions;
31
+ if (settings) {
32
+ this.validatorSettings = settings;
33
+ }
34
+ else {
35
+ this.validatorSettings = {
36
+ skipJsonPathTest: false,
37
+ };
38
+ }
31
39
  }
32
40
  }
@@ -20,7 +20,8 @@ export declare class ErrorCodeValidator extends TestObjectValidator {
20
20
  export declare class VariableValidator extends TestObjectValidator {
21
21
  possibleJsonPaths: string[];
22
22
  externalVariables: string[];
23
- constructor(testObject: TestObject, path: string, posibleJsonPaths: string[], externalVariables: string[]);
23
+ skipJsonPathTest: boolean;
24
+ constructor(testObject: TestObject, path: string, posibleJsonPaths: string[], externalVariables: string[], skipJsonPathTest?: boolean);
24
25
  validate: () => Promise<void>;
25
26
  validateKey(key: string): void;
26
27
  validateExternalData(path: string, definedExternalValues: string[]): void;
@@ -89,8 +89,9 @@ export class ErrorCodeValidator extends TestObjectValidator {
89
89
  }
90
90
  }
91
91
  export class VariableValidator extends TestObjectValidator {
92
- constructor(testObject, path, posibleJsonPaths, externalVariables) {
92
+ constructor(testObject, path, posibleJsonPaths, externalVariables, skipJsonPathTest = false) {
93
93
  super(testObject, path);
94
+ this.skipJsonPathTest = false;
94
95
  this.validate = async () => {
95
96
  for (const key in this.targetObject) {
96
97
  if (Object.values(TestObjectSyntax).includes(key)) {
@@ -119,7 +120,8 @@ export class VariableValidator extends TestObjectValidator {
119
120
  path = `${scope}.${pathWithoutDollar}`;
120
121
  }
121
122
  const replaced = replaceBracketsWithAsteriskNested(path);
122
- if (!this.possibleJsonPaths.includes(replaced)) {
123
+ if (!this.possibleJsonPaths.includes(replaced) &&
124
+ !this.skipJsonPathTest) {
123
125
  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
126
  }
125
127
  }
@@ -127,6 +129,7 @@ export class VariableValidator extends TestObjectValidator {
127
129
  };
128
130
  this.externalVariables = externalVariables;
129
131
  this.possibleJsonPaths = posibleJsonPaths;
132
+ this.skipJsonPathTest = skipJsonPathTest;
130
133
  }
131
134
  validateKey(key) {
132
135
  if (nodeReservedKeywords.has(key)) {
@@ -13,7 +13,7 @@ export class CompleteTestObjectValidator extends TestObjectValidator {
13
13
  if (this.targetObject[TestObjectSyntax.ErrorCode]) {
14
14
  await new ErrorCodeValidator(this.targetObject, this.validationPath, this.dependencies.errorDefinitions).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.skipJsonPathTest).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
@@ -16,7 +16,7 @@ export { ConfigCompiler };
16
16
  // const buildYaml = readFileSync(buildPath, "utf-8");
17
17
  // const valConfig = JSON.parse(readFileSync(valConfigPath, "utf-8"));
18
18
  // await compiler.initialize(buildYaml);
19
- // await compiler.generateCode(valConfig, "L1-validations");
19
+ // await compiler.generateCode(valConfig, "L1-validations");
20
20
  // };
21
21
  // (async () => {
22
22
  // await main();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ondc-code-generator",
3
- "version": "0.2.9",
3
+ "version": "0.3.0",
4
4
  "description": "generate code from build.yaml ",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",