Skip to content

ZodValidationBehavior

Defined in: packages/pipeline-zod/src/zod-validation.behavior.ts:112

Pipeline behavior that parses the incoming request (command, query, or event) with a Zod schema when one is attached to the request class via the _zodSchema static property (set automatically by createCommand(), createQuery(), or createZodRequest()).

How it works:

  • If context.requestType._zodSchema is a ZodType, the behavior runs schema.safeParseAsync(context.request).
  • On failure it throws ZodValidationError; toHttpResponse() from @cqrs-ddd/pipeline-zod/http maps it to an HTTP 400.
  • On success, the parsed result must be a plain object because pipeline request identity is preserved in-place. Keys omitted by the schema are deleted and parsed/coerced/defaulted values are assigned before the handler runs. A top-level transform to an array, primitive, Date, or other non-record shape is rejected rather than corrupting the request instance.
  • If no schema is attached (e.g. a plain event class), the behavior is a transparent no-op and simply calls next().

Registration — globally for all request kinds:

createPipeline({
globalBehaviors: {
scope: 'all',
before: [ZodValidationBehavior],
},
});

Registration — per handler only:

class CreateUserHandler {
@pipeline.wrap({ kind: 'command' }, ZodValidationBehavior)
async handle(command: CreateUserCommand) {}
}

new ZodValidationBehavior(): ZodValidationBehavior

ZodValidationBehavior

handle(context, next): Promise<unknown>

Defined in: packages/pipeline-zod/src/zod-validation.behavior.ts:113

IPipelineContext

NextDelegate

Promise<unknown>

IPipelineBehavior.handle