@ondc/automation-mock-runner 1.3.49 → 1.3.50
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.
|
@@ -15,9 +15,12 @@ export declare class CodeValidator {
|
|
|
15
15
|
* Validate that return statements match expected structure
|
|
16
16
|
*/
|
|
17
17
|
/**
|
|
18
|
-
* Collect ReturnStatement arguments belonging only to the
|
|
19
|
-
*
|
|
20
|
-
*
|
|
18
|
+
* Collect ReturnStatement arguments belonging only to the target function.
|
|
19
|
+
* When `targetFnName` is given and a top-level `function <name>(...)` exists,
|
|
20
|
+
* we walk only that body — sibling helpers like `function x(){return "hi"}`
|
|
21
|
+
* declared next to `validate` are ignored. Returns inside nested
|
|
22
|
+
* functions/arrows are also skipped (they aren't the contract).
|
|
23
|
+
* Falls back to the whole AST when no named match is found.
|
|
21
24
|
*/
|
|
22
25
|
private static collectTopLevelReturns;
|
|
23
26
|
private static validateReturnStructure;
|
|
@@ -139,7 +139,7 @@ class CodeValidator {
|
|
|
139
139
|
});
|
|
140
140
|
// 5. Validate return type structure (for validate and meetsRequirements)
|
|
141
141
|
if (schema.returnType.properties) {
|
|
142
|
-
const returnValidation = this.validateReturnStructure(ast, schema.returnType.properties);
|
|
142
|
+
const returnValidation = this.validateReturnStructure(ast, schema.returnType.properties, schema.name);
|
|
143
143
|
errors.push(...returnValidation);
|
|
144
144
|
}
|
|
145
145
|
// 6. Check for best practices
|
|
@@ -172,24 +172,27 @@ class CodeValidator {
|
|
|
172
172
|
* Validate that return statements match expected structure
|
|
173
173
|
*/
|
|
174
174
|
/**
|
|
175
|
-
* Collect ReturnStatement arguments belonging only to the
|
|
176
|
-
*
|
|
177
|
-
*
|
|
175
|
+
* Collect ReturnStatement arguments belonging only to the target function.
|
|
176
|
+
* When `targetFnName` is given and a top-level `function <name>(...)` exists,
|
|
177
|
+
* we walk only that body — sibling helpers like `function x(){return "hi"}`
|
|
178
|
+
* declared next to `validate` are ignored. Returns inside nested
|
|
179
|
+
* functions/arrows are also skipped (they aren't the contract).
|
|
180
|
+
* Falls back to the whole AST when no named match is found.
|
|
178
181
|
*/
|
|
179
|
-
static collectTopLevelReturns(ast) {
|
|
182
|
+
static collectTopLevelReturns(ast, targetFnName) {
|
|
180
183
|
const returns = [];
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
};
|
|
189
|
-
walk.recursive(
|
|
190
|
-
FunctionDeclaration:
|
|
191
|
-
FunctionExpression:
|
|
192
|
-
ArrowFunctionExpression:
|
|
184
|
+
const programBody = Array.isArray(ast.body)
|
|
185
|
+
? ast.body
|
|
186
|
+
: [];
|
|
187
|
+
const targetFn = targetFnName
|
|
188
|
+
? programBody.find((n) => n.type === "FunctionDeclaration" && n.id?.name === targetFnName)
|
|
189
|
+
: undefined;
|
|
190
|
+
const walkRoot = targetFn ? targetFn.body : ast;
|
|
191
|
+
const skip = () => { };
|
|
192
|
+
walk.recursive(walkRoot, null, {
|
|
193
|
+
FunctionDeclaration: skip,
|
|
194
|
+
FunctionExpression: skip,
|
|
195
|
+
ArrowFunctionExpression: skip,
|
|
193
196
|
ReturnStatement(node) {
|
|
194
197
|
if (node.argument)
|
|
195
198
|
returns.push(node.argument);
|
|
@@ -197,9 +200,9 @@ class CodeValidator {
|
|
|
197
200
|
});
|
|
198
201
|
return returns;
|
|
199
202
|
}
|
|
200
|
-
static validateReturnStructure(ast, expectedProperties) {
|
|
203
|
+
static validateReturnStructure(ast, expectedProperties, targetFnName) {
|
|
201
204
|
const warnings = [];
|
|
202
|
-
const foundReturns = this.collectTopLevelReturns(ast);
|
|
205
|
+
const foundReturns = this.collectTopLevelReturns(ast, targetFnName);
|
|
203
206
|
// Check if we have return statements
|
|
204
207
|
if (foundReturns.length === 0) {
|
|
205
208
|
warnings.push(`Function should return an object with properties: ${Object.keys(expectedProperties).join(", ")}`);
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const code_validator_1 = require("../lib/validators/code-validator");
|
|
4
4
|
const function_registry_1 = require("../lib/constants/function-registry");
|
|
5
5
|
const validateSchema = (0, function_registry_1.getFunctionSchema)("validate");
|
|
6
|
+
const meetsRequirementsSchema = (0, function_registry_1.getFunctionSchema)("meetsRequirements");
|
|
6
7
|
describe("CodeValidator.validate — return structure", () => {
|
|
7
8
|
it("accepts an outer return with the full expected shape", () => {
|
|
8
9
|
const code = `
|
|
@@ -63,6 +64,34 @@ describe("CodeValidator.validate — return structure", () => {
|
|
|
63
64
|
expect(result.errors).toEqual([]);
|
|
64
65
|
expect(result.isValid).toBe(true);
|
|
65
66
|
});
|
|
67
|
+
it("ignores sibling top-level helper function returns (validate)", () => {
|
|
68
|
+
const code = `
|
|
69
|
+
function x() {
|
|
70
|
+
return "hello";
|
|
71
|
+
}
|
|
72
|
+
function validate(targetPayload, sessionData) {
|
|
73
|
+
let some = x();
|
|
74
|
+
return { valid: true, code: 200, description: "Valid request" };
|
|
75
|
+
}
|
|
76
|
+
`;
|
|
77
|
+
const result = code_validator_1.CodeValidator.validate(code, validateSchema);
|
|
78
|
+
expect(result.errors).toEqual([]);
|
|
79
|
+
expect(result.isValid).toBe(true);
|
|
80
|
+
});
|
|
81
|
+
it("ignores sibling top-level helper function returns (meetsRequirements)", () => {
|
|
82
|
+
const code = `
|
|
83
|
+
function helper() {
|
|
84
|
+
return 123;
|
|
85
|
+
}
|
|
86
|
+
function meetsRequirements(sessionData) {
|
|
87
|
+
const n = helper();
|
|
88
|
+
return { valid: true, code: 200, description: "Requirements met" };
|
|
89
|
+
}
|
|
90
|
+
`;
|
|
91
|
+
const result = code_validator_1.CodeValidator.validate(code, meetsRequirementsSchema);
|
|
92
|
+
expect(result.errors).toEqual([]);
|
|
93
|
+
expect(result.isValid).toBe(true);
|
|
94
|
+
});
|
|
66
95
|
it("warns when only a nested helper returns and the outer function has no return", () => {
|
|
67
96
|
const code = `
|
|
68
97
|
function validate(targetPayload, sessionData) {
|