Added an $isNull EventFilter condition matcher

This commit is contained in:
Eric Allam
2023-08-09 12:35:44 +01:00
parent a1bc15d18b
commit fa3a22eb78
4 changed files with 106 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/core": patch
---
Added an $isNull EventFilter condition matcher
+8
View File
@@ -182,5 +182,13 @@ function contentFilterMatches(actualValue: any, contentFilter: ContentFilters[nu
);
}
if ("$isNull" in contentFilter) {
if (contentFilter.$isNull) {
return actualValue === null;
}
return actualValue !== null;
}
return true;
}
+3
View File
@@ -18,6 +18,9 @@ const EventMatcherSchema = z.union([
z.object({
$exists: z.boolean(),
}),
z.object({
$isNull: z.boolean(),
}),
z.object({
$anythingBut: z.union([z.string(), z.number(), z.boolean()]),
}),
@@ -259,6 +259,50 @@ describe("eventFilterMatches", () => {
expect(eventFilterMatches(payload, filter)).toBe(true);
});
it("should return true when payload matches an isNull condition", () => {
const payload = {
name: "John",
age: 30,
score: 100,
isAdmin: false,
confirmedAt: null,
hobbies: ["reading", "swimming"],
address: {
street: "123 Main St",
city: "Anytown",
state: "CA",
zip: "12345",
},
};
const filter: EventFilter = {
confirmedAt: [{ $isNull: true }],
};
expect(eventFilterMatches(payload, filter)).toBe(true);
});
it("should return true when payload matches an isNull condition (flipped)", () => {
const payload = {
name: "John",
age: 30,
score: 100,
isAdmin: false,
confirmedAt: "2020-01-01T00:00:00.000Z",
hobbies: ["reading", "swimming"],
address: {
street: "123 Main St",
city: "Anytown",
state: "CA",
zip: "12345",
},
};
const filter: EventFilter = {
confirmedAt: [{ $isNull: false }],
};
expect(eventFilterMatches(payload, filter)).toBe(true);
});
it("should return false when payload does not match string filter", () => {
const payload = {
name: "Jane",
@@ -519,4 +563,50 @@ describe("eventFilterMatches", () => {
};
expect(eventFilterMatches(payload, filter)).toBe(false);
});
it("should return false when the payload does not match an isNull condition", () => {
const payload = {
name: "Jane",
age: 25,
score: 100,
isAdmin: true,
hobbies: ["running", "yoga"],
confirmedAt: "2020-01-01T00:00:00.000Z",
address: {
street: "456 Elm St",
city: "San Francisco",
state: "CA",
zip: "67890",
latitude: 37.7749,
longitude: 122.4194,
},
};
const filter: EventFilter = {
confirmedAt: [{ $isNull: true }],
};
expect(eventFilterMatches(payload, filter)).toBe(false);
});
it("should return false when the payload does not match an isNull condition (flipped)", () => {
const payload = {
name: "Jane",
age: 25,
score: 100,
isAdmin: true,
hobbies: ["running", "yoga"],
confirmedAt: null,
address: {
street: "456 Elm St",
city: "San Francisco",
state: "CA",
zip: "67890",
latitude: 37.7749,
longitude: 122.4194,
},
};
const filter: EventFilter = {
confirmedAt: [{ $isNull: false }],
};
expect(eventFilterMatches(payload, filter)).toBe(false);
});
});