UNPKG

@mastra/schema-compat

Version:

Tool schema compatibility layer for Mastra.ai

107 lines 4.28 kB
import type { StandardJSONSchemaV1, StandardSchemaV1 } from '../_types/@standard-schema_spec/dist/index.d.ts'; import type { JSONSchema7 } from '../_types/@types_json-schema/index.d.ts'; import type { PublicSchema } from '../schema.types.js'; import type { StandardSchemaWithJSON } from './standard-schema.types.js'; /** * Override function for JSON Schema conversion. * Handles types that Zod v4 cannot natively represent in JSON Schema: * - z.date() -> { type: 'string', format: 'date-time' } */ declare function jsonSchemaOverride(ctx: { zodSchema: unknown; jsonSchema: Record<string, unknown>; }): undefined; /** * Library options for JSON Schema conversion. * - unrepresentable: 'any' allows z.custom() and other unrepresentable types to be converted to {} * instead of throwing "Custom types cannot be represented in JSON Schema" * - override: converts z.date() to { type: 'string', format: 'date-time' } */ export declare const JSON_SCHEMA_LIBRARY_OPTIONS: { unrepresentable: "any"; override: typeof jsonSchemaOverride; }; export type { StandardSchemaWithJSON, StandardSchemaWithJSONProps, InferInput, InferOutput, StandardSchemaIssue, } from './standard-schema.types.js'; export declare function toStandardSchema<T = unknown>(schema: PublicSchema<T>): StandardSchemaWithJSON<T>; /** * Type guard to check if a value implements the StandardSchemaV1 interface. * * @param value - The value to check * @returns True if the value implements StandardSchemaV1 * * @example * ```typescript * import { isStandardSchema } from '@mastra/schema-compat'; * * if (isStandardSchema(someValue)) { * const result = someValue['~standard'].validate(input); * } * ``` */ export declare function isStandardSchema(value: unknown): value is StandardSchemaV1; /** * Type guard to check if a value implements the StandardJSONSchemaV1 interface. * * @param value - The value to check * @returns True if the value implements StandardJSONSchemaV1 * * @example * ```typescript * import { isStandardJSONSchema } from '@mastra/schema-compat'; * * if (isStandardJSONSchema(someValue)) { * const jsonSchema = someValue['~standard'].jsonSchema.output({ target: 'draft-07' }); * } * ``` */ export declare function isStandardJSONSchema(value: unknown): value is StandardJSONSchemaV1; /** * Type guard to check if a value implements both StandardSchemaV1 and StandardJSONSchemaV1. * * @param value - The value to check * @returns True if the value implements both interfaces * * @example * ```typescript * import { isStandardSchemaWithJSON } from '@mastra/schema-compat'; * * if (isStandardSchemaWithJSON(someValue)) { * // Can use both validation and JSON Schema conversion * const result = someValue['~standard'].validate(input); * const jsonSchema = someValue['~standard'].jsonSchema.output({ target: 'draft-07' }); * } * ``` */ export declare function isStandardSchemaWithJSON(value: unknown): value is StandardSchemaWithJSON; /** * Converts a StandardSchemaWithJSON to a JSON Schema. * * @param schema - The StandardSchemaWithJSON schema to convert * @param options - Conversion options * @param options.target - The JSON Schema target version (default: 'draft-07') * @param options.io - Whether to use input or output schema (default: 'output') * - 'input': Use for tool parameters, function arguments, request bodies * - 'output': Use for return types, response bodies * @returns The JSON Schema representation * * @example * ```typescript * import { standardSchemaToJSONSchema, toStandardSchema } from '@mastra/schema-compat'; * import { z } from 'zod'; * * const zodSchema = z.object({ name: z.string() }); * const standardSchema = toStandardSchema(zodSchema); * * // For output types (default) * const outputSchema = standardSchemaToJSONSchema(standardSchema); * * // For input types (tool parameters) * const inputSchema = standardSchemaToJSONSchema(standardSchema, { io: 'input' }); * ``` */ export declare function standardSchemaToJSONSchema(schema: StandardSchemaWithJSON, options?: { target?: StandardJSONSchemaV1.Target; io?: 'input' | 'output'; override?: (typeof JSON_SCHEMA_LIBRARY_OPTIONS)['override']; }): JSONSchema7; //# sourceMappingURL=standard-schema.d.ts.map