Skip to content

Commit 4b840a2

Browse files
committed
Refactor: Remove getSchemaToJsonSchema in favor of schemaToJsonSchema
The `getSchemaToJsonSchema` function was removed and replaced with `schemaToJsonSchema` across the codebase. This update introduces a new `@trigger.dev/schema-to-json` package to handle conversions of schema validation libraries to JSON Schema format, centralizing the functionality and improving maintainability. - Removed `getSchemaToJsonSchema` exports and references. - Added new schema conversion utility `@trigger.dev/schema-to-json`. - Updated `trigger-sdk` package to utilize `schemaToJsonSchema` for payloads. - Extensive testing coverage included to ensure conversion accuracy across various schema libraries including Zod, Yup, ArkType, Effect, and TypeBox. - The update ensures consistent and reliable schema conversions, facilitating future enhancements and supporting additional schema libraries.
1 parent f24423e commit 4b840a2

9 files changed

Lines changed: 677 additions & 36 deletions

File tree

packages/core/src/v3/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ export * from "./utils/interval.js";
7373
export * from "./config.js";
7474
export {
7575
getSchemaParseFn,
76-
getSchemaToJsonSchema,
7776
type AnySchemaParseFn,
7877
type SchemaParseFn,
7978
isSchemaZodEsque,

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

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -146,36 +146,3 @@ export function getSchemaParseFn<TType>(procedureParser: Schema): SchemaParseFn<
146146

147147
throw new Error("Could not find a validator fn");
148148
}
149-
150-
export function getSchemaToJsonSchema(schema: Schema): any | undefined {
151-
const parser = schema as any;
152-
153-
// Check if schema has a built-in toJsonSchema method (e.g., ArkType)
154-
if (typeof parser.toJsonSchema === "function") {
155-
return parser.toJsonSchema();
156-
}
157-
158-
// Check if it's a Zod schema
159-
if (typeof parser.parseAsync === "function" || typeof parser.parse === "function") {
160-
// Zod schema detected, but we need zod-to-json-schema library to convert
161-
// Return undefined for now, will be handled by the caller
162-
return undefined;
163-
}
164-
165-
// Check if it's a Yup schema
166-
if (typeof parser.validateSync === "function") {
167-
// Yup schema detected, but we need a yup-to-json-schema library to convert
168-
// Return undefined for now, will be handled by the caller
169-
return undefined;
170-
}
171-
172-
// Check if it's an Effect schema
173-
if (parser._tag === "Schema" || parser._tag === "SchemaClass") {
174-
// Effect schema detected, but we need Effect's JSONSchema.make to convert
175-
// Return undefined for now, will be handled by the caller
176-
return undefined;
177-
}
178-
179-
// For other schema types, return undefined
180-
return undefined;
181-
}

