UNPKG

@mastra/schema-compat

Version:

Tool schema compatibility layer for Mastra.ai

85 lines 3.15 kB
import type { Schema } from '../../_types/@internal_ai-v6/dist/index.d.ts'; import type { JSONSchema7 } from '../../_types/@types_json-schema/index.d.ts'; import type { StandardSchemaWithJSON, StandardSchemaWithJSONProps } from '../standard-schema.types.js'; /** * A wrapper class that makes AI SDK Schema compatible with @standard-schema/spec. * * This class implements both `StandardSchemaV1` (validation) and `StandardJSONSchemaV1` * (JSON Schema conversion) interfaces. It wraps an AI SDK Schema and adapts its * validation method and jsonSchema property to the standard-schema interface. * * @typeParam T - The TypeScript type that the AI SDK Schema represents * * @example * ```typescript * import { jsonSchema } from '@internal/ai-v6'; * import { toStandardSchema } from '@mastra/schema-compat/adapters/ai-sdk'; * * // Create an AI SDK schema * const aiSdkSchema = jsonSchema<{ name: string; age: number }>({ * type: 'object', * properties: { * name: { type: 'string' }, * age: { type: 'number' }, * }, * required: ['name', 'age'], * }); * * // Convert to standard-schema * const standardSchema = toStandardSchema(aiSdkSchema); * * // Use validation (from StandardSchemaV1) * const result = standardSchema['~standard'].validate({ name: 'John', age: 30 }); * * // Get JSON Schema (from StandardJSONSchemaV1) * const jsonSchema = standardSchema['~standard'].jsonSchema.output({ target: 'draft-07' }); * ``` */ export declare class AiSdkSchemaWrapper<Input = unknown, Output = Input> implements StandardSchemaWithJSON<Input, Output> { #private; readonly '~standard': StandardSchemaWithJSONProps<Input, Output>; constructor(schema: Schema<Output>); /** * Returns the original AI SDK Schema. */ getSchema(): Schema<Output>; /** * Returns the original JSON Schema from the AI SDK Schema. */ getJsonSchema(): JSONSchema7; } /** * Wraps an AI SDK Schema to implement the full @standard-schema/spec interface. * * This function creates a wrapper that implements both `StandardSchemaV1` (validation) * and `StandardJSONSchemaV1` (JSON Schema conversion) interfaces. * * @typeParam T - The TypeScript type that the AI SDK Schema represents * @param schema - The AI SDK Schema to wrap * @returns A wrapper implementing StandardSchemaWithJSON * * @example * ```typescript * import { jsonSchema } from '@internal/ai-v6'; * import { toStandardSchema } from '@mastra/schema-compat/adapters/ai-sdk'; * * const aiSdkSchema = jsonSchema<{ name: string; age: number }>({ * type: 'object', * properties: { * name: { type: 'string' }, * age: { type: 'number' }, * }, * required: ['name', 'age'], * }); * * const standardSchema = toStandardSchema(aiSdkSchema); * * // Validate data * const result = standardSchema['~standard'].validate({ name: 'John', age: 30 }); * * // Get JSON Schema * const jsonSchema = standardSchema['~standard'].jsonSchema.output({ target: 'draft-07' }); * ``` */ export declare function toStandardSchema<T = unknown>(schema: Schema<T>): AiSdkSchemaWrapper<T, T>; //# sourceMappingURL=ai-sdk.d.ts.map