Skip to content

Commit bfd3645

Browse files
committed
Add JSON Schema examples using various libraries
The change introduces extensive examples of using JSON Schemas in the 'references/hello-world' project within the 'trigger.dev' repository. These examples utilize libraries like Zod, Yup, and TypeBox for JSON Schema conversion and validation. The new examples demonstrate different use cases, including automatic conversion with schemaTask, manual schema provision, and schema conversion at build time. We also updated the dependencies in 'package.json' to include the necessary libraries for schema conversion and validation. - Included examples of processing tasks with JSON Schema using libraries such as Zod, Yup, TypeBox, and ArkType. - Showcased schema conversion techniques and type-safe JSON Schema creation. - Updated 'package.json' to ensure all necessary dependencies for schema operations are available. - Created illustrative scripts that cover task management from user processing to complex schema implementations.
1 parent 3935c1e commit bfd3645

File tree

3 files changed

+510
-1
lines changed

3 files changed

+510
-1
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { task } from '@trigger.dev/sdk/v3';
2+
import { z } from 'zod';
3+
import { schemaToJsonSchema, type JSONSchema } from '@trigger.dev/schema-to-json';
4+
5+
// Example 1: Using schemaTask (automatic conversion)
6+
import { schemaTask } from '@trigger.dev/sdk/v3';
7+
8+
const userSchema = z.object({
9+
id: z.string(),
10+
name: z.string(),
11+
email: z.string().email(),
12+
age: z.number().int().min(0),
13+
});
14+
15+
export const processUser = schemaTask({
16+
id: 'process-user',
17+
schema: userSchema,
18+
run: async (payload) => {
19+
// payload is fully typed based on the schema
20+
console.log(`Processing user ${payload.name}`);
21+
return { processed: true };
22+
},
23+
});
24+
25+
// Example 2: Using plain task with manual JSON Schema
26+
export const processOrder = task({
27+
id: 'process-order',
28+
// Manually provide JSON Schema for the payload
29+
payloadSchema: {
30+
type: 'object',
31+
properties: {
32+
orderId: { type: 'string' },
33+
items: {
34+
type: 'array',
35+
items: {
36+
type: 'object',
37+
properties: {
38+
productId: { type: 'string' },
39+
quantity: { type: 'integer', minimum: 1 },
40+
price: { type: 'number', minimum: 0 },
41+
},
42+
required: ['productId', 'quantity', 'price'],
43+
},
44+
},
45+
totalAmount: { type: 'number' },
46+
},
47+
required: ['orderId', 'items', 'totalAmount'],
48+
} satisfies JSONSchema,
49+
run: async (payload) => {
50+
// payload is typed as any, but the schema will be validated at runtime
51+
console.log(`Processing order ${payload.orderId}`);
52+
return { processed: true };
53+
},
54+
});
55+
56+
// Example 3: Using plain task with schema conversion
57+
const orderSchema = z.object({
58+
orderId: z.string(),
59+
items: z.array(z.object({
60+
productId: z.string(),
61+
quantity: z.number().int().min(1),
62+
price: z.number().min(0),
63+
})),
64+
totalAmount: z.number(),
65+
});
66+
67+
// Convert the schema to JSON Schema
68+
const orderJsonSchema = schemaToJsonSchema(orderSchema);
69+
70+
export const processOrderWithConversion = task({
71+
id: 'process-order-converted',
72+
// Use the converted JSON Schema
73+
payloadSchema: orderJsonSchema?.jsonSchema,
74+
run: async (payload) => {
75+
// Note: You still need to validate the payload yourself in plain tasks
76+
const parsed = orderSchema.parse(payload);
77+
console.log(`Processing order ${parsed.orderId}`);
78+
return { processed: true };
79+
},
80+
});
81+
82+
// Example 4: Type-safe JSON Schema creation
83+
import { Type, Static } from '@sinclair/typebox';
84+
85+
const typeBoxSchema = Type.Object({
86+
userId: Type.String(),
87+
action: Type.Union([
88+
Type.Literal('create'),
89+
Type.Literal('update'),
90+
Type.Literal('delete'),
91+
]),
92+
timestamp: Type.Number(),
93+
});
94+
95+
type UserAction = Static<typeof typeBoxSchema>;
96+
97+
export const processUserAction = task({
98+
id: 'process-user-action',
99+
// TypeBox schemas are already JSON Schema compliant
100+
payloadSchema: typeBoxSchema,
101+
run: async (payload) => {
102+
// Cast to get type safety (or validate at runtime)
103+
const action = payload as UserAction;
104+
console.log(`User ${action.userId} performed ${action.action}`);
105+
return { processed: true };
106+
},
107+
});

references/hello-world/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
"dependencies": {
99
"@trigger.dev/build": "workspace:*",
1010
"@trigger.dev/sdk": "workspace:*",
11+
"@trigger.dev/schema-to-json": "workspace:*",
12+
"arktype": "^2.0.0",
1113
"openai": "^4.97.0",
1214
"puppeteer-core": "^24.15.0",
1315
"replicate": "^1.0.1",
14-
"zod": "3.23.8"
16+
"yup": "^1.6.1",
17+
"zod": "3.23.8",
18+
"@sinclair/typebox": "^0.34.3"
1519
},
1620
"scripts": {
1721
"dev": "trigger dev",

0 commit comments

Comments
 (0)