@mastra/schema-compat
Version:
Tool schema compatibility layer for Mastra.ai
100 lines • 3.53 kB
TypeScript
import Ajv from 'ajv';
import type { JSONSchema7 } from '../../_types/@types_json-schema/index.d.ts';
import type { StandardSchemaWithJSON, StandardSchemaWithJSONProps } from '../standard-schema.types.js';
/**
* Options for creating a Standard Schema from JSON Schema.
*/
export interface JsonSchemaAdapterOptions {
/**
* Custom name for the schema (used for error messages).
*/
name?: string;
/**
* Ajv options to customize validation behavior.
* @see https://ajv.js.org/options.html
*/
ajvOptions?: ConstructorParameters<typeof Ajv>[0];
}
/**
* A wrapper class that makes JSON Schema compatible with @standard-schema/spec.
*
* This class implements both `StandardSchemaV1` (validation) and `StandardJSONSchemaV1`
* (JSON Schema conversion) interfaces. Validation is performed using Ajv (Another JSON
* Schema Validator).
*
* @typeParam T - The TypeScript type that the JSON Schema represents
*
* @example
* ```typescript
* import { toStandardSchema } from '@mastra/schema-compat/adapters/json-schema';
*
* const userJsonSchema = {
* type: 'object',
* properties: {
* name: { type: 'string' },
* age: { type: 'number', minimum: 0 },
* },
* required: ['name', 'age'],
* } as const;
*
* const standardSchema = toStandardSchema<{ name: string; age: number }>(userJsonSchema);
*
* // 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 JsonSchemaWrapper<Input = unknown, Output = Input> implements StandardSchemaWithJSON<Input, Output> {
#private;
readonly '~standard': StandardSchemaWithJSONProps<Input, Output>;
constructor(schema: JSONSchema7, options?: JsonSchemaAdapterOptions);
/**
* Returns the original JSON Schema.
*/
getSchema(): JSONSchema7;
/**
* Returns the Ajv instance used for validation.
* Useful for advanced use cases like adding custom formats or keywords.
*/
getAjv(): Ajv;
}
/**
* Wraps a JSON 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. Validation is performed
* using Ajv (Another JSON Schema Validator).
*
* @typeParam T - The TypeScript type that the JSON Schema represents
* @param schema - The JSON Schema to wrap
* @param options - Optional configuration options
* @returns A wrapper implementing StandardSchemaWithJSON
*
* @example
* ```typescript
* import { toStandardSchema } from '@mastra/schema-compat/adapters/json-schema';
*
* const userSchema = {
* type: 'object',
* properties: {
* name: { type: 'string' },
* age: { type: 'number' },
* },
* required: ['name'],
* } as const;
*
* type User = { name: string; age?: number };
*
* const standardSchema = toStandardSchema<User>(userSchema);
*
* // Validate data
* const result = standardSchema['~standard'].validate({ name: 'John' });
*
* // Get JSON Schema
* const jsonSchema = standardSchema['~standard'].jsonSchema.output({ target: 'draft-07' });
* ```
*/
export declare function toStandardSchema<T = unknown>(schema: JSONSchema7, options?: JsonSchemaAdapterOptions): JsonSchemaWrapper<T, T>;
//# sourceMappingURL=json-schema.d.ts.map