import type { GenericSchema, LiteralSchema, NumberSchema, ObjectSchema, OptionalSchema, StringSchema } from "valibot"; /** * Minimal interface for Valibot instance to avoid circular type references. * WARNING: Using `typeof v` directly causes TypeScript declaration generation to fail * with stack overflow errors. This interface captures only the methods we actually use. */ interface ValibotLike { object: (...args: any[]) => any; strictObject: (...args: any[]) => any; string: (...args: any[]) => any; number: (...args: any[]) => any; literal: (...args: any[]) => any; union: (...args: any[]) => any; optional: (...args: any[]) => any; record: (...args: any[]) => any; any: (...args: any[]) => any; picklist: (...args: any[]) => any; pipe: (...args: any[]) => any; integer: (...args: any[]) => any; minValue: (...args: any[]) => any; safeParse: (...args: any[]) => any; } /** * Shape helper types for better cross-package type inference * These mirror the Zod implementation patterns exactly */ type BaseMessageEntries = { type: LiteralSchema; meta: ObjectSchema<{ timestamp: OptionalSchema, undefined>; correlationId: OptionalSchema, undefined>; }, undefined>; }; type MessageWithPayloadEntries> = BaseMessageEntries & { payload: ObjectSchema; }; type MessageWithExtendedMetaEntries> = { type: LiteralSchema; meta: ObjectSchema<{ timestamp: OptionalSchema, undefined>; correlationId: OptionalSchema, undefined>; } & M, undefined>; }; type MessageWithPayloadAndMetaEntries, M extends Record> = { type: LiteralSchema; meta: ObjectSchema<{ timestamp: OptionalSchema, undefined>; correlationId: OptionalSchema, undefined>; } & M, undefined>; payload: ObjectSchema; }; /** * Factory function to create messageSchema using the consumer's Valibot instance. * * CRITICAL: This factory pattern is required to fix discriminated union support. * Without it, the library and consumer use different Valibot instances, causing * instanceof checks to fail and discriminatedUnion to throw runtime errors. * * The factory pattern ensures: * - Both library and app use the same Valibot instance (no dual package hazard) * - Validation works correctly with proper instanceof checks * - Type inference flows through without manual type assertions * - Schemas are composable and can be used in unions * * @param valibot - The Valibot instance from the consuming application * @returns Object with messageSchema function and related utilities * * @example Basic usage: * ```typescript * import * as v from "valibot"; * import { createMessageSchema } from "bun-ws-router/valibot"; * * const { messageSchema } = createMessageSchema(v); * const PingSchema = messageSchema("PING"); * ``` * * @example Singleton pattern (recommended for apps): * ```typescript * // schemas/factory.ts * export const { messageSchema, createMessage } = createMessageSchema(v); * * // schemas/messages.ts * import { messageSchema } from "./factory"; * const LoginSchema = messageSchema("LOGIN", { username: v.string() }); * ``` * * @example With discriminated unions: * ```typescript * const PingSchema = messageSchema("PING"); * const PongSchema = messageSchema("PONG"); * * // This now works correctly! * const MessageUnion = v.union([PingSchema, PongSchema]); * ``` */ export declare function createMessageSchema(valibot: ValibotLike): { messageSchema: { (messageType: T): ObjectSchema, undefined>; , undefined>>(messageType: T, payload: P): ObjectSchema, undefined>; >(messageType: T, payload: P): ObjectSchema, undefined>; >(messageType: T, payload: undefined, meta: M): ObjectSchema, undefined>; , undefined>, M extends Record>(messageType: T, payload: P, meta: M): ObjectSchema, undefined>; , M extends Record>(messageType: T, payload: P, meta: M): ObjectSchema, undefined>; }; MessageMetadataSchema: any; ErrorCode: any; ErrorMessage: ObjectSchema, undefined>; createMessage: (schema: MessageSchemaType, payload?: unknown, meta?: Record) => any; }; type MessageSchemaType = ObjectSchema<{ type: LiteralSchema; meta: ObjectSchema; payload?: ObjectSchema; }, undefined>; export type MessageSchema = never> = [P] extends [never] ? ObjectSchema, undefined> : ObjectSchema, undefined>; export type AnyMessageSchema = ObjectSchema<{ type: LiteralSchema; meta: ObjectSchema; payload?: ObjectSchema; }, undefined>; export {}; //# sourceMappingURL=schema.d.ts.map