From e2e81091c34ebb3c376acd536e7d74f87fdfe507 Mon Sep 17 00:00:00 2001
From: Matt Visnovsky <matt@visnovsky.us>
Date: Mon, 10 Jun 2024 12:49:41 -0600
Subject: [PATCH] Helpful error when query returns object or array

---
 src/util.js | 3 +++
 src/util.ts | 4 ++++
 2 files changed, 7 insertions(+)

diff --git a/src/util.js b/src/util.js
index fb77cc95..c0c6a6d2 100644
--- a/src/util.js
+++ b/src/util.js
@@ -407,6 +407,9 @@ async function evaluateJsonQuery(data, jsonPath, jsonPathOperator, expectedValue
     }
     try {
         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;
         switch (jsonPathOperator) {
             case ">":
diff --git a/src/util.ts b/src/util.ts
index 8c67090b..24a03f58 100644
--- a/src/util.ts
+++ b/src/util.ts
@@ -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.
         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
         let jsonQueryExpression;
         switch (jsonPathOperator) {