Skip to content

Commit 6b1ff95

Browse files
committed
Add JSONSchema type for payloadSchema in tasks
The change was necessary to improve type safety by using a proper JSONSchema type definition instead of a generic Record<string, unknown>. This enhances the developer experience and ensures that task payloads conform to the JSON Schema Draft 7 specification. The JSONSchema type is now re-exported from the SDK for user convenience, hiding internal complexity and maintaining a seamless developer experience. - Added JSONSchema type based on Draft 7 specification - Updated task metadata and options to use JSONSchema type - Hid internal schema conversion logic from users by re-exporting types from SDK - Improved bundle safety and dependency management
1 parent 7d7a3c4 commit 6b1ff95

File tree

6 files changed

+75
-12
lines changed

6 files changed

+75
-12
lines changed

packages/core/src/v3/schemas/resources.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const TaskResource = z.object({
1313
triggerSource: z.string().optional(),
1414
schedule: ScheduleMetadata.optional(),
1515
maxDuration: z.number().optional(),
16+
// JSONSchema type - using z.record for runtime validation
1617
payloadSchema: z.record(z.unknown()).optional(),
1718
});
1819

packages/core/src/v3/schemas/schemas.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ const taskMetadata = {
189189
triggerSource: z.string().optional(),
190190
schedule: ScheduleMetadata.optional(),
191191
maxDuration: z.number().optional(),
192+
// JSONSchema type - using z.record for runtime validation
192193
payloadSchema: z.record(z.unknown()).optional(),
193194
};
194195

packages/core/src/v3/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export * from "./tasks.js";
77
export * from "./idempotencyKeys.js";
88
export * from "./tools.js";
99
export * from "./queues.js";
10+
export * from "./jsonSchema.js";
1011

1112
type ResolveEnvironmentVariablesOptions = {
1213
variables: Record<string, string> | Array<{ name: string; value: string }>;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Basic JSON Schema type definition
3+
* Based on JSON Schema Draft 7
4+
*/
5+
export type JSONSchemaType = "string" | "number" | "integer" | "boolean" | "object" | "array" | "null";
6+
7+
export interface JSONSchema {
8+
$id?: string;
9+
$ref?: string;
10+
$schema?: string;
11+
$comment?: string;
12+
13+
type?: JSONSchemaType | JSONSchemaType[];
14+
enum?: any[];
15+
const?: any;
16+
17+
// Number/Integer validations
18+
multipleOf?: number;
19+
maximum?: number;
20+
exclusiveMaximum?: number;
21+
minimum?: number;
22+
exclusiveMinimum?: number;
23+
24+
// String validations
25+
maxLength?: number;
26+
minLength?: number;
27+
pattern?: string;
28+
format?: string;
29+
30+
// Array validations
31+
items?: JSONSchema | JSONSchema[];
32+
additionalItems?: JSONSchema | boolean;
33+
maxItems?: number;
34+
minItems?: number;
35+
uniqueItems?: boolean;
36+
contains?: JSONSchema;
37+
38+
// Object validations
39+
maxProperties?: number;
40+
minProperties?: number;
41+
required?: string[];
42+
properties?: Record<string, JSONSchema>;
43+
patternProperties?: Record<string, JSONSchema>;
44+
additionalProperties?: JSONSchema | boolean;
45+
dependencies?: Record<string, JSONSchema | string[]>;
46+
propertyNames?: JSONSchema;
47+
48+
// Conditionals
49+
if?: JSONSchema;
50+
then?: JSONSchema;
51+
else?: JSONSchema;
52+
53+
// Boolean logic
54+
allOf?: JSONSchema[];
55+
anyOf?: JSONSchema[];
56+
oneOf?: JSONSchema[];
57+
not?: JSONSchema;
58+
59+
// Metadata
60+
title?: string;
61+
description?: string;
62+
default?: any;
63+
readOnly?: boolean;
64+
writeOnly?: boolean;
65+
examples?: any[];
66+
67+
// Additional properties
68+
[key: string]: any;
69+
}

packages/core/src/v3/types/tasks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { QueueOptions } from "./queues.js";
2828
import { AnySchemaParseFn, inferSchemaIn, inferSchemaOut, Schema } from "./schemas.js";
2929
import { inferToolParameters, ToolTaskParameters } from "./tools.js";
3030
import { Prettify } from "./utils.js";
31+
import { JSONSchema } from "./jsonSchema.js";
3132

3233
export type Queue = QueueOptions;
3334
export type TaskSchema = Schema;
@@ -344,7 +345,7 @@ type CommonTaskOptions<
344345
* JSON Schema for the task payload. This will be synced to the server during indexing.
345346
* Should be a valid JSON Schema Draft 7 object.
346347
*/
347-
payloadSchema?: Record<string, unknown>;
348+
payloadSchema?: JSONSchema;
348349
};
349350

350351
export type TaskOptions<
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,2 @@
11
// Re-export JSON Schema types for user convenience
2-
export type { JSONSchema, JSONSchemaDefinition } from "@trigger.dev/schema-to-json";
3-
4-
// Re-export the standard JSON Schema types from @types/json-schema
5-
export type {
6-
JSONSchema7,
7-
JSONSchema7Type,
8-
JSONSchema7TypeName,
9-
JSONSchema7Definition,
10-
JSONSchema7Object,
11-
JSONSchema7Array,
12-
} from "@trigger.dev/schema-to-json";
2+
export type { JSONSchema } from "@trigger.dev/core/v3";

0 commit comments

Comments
 (0)