/** * vapor-chamber - Standard Schema validator plugin. * * Schema-library agnostic. Works with any schema lib implementing * [Standard Schema v1](https://standardschema.dev/): Zod, Valibot, * ArkType, Effect Schema, etc. The plugin only depends on the * `~standard` interop shape - no schema lib is bundled. * * @example with Zod * import { z } from 'zod'; * import { validateSchemas } from 'vapor-chamber/standard-schema'; * * bus.use(validateSchemas({ * cartAdd: z.object({ id: z.number(), qty: z.number().min(1) }), * orderCreate: z.object({ items: z.array(z.any()).min(1) }), * })); * * @example with Valibot * import * as v from 'valibot'; * * bus.use(validateSchemas({ * cartAdd: v.object({ id: v.number(), qty: v.pipe(v.number(), v.minValue(1)) }), * })); * * @example with ArkType * import { type } from 'arktype'; * * bus.use(validateSchemas({ * cartAdd: type({ id: 'number', 'qty?': 'number>0' }), * })); * * Validation runs against `cmd.target` by default. Switch to `payload`, * `both`, or a custom selector via the `field` option. */ import type { Command, PluginParts } from './command-bus'; export interface StandardSchemaV1 { readonly '~standard': { readonly version: 1; readonly vendor: string; readonly validate: (value: unknown) => StandardSchemaV1Result | Promise>; readonly types?: { readonly input: Input; readonly output: Output; }; }; } export type StandardSchemaV1Result = { readonly value: Output; readonly issues?: undefined; } | { readonly issues: ReadonlyArray; }; export interface StandardSchemaV1Issue { readonly message: string; readonly path?: ReadonlyArray; } export type SchemaValidatorOptions = { /** * Which field on the Command to validate. Default: `'target'`. * - `'target'` - validate `cmd.target` * - `'payload'` - validate `cmd.payload` * - `'both'` - validate `{ target, payload }` as a single object * - `(cmd) => unknown` - extract a custom value from `cmd` */ field?: 'target' | 'payload' | 'both' | ((cmd: Command) => unknown); /** * What to do on validation failure: * - `'reject'` (default) - return `{ ok: false, error }`, a `validateSchemas:invalid:payload` BusError, * without invoking the handler * - `'warn'` - report a `validateSchemas:invalid:payload` warning and continue to the handler * * In both modes the handler gets the command as dispatched: the schema's * output (a coerced or defaulted value) is never written back * (tests/plugins-schema.test.ts). To use that output, validate before * dispatching and dispatch the output. */ onInvalid?: 'reject' | 'warn'; }; /** * Sync schema validator plugin. Use with `createCommandBus()`. * * If any of your schemas are async (return a Promise from `~standard.validate`), * use {@link validateSchemasAsync} on `createAsyncCommandBus()` instead. */ export declare function validateSchemas(schemas: Record, options?: SchemaValidatorOptions): PluginParts; /** * Async variant - supports schemas whose `validate` returns a Promise. * Use with `createAsyncCommandBus()`. */ export declare function validateSchemasAsync(schemas: Record, options?: SchemaValidatorOptions): PluginParts; //# sourceMappingURL=plugins-schema.d.ts.map