packages/schema-to-json/README.md
Convert various schema validation libraries to JSON Schema format.
npm install @trigger.dev/schema-to-json
This package is designed to be bundle-safe. It does NOT bundle any schema libraries (zod, yup, etc.) as dependencies. Instead:
This design ensures that:
toJsonSchema method (no external deps needed)zod-to-json-schema to be installed@sodaru/yup-to-json-schema to be installedtoJsonSchema method)effect or @effect/schema to be installedimport { schemaToJsonSchema } from '@trigger.dev/schema-to-json';
import { type } from 'arktype';
// Works immediately for schemas with built-in conversion
const arkSchema = type({
name: 'string',
age: 'number',
});
const result = schemaToJsonSchema(arkSchema);
console.log(result);
// { jsonSchema: {...}, schemaType: 'arktype' }
import { schemaToJsonSchema, initializeSchemaConverters } from '@trigger.dev/schema-to-json';
import { z } from 'zod';
// Initialize converters once in your app (loads conversion libraries if available)
await initializeSchemaConverters();
// Now you can convert Zod 3, Yup, and Effect schemas
const zodSchema = z.object({
name: z.string(),
age: z.number(),
email: z.string().email(),
});
const result = schemaToJsonSchema(zodSchema);
console.log(result);
// {
// jsonSchema: {
// type: 'object',
// properties: {
// name: { type: 'string' },
// age: { type: 'number' },
// email: { type: 'string', format: 'email' }
// },
// required: ['name', 'age', 'email']
// },
// schemaType: 'zod'
// }
schemaToJsonSchema(schema, options?)Convert a schema to JSON Schema format.
Parameters:
schema - The schema to convertoptions (optional)
name - Name to use for the schema (supported by some converters)additionalProperties - Additional properties to merge into the resultReturns:
{ jsonSchema, schemaType } - The converted JSON Schema and detected typeundefined - If the schema cannot be convertedinitializeSchemaConverters()Initialize the external conversion libraries. Call this once in your application if you need to convert schemas that don't have built-in JSON Schema support (Zod 3, Yup, Effect).
Returns: Promise<void>
canConvertSchema(schema)Check if a schema can be converted to JSON Schema.
Returns: boolean
detectSchemaType(schema)Detect the type of schema.
Returns: 'zod' | 'yup' | 'arktype' | 'effect' | 'valibot' | 'superstruct' | 'runtypes' | 'typebox' | 'unknown'
areConvertersInitialized()Check which conversion libraries are available.
Returns: { zod: boolean, yup: boolean, effect: boolean }
Each schema library is an optional peer dependency. Install only the ones you need:
# For Zod
npm install zod
# For Yup
npm install yup
# For ArkType
npm install arktype
# For Effect
npm install effect @effect/schema
# For TypeBox
npm install @sinclair/typebox
MIT