Skip to content

Commit

Permalink
feat: Add $not to eventFilters (#815)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
meetwithyash and matt-aitken authored Jan 2, 2024
1 parent 26d223a commit 740b7b2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-rivers-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@trigger.dev/core": patch
---

feat: Add $not to eventFilters
10 changes: 10 additions & 0 deletions packages/core/src/eventFilterMatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 3 additions & 0 deletions packages/core/src/schemas/eventFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()])
})
])
),
]);
Expand Down
48 changes: 48 additions & 0 deletions packages/core/test/eventFilterMatches.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 740b7b2

Please sign in to comment.