1- import type { JSONSchema7 , JSONSchema7Definition , JSONSchema7Type , JSONSchema7TypeName , JSONSchema7Object , JSONSchema7Array } from '@types/json-schema' ;
1+ // Import JSONSchema from core to ensure compatibility
2+ import type { JSONSchema } from "@trigger.dev/core/v3" ;
23
34export type Schema = unknown ;
4- export type JSONSchema = JSONSchema7 ;
5- export type JSONSchemaDefinition = JSONSchema7Definition ;
6-
7- // Re-export the standard JSON Schema types for convenience
8- export type {
9- JSONSchema7 ,
10- JSONSchema7Type ,
11- JSONSchema7TypeName ,
12- JSONSchema7Definition ,
13- JSONSchema7Object ,
14- JSONSchema7Array ,
15- } from '@types/json-schema' ;
5+ export type { JSONSchema } ;
166
177export interface ConversionOptions {
188 /**
@@ -33,74 +23,97 @@ export interface ConversionResult {
3323 /**
3424 * The detected schema type
3525 */
36- schemaType : 'zod' | 'yup' | 'arktype' | 'effect' | 'valibot' | 'superstruct' | 'runtypes' | 'typebox' | 'unknown' ;
26+ schemaType :
27+ | "zod"
28+ | "yup"
29+ | "arktype"
30+ | "effect"
31+ | "valibot"
32+ | "superstruct"
33+ | "runtypes"
34+ | "typebox"
35+ | "unknown" ;
3736}
3837
3938/**
4039 * Convert a schema from various validation libraries to JSON Schema
41- *
40+ *
4241 * This function attempts to convert schemas without requiring external dependencies to be bundled.
4342 * It will only succeed if:
4443 * 1. The schema has built-in JSON Schema conversion (ArkType, Zod 4, TypeBox)
4544 * 2. The required conversion library is available at runtime (zod-to-json-schema, @sodaru/yup-to-json-schema, etc.)
46- *
45+ *
4746 * @param schema The schema to convert
4847 * @param options Optional conversion options
4948 * @returns The conversion result or undefined if conversion is not possible
5049 */
51- export function schemaToJsonSchema ( schema : Schema , options ?: ConversionOptions ) : ConversionResult | undefined {
50+ export function schemaToJsonSchema (
51+ schema : Schema ,
52+ options ?: ConversionOptions
53+ ) : ConversionResult | undefined {
5254 const parser = schema as any ;
5355
5456 // Check if schema has a built-in toJsonSchema method (e.g., ArkType, Zod 4)
5557 if ( typeof parser . toJsonSchema === "function" ) {
5658 try {
5759 const jsonSchema = parser . toJsonSchema ( ) ;
5860 // Determine if it's Zod or ArkType based on other methods
59- const schemaType = ( typeof parser . parseAsync === "function" || typeof parser . parse === "function" ) ? 'zod' : 'arktype' ;
61+ const schemaType =
62+ typeof parser . parseAsync === "function" || typeof parser . parse === "function"
63+ ? "zod"
64+ : "arktype" ;
6065 return {
61- jsonSchema : options ?. additionalProperties ? { ...jsonSchema , ...options . additionalProperties } : jsonSchema ,
62- schemaType
66+ jsonSchema : options ?. additionalProperties
67+ ? { ...jsonSchema , ...options . additionalProperties }
68+ : jsonSchema ,
69+ schemaType,
6370 } ;
6471 } catch ( error ) {
6572 // If toJsonSchema fails, continue to other checks
6673 }
6774 }
6875
6976 // Check if it's a TypeBox schema (has Static and Kind symbols)
70- if ( parser [ Symbol . for ( ' TypeBox.Kind' ) ] !== undefined ) {
77+ if ( parser [ Symbol . for ( " TypeBox.Kind" ) ] !== undefined ) {
7178 // TypeBox schemas are already JSON Schema compliant
7279 return {
73- jsonSchema : options ?. additionalProperties ? { ...parser , ...options . additionalProperties } : parser ,
74- schemaType : 'typebox'
80+ jsonSchema : options ?. additionalProperties
81+ ? { ...parser , ...options . additionalProperties }
82+ : parser ,
83+ schemaType : "typebox" ,
7584 } ;
7685 }
7786
7887 // For schemas that need external libraries, we need to check if they're available
7988 // This approach avoids bundling the dependencies while still allowing runtime usage
80-
89+
8190 // Check if it's a Zod schema (without built-in toJsonSchema)
8291 if ( typeof parser . parseAsync === "function" || typeof parser . parse === "function" ) {
8392 try {
8493 // Try to access zod-to-json-schema if it's available
8594 // @ts -ignore - This is intentionally dynamic
86- if ( typeof globalThis . __zodToJsonSchema !== ' undefined' ) {
95+ if ( typeof globalThis . __zodToJsonSchema !== " undefined" ) {
8796 // @ts -ignore
8897 const { zodToJsonSchema } = globalThis . __zodToJsonSchema ;
89- const jsonSchema = options ?. name
98+ const jsonSchema = options ?. name
9099 ? zodToJsonSchema ( parser , options . name )
91100 : zodToJsonSchema ( parser ) ;
92-
93- if ( jsonSchema && typeof jsonSchema === ' object' && ' $schema' in jsonSchema ) {
101+
102+ if ( jsonSchema && typeof jsonSchema === " object" && " $schema" in jsonSchema ) {
94103 const { $schema, ...rest } = jsonSchema as any ;
95104 return {
96- jsonSchema : options ?. additionalProperties ? { ...rest , ...options . additionalProperties } : rest ,
97- schemaType : 'zod'
105+ jsonSchema : options ?. additionalProperties
106+ ? { ...rest , ...options . additionalProperties }
107+ : rest ,
108+ schemaType : "zod" ,
98109 } ;
99110 }
100-
111+
101112 return {
102- jsonSchema : options ?. additionalProperties ? { ...jsonSchema , ...options . additionalProperties } : jsonSchema ,
103- schemaType : 'zod'
113+ jsonSchema : options ?. additionalProperties
114+ ? { ...jsonSchema , ...options . additionalProperties }
115+ : jsonSchema ,
116+ schemaType : "zod" ,
104117 } ;
105118 }
106119 } catch ( error ) {
@@ -112,13 +125,15 @@ export function schemaToJsonSchema(schema: Schema, options?: ConversionOptions):
112125 if ( typeof parser . validateSync === "function" && typeof parser . describe === "function" ) {
113126 try {
114127 // @ts -ignore
115- if ( typeof globalThis . __yupToJsonSchema !== ' undefined' ) {
128+ if ( typeof globalThis . __yupToJsonSchema !== " undefined" ) {
116129 // @ts -ignore
117130 const { convertSchema } = globalThis . __yupToJsonSchema ;
118131 const jsonSchema = convertSchema ( parser ) ;
119132 return {
120- jsonSchema : options ?. additionalProperties ? { ...jsonSchema , ...options . additionalProperties } : jsonSchema ,
121- schemaType : 'yup'
133+ jsonSchema : options ?. additionalProperties
134+ ? { ...jsonSchema , ...options . additionalProperties }
135+ : jsonSchema ,
136+ schemaType : "yup" ,
122137 } ;
123138 }
124139 } catch ( error ) {
@@ -127,16 +142,22 @@ export function schemaToJsonSchema(schema: Schema, options?: ConversionOptions):
127142 }
128143
129144 // Check if it's an Effect schema
130- if ( parser . _tag === "Schema" || parser . _tag === "SchemaClass" || typeof parser . ast === "function" ) {
145+ if (
146+ parser . _tag === "Schema" ||
147+ parser . _tag === "SchemaClass" ||
148+ typeof parser . ast === "function"
149+ ) {
131150 try {
132151 // @ts -ignore
133- if ( typeof globalThis . __effectJsonSchema !== ' undefined' ) {
152+ if ( typeof globalThis . __effectJsonSchema !== " undefined" ) {
134153 // @ts -ignore
135154 const { JSONSchema } = globalThis . __effectJsonSchema ;
136155 const jsonSchema = JSONSchema . make ( parser ) ;
137156 return {
138- jsonSchema : options ?. additionalProperties ? { ...jsonSchema , ...options . additionalProperties } : jsonSchema ,
139- schemaType : 'effect'
157+ jsonSchema : options ?. additionalProperties
158+ ? { ...jsonSchema , ...options . additionalProperties }
159+ : jsonSchema ,
160+ schemaType : "effect" ,
140161 } ;
141162 }
142163 } catch ( error ) {
@@ -158,14 +179,14 @@ export function schemaToJsonSchema(schema: Schema, options?: ConversionOptions):
158179export async function initializeSchemaConverters ( ) : Promise < void > {
159180 try {
160181 // @ts -ignore
161- globalThis . __zodToJsonSchema = await import ( ' zod-to-json-schema' ) ;
182+ globalThis . __zodToJsonSchema = await import ( " zod-to-json-schema" ) ;
162183 } catch {
163184 // Zod conversion not available
164185 }
165186
166187 try {
167188 // @ts -ignore
168- globalThis . __yupToJsonSchema = await import ( ' @sodaru/yup-to-json-schema' ) ;
189+ globalThis . __yupToJsonSchema = await import ( " @sodaru/yup-to-json-schema" ) ;
169190 } catch {
170191 // Yup conversion not available
171192 }
@@ -174,9 +195,9 @@ export async function initializeSchemaConverters(): Promise<void> {
174195 // Try Effect first, then @effect/schema
175196 let module ;
176197 try {
177- module = await import ( ' effect' ) ;
198+ module = await import ( " effect" ) ;
178199 } catch {
179- module = await import ( ' @effect/schema' ) ;
200+ module = await import ( " @effect/schema" ) ;
180201 }
181202 if ( module ?. JSONSchema ) {
182203 // @ts -ignore
@@ -198,9 +219,9 @@ export function canConvertSchema(schema: Schema): boolean {
198219/**
199220 * Get the detected schema type
200221 */
201- export function detectSchemaType ( schema : Schema ) : ConversionResult [ ' schemaType' ] {
222+ export function detectSchemaType ( schema : Schema ) : ConversionResult [ " schemaType" ] {
202223 const result = schemaToJsonSchema ( schema ) ;
203- return result ?. schemaType ?? ' unknown' ;
224+ return result ?. schemaType ?? " unknown" ;
204225}
205226
206227/**
@@ -213,10 +234,10 @@ export function areConvertersInitialized(): {
213234} {
214235 return {
215236 // @ts -ignore
216- zod : typeof globalThis . __zodToJsonSchema !== ' undefined' ,
237+ zod : typeof globalThis . __zodToJsonSchema !== " undefined" ,
217238 // @ts -ignore
218- yup : typeof globalThis . __yupToJsonSchema !== ' undefined' ,
239+ yup : typeof globalThis . __yupToJsonSchema !== " undefined" ,
219240 // @ts -ignore
220- effect : typeof globalThis . __effectJsonSchema !== ' undefined' ,
241+ effect : typeof globalThis . __effectJsonSchema !== " undefined" ,
221242 } ;
222- }
243+ }
0 commit comments