1- import { task } from '@trigger.dev/sdk/v3' ;
2- import { z } from 'zod' ;
1+ // This file shows how @trigger .dev/schema-to-json is used INTERNALLY by the SDK
2+ // Regular users should NOT import this package directly!
3+
34import { schemaToJsonSchema , type JSONSchema } from '@trigger.dev/schema-to-json' ;
5+ import { z } from 'zod' ;
46
5- // Example 1: Using schemaTask (automatic conversion)
6- import { schemaTask } from '@trigger.dev/sdk/v3' ;
7+ // Example of how the SDK uses this internally:
78
89const userSchema = z . object ( {
910 id : z . string ( ) ,
@@ -12,96 +13,69 @@ const userSchema = z.object({
1213 age : z . number ( ) . int ( ) . min ( 0 ) ,
1314} ) ;
1415
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- } ) ;
16+ // This is what happens internally in the SDK's schemaTask:
17+ const result = schemaToJsonSchema ( userSchema ) ;
18+ if ( result ) {
19+ console . log ( 'Converted Zod schema to JSON Schema:' , result . jsonSchema ) ;
20+ console . log ( 'Detected schema type:' , result . schemaType ) ;
21+ // The SDK then includes this JSON Schema in the task metadata
22+ }
2423
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- } ) ;
24+ // Example: How different schema libraries are detected and converted
5525
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 ( ) ,
26+ // Yup schema
27+ import * as y from 'yup' ;
28+ const yupSchema = y . object ( {
29+ name : y . string ( ) . required ( ) ,
30+ age : y . number ( ) . required ( ) ,
6531} ) ;
6632
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- } ,
33+ const yupResult = schemaToJsonSchema ( yupSchema ) ;
34+ console . log ( 'Yup conversion:' , yupResult ) ;
35+
36+ // ArkType schema (has built-in toJsonSchema)
37+ import { type } from 'arktype' ;
38+ const arkSchema = type ( {
39+ name : 'string' ,
40+ age : 'number' ,
8041} ) ;
8142
82- // Example 4: Type-safe JSON Schema creation
83- import { Type , Static } from '@sinclair/typebox' ;
43+ const arkResult = schemaToJsonSchema ( arkSchema ) ;
44+ console . log ( 'ArkType conversion:' , arkResult ) ;
8445
46+ // TypeBox (already JSON Schema)
47+ import { Type } from '@sinclair/typebox' ;
8548const 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 ( ) ,
49+ name : Type . String ( ) ,
50+ age : Type . Number ( ) ,
9351} ) ;
9452
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- } ) ;
53+ const typeBoxResult = schemaToJsonSchema ( typeBoxSchema ) ;
54+ console . log ( 'TypeBox conversion:' , typeBoxResult ) ;
55+
56+ // Example: Initialization (done automatically by the SDK)
57+ import { initializeSchemaConverters , areConvertersInitialized } from '@trigger.dev/schema-to-json' ;
58+
59+ // The SDK calls this once when it loads
60+ await initializeSchemaConverters ( ) ;
61+
62+ // Check which converters are available
63+ const status = areConvertersInitialized ( ) ;
64+ console . log ( 'Converter status:' , status ) ;
65+ // { zod: true, yup: true, effect: true }
66+
67+ // Example: How the SDK determines if a schema can be converted
68+ import { canConvertSchema , detectSchemaType } from '@trigger.dev/schema-to-json' ;
69+
70+ const zodSchema = z . string ( ) ;
71+ console . log ( 'Can convert Zod?' , canConvertSchema ( zodSchema ) ) ; // true
72+ console . log ( 'Schema type:' , detectSchemaType ( zodSchema ) ) ; // 'zod'
73+
74+ // For users: Just use the SDK!
75+ // import { schemaTask } from '@trigger.dev/sdk/v3';
76+ //
77+ // export const myTask = schemaTask({
78+ // id: 'my-task',
79+ // schema: zodSchema,
80+ // run: async (payload) => { /* ... */ }
81+ // });
0 commit comments