Skip to main content
The Solmyr workflow system provides a utility function zodToJsonSchema to convert Zod schemas to JSON Schema. This is particularly useful when defining event parameters in your workflow.

Usage

import { z } from "zod";
import { zodToJsonSchema } from "@solmyr/workflow";

// Define your Zod schema
const userInputSchema = z.object({
    text: z.string(),
    age: z.number().optional()
});

// Convert to JSON Schema
const jsonSchema = zodToJsonSchema(userInputSchema);

Function Signature

function zodToJsonSchema(schema: z.ZodType): object
The function takes a Zod schema as input and returns a JSON Schema object.

Example in Workflow Context

When defining events in your workflow, you can use zodToJsonSchema to generate the schema for event parameters:
import { z } from "zod";
import { zodToJsonSchema, Workflow } from "@solmyr/workflow";

const myWorkflow: Workflow = {
    // ... other workflow properties
    events: {
        "userInput": zodToJsonSchema(z.object({
            text: z.string(),
            age: z.number().optional()
        })),
        "systemEvent": null // events without parameters
    }
    // ... rest of the workflow definition
};

Notes

  • The zodToJsonSchema function uses the zod-to-json-schema library internally.
  • It applies some customizations to the output, such as omitting certain properties ($ref, $schema, default, definitions, additionalProperties, description, markdownDescription).
  • The resulting JSON Schema is compatible with OpenAPI 3.0 specifications.
By using Zod schemas and converting them to JSON Schema, you can ensure type safety in your TypeScript code while also providing clear, validatable schemas for your workflow events.