Helpful error when query returns object or array

This commit is contained in:
Matt Visnovsky 2024-06-10 12:49:41 -06:00
parent 82352910bf
commit e2e81091c3
2 changed files with 7 additions and 0 deletions

View File

@ -407,6 +407,9 @@ async function evaluateJsonQuery(data, jsonPath, jsonPathOperator, expectedValue
} }
try { try {
response = (jsonPath) ? await jsonata(jsonPath).evaluate(response) : response; response = (jsonPath) ? await jsonata(jsonPath).evaluate(response) : response;
if (typeof response === "object" || Array.isArray(response) || response instanceof Date || typeof response === "function") {
throw new Error(`The post-JSON query evaluated response from the server is of type ${typeof response}, which cannot be directly compared to the expected value`);
}
let jsonQueryExpression; let jsonQueryExpression;
switch (jsonPathOperator) { switch (jsonPathOperator) {
case ">": case ">":

View File

@ -667,6 +667,10 @@ export async function evaluateJsonQuery(data: any, jsonPath: string, jsonPathOpe
// If a JSON path is provided, pre-evaluate the data using it. // If a JSON path is provided, pre-evaluate the data using it.
response = (jsonPath) ? await jsonata(jsonPath).evaluate(response) : response; response = (jsonPath) ? await jsonata(jsonPath).evaluate(response) : response;
if (typeof response === "object" || Array.isArray(response) || response instanceof Date || typeof response === "function") {
throw new Error(`The post-JSON query evaluated response from the server is of type ${typeof response}, which cannot be directly compared to the expected value`);
}
// Perform the comparison logic using the chosen operator // Perform the comparison logic using the chosen operator
let jsonQueryExpression; let jsonQueryExpression;
switch (jsonPathOperator) { switch (jsonPathOperator) {