feat: Add $not to eventFilters (#815)

* feat: Add  to eventFilters

* not filter test case added for false evaluation

* Added a patch changeset for @trigger.dev/core

---------

Co-authored-by: Matt Aitken <matt@mattaitken.com>
This commit is contained in:
Yash Khokhaneshiya
2024-01-02 21:13:02 +05:30
committed by GitHub
parent 26d223ab36
commit 740b7b2385
4 changed files with 66 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/core": patch
---
feat: Add $not to eventFilters
+10
View File
@@ -198,5 +198,15 @@ function contentFilterMatches(actualValue: any, contentFilter: ContentFilters[nu
return actualValue !== null;
}
if ("$not" in contentFilter) {
if (Array.isArray(actualValue)) {
return !actualValue.includes(contentFilter.$not);
} else if (typeof actualValue === 'number' || typeof actualValue === 'boolean' || typeof actualValue === 'string') {
return actualValue !== contentFilter.$not;
}
return false;
}
return true;
}
+3
View File
@@ -52,6 +52,9 @@ const EventMatcherSchema = z.union([
z.object({
$includes: z.union([z.string(), z.number(), z.boolean()]),
}),
z.object({
$not: z.union([z.string(), z.number(), z.boolean()])
})
])
),
]);
@@ -252,6 +252,54 @@ describe("eventFilterMatches", () => {
expect(eventFilterMatches(payload, filter)).toBe(true);
});
it("should return true when payload matches an not condition", () => {
const payload = {
name: "John",
age: 30,
score: 100,
isAdmin: false,
hobbies: ["reading", "swimming"],
address: {
street: "123 Main St",
city: "Anytown",
state: "CA",
zip: "12345",
},
};
const filter: EventFilter = {
hobbies: [{ $not: "gaming" }],
age: [{ $not: 39 }],
isAdmin: [{ $not: true }],
name: [{ $not: 'Test' }]
};
expect(eventFilterMatches(payload, filter)).toBe(true);
});
it("should return false when payload not matches an not condition", () => {
const payload = {
name: "John",
age: 30,
score: 100,
isAdmin: false,
hobbies: ["reading", "swimming"],
address: {
street: "123 Main St",
city: "Anytown",
state: "CA",
zip: "12345",
},
};
const filter: EventFilter = {
hobbies: [{ $not: "reading" }],
age: [{ $not: 30 }],
isAdmin: [{ $not: false }],
name: [{ $not: 'John' }]
};
expect(eventFilterMatches(payload, filter)).toBe(false);
});
it("should return true when payload matches an ignoreCaseEquals condition", () => {
const payload = {
name: "John",