UNPKG

tyntec-sdk

Version:

TypeScript SDK for Tyntec Conversations API V3

62 lines (61 loc) 2.16 kB
import { z } from 'zod'; /** * A singleton registry for managing Zod schemas. * This class provides a centralized way to store and retrieve Zod validation schemas. */ export declare class SchemaRegistry { private static instance; private schemas; private constructor(); /** * Gets the singleton instance of SchemaRegistry. * @returns The singleton instance of SchemaRegistry */ static getInstance(): SchemaRegistry; /** * Registers a new schema with the given name. * @param name - The unique identifier for the schema * @param schema - The Zod schema to register */ registerSchema(name: string, schema: z.ZodType): void; /** * Retrieves a schema by its name. * @param name - The name of the schema to retrieve * @returns The Zod schema * @throws {Error} If the schema is not found */ getSchema(name: string): z.ZodType; /** * Returns a copy of all registered schemas. * @returns A Map containing all registered schemas */ getAllSchemas(): Map<string, z.ZodType>; /** * Validates data against a registered schema. * @param name - The name of the schema to use for validation * @param data - The data to validate * @returns The validated and typed data * @throws {z.ZodError} If validation fails */ validate<T>(name: string, data: unknown): T; /** * Safely validates data against a registered schema without throwing errors. * @param name - The name of the schema to use for validation * @param data - The data to validate * @returns An object containing the validation result and either the validated data or error */ safeValidate<T>(name: string, data: unknown): { success: boolean; data?: T; error?: z.ZodError; }; } export declare const schemaRegistry: SchemaRegistry; /** * Helper function to register multiple schemas at once. * @param schemas - Array of objects containing schema names and their corresponding Zod schemas */ export declare function registerSchemas(schemas: Array<{ name: string; schema: z.ZodType; }>): void;