From 5741475e89b1d7540c778dbddbe9e926d488c474 Mon Sep 17 00:00:00 2001 From: tamjid14 Date: Sun, 24 May 2026 17:29:15 +0100 Subject: [PATCH] test: add nested where fail-closed coverage --- src/matches-where.test.ts | 2 ++ src/matches-where.ts | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/matches-where.test.ts b/src/matches-where.test.ts index 26a2be4a9..0718d1670 100644 --- a/src/matches-where.test.ts +++ b/src/matches-where.test.ts @@ -26,6 +26,8 @@ await test('matchesWhere', async (t) => { [{ or: [{ a: { lt: 0 } }, { b: { gt: 20 } }] }, false], [{ nested: { a: { eq: 10 } } }, true], [{ nested: { b: { lt: 20 } } }, false], + [{ missing: { nested: { eq: 'value' } } }, false], + [{ c: { nested: { eq: 'value' } } }, false], [{ a: { in: [10, 11] } }, true], [{ a: { in: [1, 2] } }, false], [{ c: { in: ['x', 'y'] } }, true], diff --git a/src/matches-where.ts b/src/matches-where.ts index 036b00c69..c0a50df0a 100644 --- a/src/matches-where.ts +++ b/src/matches-where.ts @@ -21,6 +21,14 @@ function getKnownOperators(value: unknown): WhereOperator[] { return ops } +function hasNestedKnownOperator(value: JsonObject): boolean { + return Object.values(value).some( + (subValue) => + isJSONObject(subValue) && + (getKnownOperators(subValue).length > 0 || hasNestedKnownOperator(subValue)), + ) +} + export function matchesWhere(obj: JsonObject, where: JsonObject): boolean { for (const [key, value] of Object.entries(where)) { if (key === 'or') { @@ -74,6 +82,8 @@ export function matchesWhere(obj: JsonObject, where: JsonObject): boolean { if (isJSONObject(field)) { if (!matchesWhere(field, value)) return false + } else if (hasNestedKnownOperator(value)) { + return false } continue