UNPKG

@mastra/schema-compat

Version:

Tool schema compatibility layer for Mastra.ai

96 lines 3.56 kB
import type { ZodSchema as ZodSchemaV3, ZodType as ZodTypeV3 } from 'zod/v3'; import type { ZodType as ZodSchemaV4, ZodType as ZodTypeV4 } from 'zod/v4'; import type { Targets } from 'zod-to-json-schema'; import type { JSONSchema7, Schema } from './json-schema/index.js'; import type { PublicSchema } from './schema.js'; import type { SchemaCompatLayer } from './schema-compatibility.js'; type ZodSchema = ZodSchemaV3 | ZodSchemaV4; type ZodType = ZodTypeV3 | ZodTypeV4; /** * Converts a Zod schema to an AI SDK Schema with validation support. * * This function mirrors the behavior of Vercel's AI SDK zod-schema utility but allows * customization of the JSON Schema target format. * * @param zodSchema - The Zod schema to convert * @param target - The JSON Schema target format (defaults to 'jsonSchema7') * @returns An AI SDK Schema object with built-in validation * * @example * ```typescript * import { z } from 'zod'; * import { convertZodSchemaToAISDKSchema } from '@mastra/schema-compat'; * * const userSchema = z.object({ * name: z.string(), * age: z.number().min(0) * }); * * const aiSchema = convertZodSchemaToAISDKSchema(userSchema); * ``` */ export declare function convertZodSchemaToAISDKSchema(zodSchema: ZodSchema, target?: Targets): Schema<any>; /** * Checks if a value is a Zod type by examining its properties and methods. * * @param value - The value to check * @returns True if the value is a Zod type, false otherwise * @internal */ export declare function isZodType(value: unknown): value is ZodType; /** * Converts an AI SDK Schema or Zod schema to a Zod schema. * * If the input is already a Zod schema, it returns it unchanged. * If the input is an AI SDK Schema, it extracts the JSON schema and converts it to Zod. * * @param schema - The schema to convert (AI SDK Schema or Zod schema) * @returns A Zod schema equivalent of the input * @throws Error if the conversion fails * * @example * ```typescript * import { jsonSchema } from 'ai'; * import { convertSchemaToZod } from '@mastra/schema-compat'; * * const aiSchema = jsonSchema({ * type: 'object', * properties: { * name: { type: 'string' } * } * }); * * const zodSchema = convertSchemaToZod(aiSchema); * ``` */ export declare function convertSchemaToZod(schema: Schema | ZodSchema | JSONSchema7): ZodType; /** * Processes a schema using provider compatibility layers and converts it to an AI SDK Schema. * * @param options - Configuration object for schema processing * @param options.schema - The schema to process (AI SDK Schema or Zod object schema) * @param options.compatLayers - Array of compatibility layers to try * @param options.mode - Must be 'aiSdkSchema' * @returns Processed schema as an AI SDK Schema */ export declare function applyCompatLayer(options: { schema: PublicSchema<any>; compatLayers: SchemaCompatLayer[]; mode: 'aiSdkSchema'; }): Schema; /** * Processes a schema using provider compatibility layers and converts it to a JSON Schema. * * @param options - Configuration object for schema processing * @param options.schema - The schema to process (AI SDK Schema or Zod object schema) * @param options.compatLayers - Array of compatibility layers to try * @param options.mode - Must be 'jsonSchema' * @returns Processed schema as a JSONSchema7 */ export declare function applyCompatLayer(options: { schema: PublicSchema<any>; compatLayers: SchemaCompatLayer[]; mode: 'jsonSchema'; }): JSONSchema7; export {}; //# sourceMappingURL=utils.d.ts.map