Skip to content

Commit 50137a6

Browse files
authored
Decouple zod (#500)
Zod Schemas is no longer required for validating/inferring event triggers. We’ve taken inspiration from how domain-functions did it: seasonedcc/composable-functions#114
1 parent 3703f98 commit 50137a6

File tree

21 files changed

+131
-58
lines changed

21 files changed

+131
-58
lines changed

.changeset/strong-seas-impress.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
"@trigger.dev/core": patch
4+
---
5+
6+
Decouple zod

apps/webapp/app/components/jobs/JobsTable.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ function classForJobStatus(status: JobRunStatus) {
198198
case "WAITING_ON_CONNECTIONS":
199199
case "PENDING":
200200
case "UNRESOLVED_AUTH":
201+
case "INVALID_PAYLOAD":
201202
return "text-rose-500";
202203
default:
203204
return "";

apps/webapp/app/components/run/RunCompletedDetail.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CodeBlock } from "~/components/code/CodeBlock";
22
import { DateTime } from "~/components/primitives/DateTime";
33
import { Paragraph } from "~/components/primitives/Paragraph";
44
import { RunStatusIcon, RunStatusLabel } from "~/components/runs/RunStatuses";
5-
import { MatchedRun, useRun } from "~/hooks/useRun";
5+
import { MatchedRun } from "~/hooks/useRun";
66
import { formatDuration } from "~/utils";
77
import {
88
RunPanel,

apps/webapp/app/components/runs/RunStatuses.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export function hasFinished(status: JobRunStatus): boolean {
1717
status === "ABORTED" ||
1818
status === "TIMED_OUT" ||
1919
status === "CANCELED" ||
20-
status === "UNRESOLVED_AUTH"
20+
status === "UNRESOLVED_AUTH" ||
21+
status === "INVALID_PAYLOAD"
2122
);
2223
}
2324

@@ -49,6 +50,7 @@ export function RunStatusIcon({ status, className }: { status: JobRunStatus; cla
4950
case "TIMED_OUT":
5051
return <ExclamationTriangleIcon className={cn(runStatusClassNameColor(status), className)} />;
5152
case "UNRESOLVED_AUTH":
53+
case "INVALID_PAYLOAD":
5254
return <XCircleIcon className={cn(runStatusClassNameColor(status), className)} />;
5355
case "WAITING_ON_CONNECTIONS":
5456
return <WrenchIcon className={cn(runStatusClassNameColor(status), className)} />;
@@ -77,6 +79,7 @@ export function runBasicStatus(status: JobRunStatus): RunBasicStatus {
7779
case "UNRESOLVED_AUTH":
7880
case "CANCELED":
7981
case "ABORTED":
82+
case "INVALID_PAYLOAD":
8083
return "FAILED";
8184
case "SUCCESS":
8285
return "COMPLETED";
@@ -111,6 +114,8 @@ export function runStatusTitle(status: JobRunStatus): string {
111114
return "Canceled";
112115
case "UNRESOLVED_AUTH":
113116
return "Unresolved auth";
117+
case "INVALID_PAYLOAD":
118+
return "Invalid payload";
114119
default: {
115120
const _exhaustiveCheck: never = status;
116121
throw new Error(`Non-exhaustive match for value: ${status}`);
@@ -130,6 +135,7 @@ export function runStatusClassNameColor(status: JobRunStatus): string {
130135
return "text-amber-300";
131136
case "FAILURE":
132137
case "UNRESOLVED_AUTH":
138+
case "INVALID_PAYLOAD":
133139
return "text-rose-500";
134140
case "TIMED_OUT":
135141
return "text-amber-300";

apps/webapp/app/services/runs/performRunExecutionV1.server.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
CachedTaskSchema,
33
RunJobError,
4+
RunJobInvalidPayloadError,
45
RunJobResumeWithTask,
56
RunJobRetryWithTask,
67
RunJobSuccess,
@@ -348,6 +349,11 @@ export class PerformRunExecutionV1Service {
348349

349350
break;
350351
}
352+
case "INVALID_PAYLOAD": {
353+
await this.#failRunWithInvalidPayloadError(execution, safeBody.data);
354+
355+
break;
356+
}
351357
default: {
352358
const _exhaustiveCheck: never = status;
353359
throw new Error(`Non-exhaustive match for value: ${status}`);
@@ -453,6 +459,15 @@ export class PerformRunExecutionV1Service {
453459
});
454460
}
455461

462+
async #failRunWithInvalidPayloadError(
463+
execution: FoundRunExecution,
464+
data: RunJobInvalidPayloadError
465+
) {
466+
return await $transaction(this.#prismaClient, async (tx) => {
467+
await this.#failRunExecution(tx, execution, data.errors, "INVALID_PAYLOAD");
468+
});
469+
}
470+
456471
async #retryRunWithTask(execution: FoundRunExecution, data: RunJobRetryWithTask) {
457472
const { run } = execution;
458473

@@ -572,7 +587,7 @@ export class PerformRunExecutionV1Service {
572587
prisma: PrismaClientOrTransaction,
573588
execution: FoundRunExecution,
574589
output: Record<string, any>,
575-
status: "FAILURE" | "ABORTED" | "UNRESOLVED_AUTH" = "FAILURE"
590+
status: "FAILURE" | "ABORTED" | "UNRESOLVED_AUTH" | "INVALID_PAYLOAD" = "FAILURE"
576591
): Promise<void> {
577592
const { run } = execution;
578593

apps/webapp/app/services/runs/performRunExecutionV2.server.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
CachedTask,
33
RunJobError,
4+
RunJobInvalidPayloadError,
45
RunJobResumeWithTask,
56
RunJobRetryWithTask,
67
RunJobSuccess,
@@ -360,6 +361,11 @@ export class PerformRunExecutionV2Service {
360361

361362
break;
362363
}
364+
case "INVALID_PAYLOAD": {
365+
await this.#failRunWithInvalidPayloadError(run, safeBody.data, durationInMs);
366+
367+
break;
368+
}
363369
default: {
364370
const _exhaustiveCheck: never = status;
365371
throw new Error(`Non-exhaustive match for value: ${status}`);
@@ -455,6 +461,23 @@ export class PerformRunExecutionV2Service {
455461
});
456462
}
457463

464+
async #failRunWithInvalidPayloadError(
465+
execution: FoundRun,
466+
data: RunJobInvalidPayloadError,
467+
durationInMs: number
468+
) {
469+
return await $transaction(this.#prismaClient, async (tx) => {
470+
await this.#failRunExecution(
471+
tx,
472+
"EXECUTE_JOB",
473+
execution,
474+
data.errors,
475+
"INVALID_PAYLOAD",
476+
durationInMs
477+
);
478+
});
479+
}
480+
458481
async #retryRunWithTask(
459482
run: FoundRun,
460483
data: RunJobRetryWithTask,
@@ -579,7 +602,7 @@ export class PerformRunExecutionV2Service {
579602
reason: "EXECUTE_JOB" | "PREPROCESS",
580603
run: FoundRun,
581604
output: Record<string, any>,
582-
status: "FAILURE" | "ABORTED" | "TIMED_OUT" | "UNRESOLVED_AUTH" = "FAILURE",
605+
status: "FAILURE" | "ABORTED" | "TIMED_OUT" | "UNRESOLVED_AUTH" | "INVALID_PAYLOAD" = "FAILURE",
583606
durationInMs: number = 0
584607
): Promise<void> {
585608
await $transaction(prisma, async (tx) => {

packages/core/src/schemas/api.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ulid } from "ulid";
22
import { z } from "zod";
33
import { Prettify } from "../types";
44
import { addMissingVersionField } from "./addMissingVersionField";
5-
import { ErrorWithStackSchema } from "./errors";
5+
import { ErrorWithStackSchema, SchemaErrorSchema } from "./errors";
66
import { EventRuleSchema } from "./eventFilter";
77
import { ConnectionAuthSchema, IntegrationConfigSchema } from "./integrations";
88
import { DeserializedJsonSchema, SerializableJsonSchema } from "./json";
@@ -437,6 +437,13 @@ export const RunJobErrorSchema = z.object({
437437

438438
export type RunJobError = z.infer<typeof RunJobErrorSchema>;
439439

440+
export const RunJobInvalidPayloadErrorSchema = z.object({
441+
status: z.literal("INVALID_PAYLOAD"),
442+
errors: z.array(SchemaErrorSchema),
443+
});
444+
445+
export type RunJobInvalidPayloadError = z.infer<typeof RunJobInvalidPayloadErrorSchema>;
446+
440447
export const RunJobUnresolvedAuthErrorSchema = z.object({
441448
status: z.literal("UNRESOLVED_AUTH_ERROR"),
442449
issues: z.record(z.object({ id: z.string(), error: z.string() })),
@@ -477,6 +484,7 @@ export type RunJobSuccess = z.infer<typeof RunJobSuccessSchema>;
477484
export const RunJobResponseSchema = z.discriminatedUnion("status", [
478485
RunJobErrorSchema,
479486
RunJobUnresolvedAuthErrorSchema,
487+
RunJobInvalidPayloadErrorSchema,
480488
RunJobResumeWithTaskSchema,
481489
RunJobRetryWithTaskSchema,
482490
RunJobCanceledWithTaskSchema,

packages/core/src/schemas/errors.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ export const ErrorWithStackSchema = z.object({
77
});
88

99
export type ErrorWithStack = z.infer<typeof ErrorWithStackSchema>;
10+
11+
export const SchemaErrorSchema = z.object({
12+
path: z.array(z.string()),
13+
message: z.string(),
14+
});
15+
16+
export type SchemaError = z.infer<typeof SchemaErrorSchema>;

packages/core/src/schemas/runs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const RunStatusSchema = z.union([
1414
z.literal("ABORTED"),
1515
z.literal("CANCELED"),
1616
z.literal("UNRESOLVED_AUTH"),
17+
z.literal("INVALID_PAYLOAD"),
1718
]);
1819

1920
export const RunTaskSchema = z.object({
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterEnum
2+
ALTER TYPE "JobRunStatus" ADD VALUE 'INVALID_PAYLOAD';

0 commit comments

Comments
 (0)