packages/schema-to-json/README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# @trigger.dev/schema-to-json
2+
3+
Convert various schema validation libraries to JSON Schema format.
4+
5+
## Installation
6+
7+
```bash
8+
npm install @trigger.dev/schema-to-json
9+
```
10+
11+
## Supported Schema Libraries
12+
13+
-**Zod** - Full support via `zod-to-json-schema`
14+
-**Yup** - Full support via `@sodaru/yup-to-json-schema`
15+
-**ArkType** - Native support (built-in `toJsonSchema` method)
16+
-**Effect/Schema** - Full support via Effect's JSONSchema module
17+
-**TypeBox** - Native support (already JSON Schema compliant)
18+
-**Valibot** - Coming soon
19+
-**Superstruct** - Coming soon
20+
-**Runtypes** - Coming soon
21+
22+
## Usage
23+
24+
```typescript
25+
import { schemaToJsonSchema } from '@trigger.dev/schema-to-json';
26+
import { z } from 'zod';
27+
28+
// Convert a Zod schema
29+
const zodSchema = z.object({
30+
name: z.string(),
31+
age: z.number(),
32+
email: z.string().email(),
33+
});
34+
35+
const result = schemaToJsonSchema(zodSchema);
36+
console.log(result);
37+
// {
38+
// jsonSchema: {
39+
// type: 'object',
40+
// properties: {
41+
// name: { type: 'string' },
42+
// age: { type: 'number' },
43+
// email: { type: 'string', format: 'email' }
44+
// },
45+
// required: ['name', 'age', 'email']
46+
// },
47+
// schemaType: 'zod'
48+
// }
49+
```
50+
51+
## API
52+
53+
### `schemaToJsonSchema(schema, options?)`
54+
55+
Convert a schema to JSON Schema format.
56+
57+
**Parameters:**
58+
- `schema` - The schema to convert
59+
- `options` (optional)
60+
- `name` - Name to use for the schema (supported by some converters)
61+
- `additionalProperties` - Additional properties to merge into the result
62+
63+
**Returns:**
64+
- `{ jsonSchema, schemaType }` - The converted JSON Schema and detected type
65+
- `undefined` - If the schema cannot be converted
66+
67+
### `canConvertSchema(schema)`
68+
69+
Check if a schema can be converted to JSON Schema.
70+
71+
**Returns:** `boolean`
72+
73+
### `detectSchemaType(schema)`
74+
75+
Detect the type of schema.
76+
77+
**Returns:** `'zod' | 'yup' | 'arktype' | 'effect' | 'valibot' | 'superstruct' | 'runtypes' | 'typebox' | 'unknown'`
78+
79+
## Peer Dependencies
80+
81+
Each schema library is an optional peer dependency. Install only the ones you need:
82+
83+
```bash
84+
# For Zod
85+
npm install zod
86+
87+
# For Yup
88+
npm install yup
89+
90+
# For ArkType
91+
npm install arktype
92+
93+
# For Effect
94+
npm install effect @effect/schema
95+
96+
# For TypeBox
97+
npm install @sinclair/typebox
98+
```
99+
100+
## License
101+
102+
MIT
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"name": "@trigger.dev/schema-to-json",
3+
"version": "4.0.0-v4-beta.25",
4+
"description": "Convert various schema validation libraries to JSON Schema",
5+
"license": "MIT",
6+
"publishConfig": {
7+
"access": "public"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/triggerdotdev/trigger.dev",
12+
"directory": "packages/schema-to-json"
13+
},
14+
"type": "module",
15+
"engines": {
16+
"node": ">=18.20.0"
17+
},
18+
"files": [
19+
"dist"
20+
],
21+
"exports": {
22+
".": {
23+
"import": {
24+
"@triggerdotdev/source": "./src/index.ts",
25+
"types": "./dist/esm/index.d.ts",
26+
"default": "./dist/esm/index.js"
27+
},
28+
"require": {
29+
"@triggerdotdev/source": "./src/index.ts",
30+
"types": "./dist/commonjs/index.d.ts",
31+
"default": "./dist/commonjs/index.js"
32+
}
33+
}
34+
},
35+
"scripts": {
36+
"clean": "rimraf dist",
37+
"build": "npm run clean && npm run build:tshy",
38+
"build:tshy": "tshy",
39+
"dev": "tshy --watch",
40+
"typecheck": "tsc -p tsconfig.src.json --noEmit",
41+
"test": "vitest",
42+
"update-version": "changeset version && node scripts/updateVersion.js",
43+
"check-exports": "tshy --check-exports"
44+
},
45+
"dependencies": {
46+
"zod-to-json-schema": "^3.24.5",
47+
"@sodaru/yup-to-json-schema": "^2.0.1"
48+
},
49+
"devDependencies": {
50+
"@effect/schema": "^0.76.5",
51+
"arktype": "^2.0.0",
52+
"effect": "^3.11.11",
53+
"runtypes": "^6.7.0",
54+
"superstruct": "^2.0.2",
55+
"tshy": "^3.0.2",
56+
"typebox": "^0.34.3",
57+
"valibot": "^1.0.0-beta.8",
58+
"vitest": "^2.1.8",
59+
"yup": "^1.6.1",
60+
"zod": "^3.24.1"
61+
},
62+
"peerDependencies": {
63+
"@effect/schema": "^0.76.5",
64+
"arktype": "^2.0.0",
65+
"effect": "^3.11.11",
66+
"runtypes": "^6.7.0",
67+
"superstruct": "^2.0.2",
68+
"typebox": "^0.34.3",
69+
"valibot": "^1.0.0-beta.8",
70+
"yup": "^1.6.1",
71+
"zod": "^3.24.1"
72+
},
73+
"peerDependenciesMeta": {
74+
"@effect/schema": {
75+
"optional": true
76+
},
77+
"arktype": {
78+
"optional": true
79+
},
80+
"effect": {
81+
"optional": true
82+
},
83+
"runtypes": {
84+
"optional": true
85+
},
86+
"superstruct": {
87+
"optional": true
88+
},
89+
"typebox": {
90+
"optional": true
91+
},
92+
"valibot": {
93+
"optional": true
94+
},
95+
"yup": {
96+
"optional": true
97+
},
98+
"zod": {
99+
"optional": true
100+
}
101+
},
102+
"tshy": {
103+
"selfLink": false,
104+
"exports": {
105+
".": "./src/index.ts"
106+
},
107+
"project": "./tsconfig.src.json"
108+
}
109+
}

0 commit comments

Comments
 (0)