Skip to content

Commit f953770

Browse files
fix(core): evaluate nested exists false filter when parent key is nullish (#5017)
Signed-off-by: Gaurav Patil <gauravpatil2516@gmail.com>
1 parent 598fdf2 commit f953770

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

‎packages/core/src/eventFilterMatches.ts‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@ import type { EventFilter } from "./schemas/eventFilter.js";
55
// If the values of the array are objects, then we are doing content filtering
66
// An example would be [{ $endsWith: ".png" }, { $startsWith: "images/" } ]
77
export function eventFilterMatches(payload: any, filter: EventFilter): boolean {
8-
if (payload === undefined || payload === null) {
9-
if (Object.entries(filter).length === 0) {
10-
return true;
11-
} else {
12-
return false;
13-
}
14-
}
8+
const safePayload = payload ?? {};
159

1610
for (const [patternKey, patternValue] of Object.entries(filter)) {
17-
const payloadValue = payload[patternKey];
11+
const payloadValue = safePayload[patternKey];
1812

1913
if (Array.isArray(patternValue)) {
2014
if (patternValue.length === 0) {

‎packages/core/test/eventFilterMatches.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,20 @@ describe("eventFilterMatches", () => {
209209
expect(eventFilterMatches(payload, filter)).toBe(true);
210210
});
211211

212+
it("should return true for nested $exists: false when parent key is missing or null", () => {
213+
const filter: EventFilter = {
214+
user: {
215+
email: [{ $exists: false }],
216+
},
217+
};
218+
219+
expect(eventFilterMatches({}, filter)).toBe(true);
220+
expect(eventFilterMatches({ user: null }, filter)).toBe(true);
221+
expect(eventFilterMatches({ user: {} }, filter)).toBe(true);
222+
expect(eventFilterMatches({ user: { email: undefined } }, filter)).toBe(true);
223+
expect(eventFilterMatches({ user: { email: "user@example.com" } }, filter)).toBe(false);
224+
});
225+
212226
it("should return true when payload does match numeric condition", () => {
213227
const payload = {
214228
name: "John",

0 commit comments

Comments
 (0)