Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

validate merged monitor goal for search targets on partial updates

Developers Digest8c24187960d1
diff --git a/apps/api/src/__tests__/snips/v2/types-validation.test.ts b/apps/api/src/__tests__/snips/v2/types-validation.test.ts
index 79fc03bea..a003619f9 100644
--- a/apps/api/src/__tests__/snips/v2/types-validation.test.ts
+++ b/apps/api/src/__tests__/snips/v2/types-validation.test.ts
@@ -1394,6 +1394,42 @@ describe("V2 Types Validation", () => {
});
});
+ describe("monitor search target goal validation", () => {
+ const searchTargets = [
+ { type: "search" as const, queries: ["firecrawl launch"] },
+ ];
+
+ it("create rejects a search target without a goal", () => {
+ expect(() =>
+ createMonitorSchema.parse({
+ name: "Search monitor",
+ schedule: { text: "every 30 minutes" },
+ targets: searchTargets,
+ }),
+ ).toThrow("A search target requires a non-empty goal");
+ });
+
+ it("update accepts a patch adding a search target without restating the goal", () => {
+ // The monitor may already carry a goal; the controller validates the
+ // merged state.
+ const result = updateMonitorSchema.parse({ targets: searchTargets });
+ expect(result.targets).toHaveLength(1);
+ });
+
+ it("update rejects a patch that adds search targets while clearing the goal", () => {
+ for (const goal of [null, "", " "]) {
+ expect(() =>
+ updateMonitorSchema.parse({ targets: searchTargets, goal }),
+ ).toThrow("A search target requires a non-empty goal");
+ }
+ });
+
+ it("update allows clearing the goal when the patch has no targets (merged state is checked in the controller)", () => {
+ const result = updateMonitorSchema.parse({ goal: null });
+ expect(result.goal).toBeNull();
+ });
+ });
+
describe("Edge cases", () => {
it("should handle URL without protocol (should add http://)", () => {
const input = {
diff --git a/apps/api/src/controllers/v2/monitor.ts b/apps/api/src/controllers/v2/monitor.ts
index 71d51647c..e1c98b6f5 100644
--- a/apps/api/src/controllers/v2/monitor.ts
+++ b/apps/api/src/controllers/v2/monitor.ts
@@ -294,6 +294,22 @@ export async function updateMonitorController(
}
const input = updateMonitorSchema.parse(req.body);
+
+ // Validate the merged monitor state: a partial update can add search
+ // targets that rely on the stored goal, or clear the goal while search
+ // targets remain on the monitor. Both must leave a non-empty goal behind.
+ const mergedTargets = input.targets ?? existing.targets;
+ const mergedGoal = input.goal !== undefined ? input.goal : existing.goal;
+ if (
+ mergedTargets.some(t => t.type === "search") &&
+ (typeof mergedGoal !== "string" || mergedGoal.trim().length === 0)
+ ) {
+ return res.status(400).json({
+ success: false,
+ error: "A search target requires a non-empty goal",
+ });
+ }
+
const cron = input.schedule?.cron ?? existing.schedule_cron;
const timezone = input.schedule?.timezone ?? existing.schedule_timezone;
let schedule;
@@ -584,7 +600,11 @@ const emailActionBodySchema = z.object({
type EmailActionResponse =
| {
success: true;
- result: "confirmed" | "already_confirmed" | "unsubscribed" | "already_unsubscribed";
+ result:
+ | "confirmed"
+ | "already_confirmed"
+ | "unsubscribed"
+ | "already_unsubscribed";
email: string;
monitorName: string | null;
}
@@ -593,7 +613,6 @@ type EmailActionResponse =
error: "invalid_token" | "not_found" | "internal_error";
};
-
function parseTokenFromRequest(req: { body?: unknown }): string | null {
const candidate =
req.body && typeof req.body === "object"
diff --git a/apps/api/src/services/monitoring/types.ts b/apps/api/src/services/monitoring/types.ts
index d09d07f59..6f895d338 100644
--- a/apps/api/src/services/monitoring/types.ts
+++ b/apps/api/src/services/monitoring/types.ts
@@ -185,12 +185,38 @@ export const createMonitorSchema = createMonitorBaseSchema
.superRefine(requireGoalForSearchTargets)
.transform(applyJudgeEnabledDefault);
+// Update bodies are partial: a patch adding search targets may rely on the
+// goal already stored on the monitor, so only reject here when the patch
+// itself includes search targets AND explicitly clears the goal. The
+// controller re-validates the merged monitor (stored row + patch).
+function rejectGoalClearedWithSearchTargets(
+ input: { targets?: unknown; goal?: unknown },
+ ctx: z.RefinementCtx,
+): void {
+ const targets = input.targets;
+ if (!Array.isArray(targets)) return;
+ const hasSearchTarget = targets.some(
+ t =>
+ t && typeof t === "object" && (t as { type?: unknown }).type === "search",
+ );
+ if (!hasSearchTarget) return;
+ const goal = input.goal;
+ if (goal === undefined) return;
+ if (goal === null || (typeof goal === "string" && goal.trim().length === 0)) {
+ ctx.addIssue({
+ code: "custom",
+ message: "A search target requires a non-empty goal",
+ path: ["goal"],
+ });
+ }
+}
+
export const updateMonitorSchema = createMonitorBaseSchema
.partial()
.extend({
status: z.enum(["active", "paused"]).optional(),
})
- .superRefine(requireGoalForSearchTargets)
+ .superRefine(rejectGoalClearedWithSearchTargets)
.refine(x => Object.keys(x).length > 0, "Update body cannot be empty")
.transform(applyJudgeEnabledDefault);