UNPKG

@wavequery/conductor

Version:
99 lines 2.96 kB
import { z } from "zod"; import { zodToJsonSchema } from "zod-to-json-schema"; export class ToolValidator { constructor(inputSchema, outputSchema) { this.inputSchema = inputSchema; this.outputSchema = outputSchema; } async validateInput(input) { try { const data = (await this.inputSchema.parseAsync(input)); return { success: true, data }; } catch (error) { if (error instanceof z.ZodError) { return { success: false, errors: this.formatZodErrors(error), }; } throw error; } } async validateOutput(output) { try { const data = (await this.outputSchema.parseAsync(output)); return { success: true, data }; } catch (error) { if (error instanceof z.ZodError) { return { success: false, errors: this.formatZodErrors(error), }; } throw error; } } formatZodErrors(error) { return error.errors.map((err) => ({ path: err.path.map((p) => p.toString()), message: err.message, })); } getInputSchema() { return this.schemaToJSON(this.inputSchema); } getOutputSchema() { return this.schemaToJSON(this.outputSchema); } schemaToJSON(schema) { return zodToJsonSchema(schema, { $refStrategy: "none", definitionPath: "definitions", }); } } // Example schema builders for common tool patterns // export const schemaBuilders = { // /** // * Creates a schema for paginated inputs // */ // withPagination: <T extends z.ZodSchema>(schema: T) => { // return schema.extend({ // page: z.number().int().min(1).optional().default(1), // limit: z.number().int().min(1).max(100).optional().default(10) // }); // }, // /** // * Creates a schema for filterable inputs // */ // withFilters: <T extends z.ZodSchema>(schema: T, filterSchema: z.ZodSchema) => { // return schema.extend({ // filters: z.array(filterSchema).optional() // }); // }, // /** // * Creates a schema for sortable inputs // */ // withSorting: <T extends z.ZodSchema>(schema: T, sortableFields: string[]) => { // return schema.extend({ // sortBy: z.enum(sortableFields as [string, ...string[]]).optional(), // sortOrder: z.enum(['asc', 'desc']).optional().default('asc') // }); // } // }; // Helper function to create tool validator // export function createToolValidator< // Input = any, // Output = any // >(config: { // input: z.ZodSchema<Input>; // output: z.ZodSchema<Output>; // }): ToolValidator { // return new ToolValidator( // config.input, // config.output // ); // } //# sourceMappingURL=tool-validation.js.map