|
|
@ -651,20 +651,18 @@ export function intHash(str : string, length = 10) : number {
|
|
|
|
* @param jsonPath The JSON path or custom JSON query expression.
|
|
|
|
* @param jsonPath The JSON path or custom JSON query expression.
|
|
|
|
* @param jsonPathOperator The operator to use for comparison.
|
|
|
|
* @param jsonPathOperator The operator to use for comparison.
|
|
|
|
* @param expectedValue The expected value to compare against.
|
|
|
|
* @param expectedValue The expected value to compare against.
|
|
|
|
* @returns The result of the evaluation.
|
|
|
|
* @returns An object containing the status and the evaluation result.
|
|
|
|
* @throws Error if the evaluation returns undefined.
|
|
|
|
* @throws Error if the evaluation returns undefined.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export async function evaluateJsonQuery(data: any, jsonPath: string, jsonPathOperator: string, expectedValue: any): Promise<boolean> {
|
|
|
|
export async function evaluateJsonQuery(data: any, jsonPath: string, jsonPathOperator: string, expectedValue: any): Promise<{ status: boolean; evaluation: any }> {
|
|
|
|
|
|
|
|
|
|
|
|
// Check if inputs are numeric. If not, re-parse as strings. This ensures comparisons are handled correctly.
|
|
|
|
// Check if inputs are numeric. If not, re-parse as strings. This ensures comparisons are handled correctly.
|
|
|
|
const expected = isNaN(expectedValue) ? expectedValue.toString() : parseFloat(expectedValue);
|
|
|
|
const expected = isNaN(expectedValue) ? expectedValue.toString() : parseFloat(expectedValue);
|
|
|
|
let response = isNaN(data) ? data.toString() : parseFloat(data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let response: any;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// Attempt to parse data as JSON
|
|
|
|
response = JSON.parse(data);
|
|
|
|
response = JSON.parse(response);
|
|
|
|
} catch {
|
|
|
|
} catch (_) {
|
|
|
|
response = typeof data === "number" || typeof data === "object" ? data : data.toString();
|
|
|
|
// Failed to parse as JSON, continue with the original value
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let jsonQueryExpression;
|
|
|
|
let jsonQueryExpression;
|
|
|
@ -690,18 +688,27 @@ export async function evaluateJsonQuery(data: any, jsonPath: string, jsonPathOpe
|
|
|
|
|
|
|
|
|
|
|
|
// Evaluate the JSON Query Expression
|
|
|
|
// Evaluate the JSON Query Expression
|
|
|
|
const expression = jsonata(jsonQueryExpression);
|
|
|
|
const expression = jsonata(jsonQueryExpression);
|
|
|
|
const evaluation = await expression.evaluate({
|
|
|
|
|
|
|
|
|
|
|
|
let evaluation;
|
|
|
|
|
|
|
|
if (jsonPathOperator === "custom") {
|
|
|
|
|
|
|
|
evaluation = await expression.evaluate(response);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
evaluation = await expression.evaluate({
|
|
|
|
value: response,
|
|
|
|
value: response,
|
|
|
|
control: expected
|
|
|
|
control: expectedValue
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (evaluation === undefined) {
|
|
|
|
if (evaluation === undefined) {
|
|
|
|
throw new Error("Query evaluation returned undefined. Check your query syntax and the structure of the response data.");
|
|
|
|
throw new Error("Query evaluation returned undefined. Check your query syntax and the structure of the response data.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const result = (jsonPathOperator === "custom")
|
|
|
|
const status = (jsonPathOperator === "custom")
|
|
|
|
? evaluation.toString() === expected.toString()
|
|
|
|
? evaluation.toString() === expected.toString()
|
|
|
|
: evaluation;
|
|
|
|
: evaluation;
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
return {
|
|
|
|
|
|
|
|
status,
|
|
|
|
|
|
|
|
evaluation
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|