@ws-kit/valibot
Version:
Valibot validator adapter for WS-Kit with lightweight runtime validation and minimal bundle size
70 lines • 2.68 kB
TypeScript
/**
* withValibot() plugin: adds Valibot validation capability to the router.
*
* Composes core plugins (withMessaging + withRpc) with Valibot-specific validation:
* - Validates inbound payloads against Valibot schemas
* - Optionally validates outbound payloads (send, reply, progress)
* - Adds ctx.payload (validated) to event handlers
* - Provides .rpc() method for request-response handlers
*
* Once plugged, the router gains:
* - ctx.send() - Fire-and-forget unicast (from withMessaging)
* - ctx.reply() / ctx.error() / ctx.progress() - RPC methods (from withRpc)
* - ctx.payload - Validated message payload
* - router.rpc() - Request-response handler registration
*
* Validation errors are routed to router.onError() or custom onValidationError hook.
*/
import type { ConnectionData, ProgressOptions as CoreProgressOptions, ReplyOptions as CoreReplyOptions } from "@ws-kit/core";
export interface WithValibotOptions {
/**
* Validate outgoing payloads (send, reply, publish).
* Default: true
* Set to false for ultra-hot paths where performance is critical.
* Per-schema override: message({..., options: { validateOutgoing: false }})
*/
validateOutgoing?: boolean;
/**
* Hook for validation errors (inbound/outbound).
* If provided, called instead of routing to router.onError().
*/
onValidationError?: (error: Error & {
code: string;
details: any;
}, context: {
type: string;
direction: "inbound" | "outbound";
payload: unknown;
}) => void | Promise<void>;
}
export interface ReplyOptions extends CoreReplyOptions {
/**
* Whether to validate the outgoing payload.
* Default: uses plugin validateOutgoing setting
*/
validate?: boolean;
}
export interface ProgressOptions extends CoreProgressOptions {
/**
* Whether to validate the outgoing payload.
* Default: uses plugin validateOutgoing setting
*/
validate?: boolean;
}
/**
* Validation plugin API interface with capability marker.
* Added to the router when withValibot() is applied.
*
* The { validation: true } marker enables Router type narrowing:
* - Before plugin: Router<TContext, {}> → keyof excludes rpc()
* - After plugin: Router<TContext, { validation: true }> → keyof includes rpc()
*/
interface WithValibotCapability {
readonly validation: true;
readonly __caps: {
validation: true;
};
}
export declare function withValibot<TContext extends ConnectionData = ConnectionData>(options?: WithValibotOptions): import("@ws-kit/core").Plugin<TContext, WithValibotCapability>;
export {};
//# sourceMappingURL=plugin.d.ts.map