UNPKG

@mastra/core

Version:
65 lines 2.78 kB
/** * Minimal JSON-Schema ↔ Zod bridge for stored workflows: a converter for the * static subset Zod round-trips through `standardSchemaToJSONSchema`, plus a * non-throwing validator for the write path. */ import { z } from 'zod'; /** * Minimal JSON-Schema shape we accept. Intentionally untyped on the value side * — different JSON Schema producers emit slightly different shapes and the * inline converter below just inspects the fields it cares about. */ export type JsonSchema = Record<string, any>; /** * Options controlling how `jsonSchemaToZod` handles JSON Schema keywords the * MVP converter doesn't support. * * - `throw` (default): hard-crash with a targeted error. Correct for the save * path — the author is right there and can simplify the schema. * - `warn`: emit a warning via `onUnsupported` (if provided) and fall back to * `z.any()` for the unsupported subtree. Correct for the boot-time load * path — one bad pre-existing row must not take down startup for every * other workflow. */ export interface JsonSchemaToZodOptions { onUnsupportedSchema?: 'throw' | 'warn'; onUnsupported?: (message: string) => void; } /** * Inline converter sufficient for the static subset Zod typically emits when * round-tripped through `standardSchemaToJSONSchema`. Handles: * * - `object` with `properties` + `required` * - `string` / `number` / `integer` / `boolean` / `null` * - `array` with `items` * - `enum` * - `description` (propagated via `.describe`) * * For more exotic schemas (unions, intersections, recursive refs) swap in * `json-schema-to-zod` from npm. Kept inline to avoid pulling a dependency * for the MVP demo. */ export declare function jsonSchemaToZod(schema: JsonSchema, opts?: JsonSchemaToZodOptions): z.ZodTypeAny; /** * Result of a `validateStorableJsonSchema` call. * `unsupported` lists every offending keyword usage as `<jsonPointer>: <keyword>` * so callers can log or surface a targeted message per offense. */ export type StorableJsonSchemaValidation = { ok: true; } | { ok: false; unsupported: string[]; }; /** * Non-throwing companion to `jsonSchemaToZod`. Walks a JSON Schema and reports * every unsupported-keyword usage without converting. Use this at write time * (e.g. inside `Mastra.addStoredWorkflow`) to surface a warning before the * schema is persisted — the row will still fail to rehydrate on the next boot * (`jsonSchemaToZod` throws), so this is a heads-up, not a guarantee. * * Callers decide whether to warn, reject, or ignore. This function never * throws for any input shape. */ export declare function validateStorableJsonSchema(schema: JsonSchema | undefined): StorableJsonSchemaValidation; //# sourceMappingURL=json-schema-to-zod.d.ts.map