Skip to content

Commit 9cb8e24

Browse files
committed
Fixed some stuff
1 parent 05ad00f commit 9cb8e24

File tree

14 files changed

+857
-309
lines changed

14 files changed

+857
-309
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,4 +578,4 @@
578578
"main": "./dist/commonjs/index.js",
579579
"types": "./dist/commonjs/index.d.ts",
580580
"module": "./dist/esm/index.js"
581-
}
581+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ 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
17-
payloadSchema: z.record(z.unknown()).optional(),
16+
// JSONSchema type - using z.unknown() for runtime validation to accept JSONSchema7
17+
payloadSchema: z.unknown().optional(),
1818
});
1919

2020
export type TaskResource = z.infer<typeof TaskResource>;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ 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
193-
payloadSchema: z.record(z.unknown()).optional(),
192+
// JSONSchema type - using z.unknown() for runtime validation to accept JSONSchema7
193+
payloadSchema: z.unknown().optional(),
194194
};
195195

196196
export const TaskMetadata = z.object(taskMetadata);
Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
11
/**
2-
* Basic JSON Schema type definition
3-
* Based on JSON Schema Draft 7
2+
* JSON Schema type definition - compatible with JSON Schema Draft 7
3+
* Based on the JSONSchema7 type from @types/json-schema but defined inline to avoid import issues
44
*/
5-
export type JSONSchemaType = "string" | "number" | "integer" | "boolean" | "object" | "array" | "null";
6-
75
export interface JSONSchema {
86
$id?: string;
97
$ref?: string;
108
$schema?: string;
119
$comment?: string;
12-
10+
1311
type?: JSONSchemaType | JSONSchemaType[];
1412
enum?: any[];
1513
const?: any;
16-
14+
1715
// Number/Integer validations
1816
multipleOf?: number;
1917
maximum?: number;
2018
exclusiveMaximum?: number;
2119
minimum?: number;
2220
exclusiveMinimum?: number;
23-
21+
2422
// String validations
2523
maxLength?: number;
2624
minLength?: number;
2725
pattern?: string;
2826
format?: string;
29-
27+
3028
// Array validations
3129
items?: JSONSchema | JSONSchema[];
3230
additionalItems?: JSONSchema | boolean;
3331
maxItems?: number;
3432
minItems?: number;
3533
uniqueItems?: boolean;
3634
contains?: JSONSchema;
37-
35+
3836
// Object validations
3937
maxProperties?: number;
4038
minProperties?: number;
@@ -44,26 +42,35 @@ export interface JSONSchema {
4442
additionalProperties?: JSONSchema | boolean;
4543
dependencies?: Record<string, JSONSchema | string[]>;
4644
propertyNames?: JSONSchema;
47-
48-
// Conditionals
45+
46+
// Conditional schemas
4947
if?: JSONSchema;
5048
then?: JSONSchema;
5149
else?: JSONSchema;
52-
50+
5351
// Boolean logic
5452
allOf?: JSONSchema[];
5553
anyOf?: JSONSchema[];
5654
oneOf?: JSONSchema[];
5755
not?: JSONSchema;
58-
56+
5957
// Metadata
6058
title?: string;
6159
description?: string;
6260
default?: any;
6361
readOnly?: boolean;
6462
writeOnly?: boolean;
6563
examples?: any[];
66-
67-
// Additional properties
64+
65+
// Additional properties for extensibility
6866
[key: string]: any;
69-
}
67+
}
68+
69+
export type JSONSchemaType =
70+
| "string"
71+
| "number"
72+
| "integer"
73+
| "boolean"
74+
| "object"
75+
| "array"
76+
| "null";

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,16 @@ export type TaskOptions<
355355
TInitOutput extends InitOutput = any,
356356
> = CommonTaskOptions<TIdentifier, TPayload, TOutput, TInitOutput>;
357357

358+
// Task options when payloadSchema is provided - payload should be any
359+
export type TaskOptionsWithSchema<
360+
TIdentifier extends string,
361+
TOutput = unknown,
362+
TInitOutput extends InitOutput = any,
363+
> = Omit<CommonTaskOptions<TIdentifier, any, TOutput, TInitOutput>, "run"> & {
364+
payloadSchema: JSONSchema;
365+
run: (payload: any, params: RunFnParams<TInitOutput>) => Promise<TOutput>;
366+
};
367+
358368
export type TaskWithSchemaOptions<
359369
TIdentifier extends string,
360370
TSchema extends TaskSchema | undefined = undefined,

packages/schema-to-json/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"check-exports": "tshy --check-exports"
4444
},
4545
"dependencies": {
46-
"@types/json-schema": "^7.0.15",
46+
"@trigger.dev/core": "workspace:*",
4747
"zod-to-json-schema": "^3.24.5",
4848
"@sodaru/yup-to-json-schema": "^2.0.1"
4949
},
@@ -65,7 +65,7 @@
6565
"arktype": "^2.0.0",
6666
"effect": "^3.11.11",
6767
"runtypes": "^6.7.0",
68-
"superstruct": "^2.0.2",
68+
"superstruct": "^2.0.2",
6969
"@sinclair/typebox": "^0.34.3",
7070
"valibot": "^1.0.0-beta.8",
7171
"yup": "^1.6.1",

packages/schema-to-json/src/index.ts

Lines changed: 71 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
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

34
export 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

177
export 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):
158179
export 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

Comments
 (0)