createZodRequest
createZodRequest<
TSchema,TBase>(schema,Base?):ZodRequestClass<TSchema,TBase>
Defined in: packages/pipeline-zod/src/create-zod-request.ts:130
Generates a strongly-typed Command, Query, or Event class from a Zod schema.
Compatible with NestJS CQRS, Standard Schema, and ZodValidationBehavior:
- Attaches the schema as static
_zodSchema(ZOD_SCHEMA_KEY) andschema. - Forwards Standard Schema specification (
~standard) for schema interoperability. - Provides static
parse()andsafeParse()directly on the class. - Inherits from an optional base class, preserving its prototype, instanceof checks, and constructor arguments.
- Validates input and applies transformations (e.g. email trimming / lowercasing) on construction, throwing ZodValidationError on invalid payload.
- Safely assigns transformed output properties onto the instance including own keys whose parsed value is
undefined.
Type Parameters
Section titled “Type Parameters”TSchema
Section titled “TSchema”TSchema extends ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>
TBase extends AbstractConstructor<object> = AbstractConstructor<object>
Parameters
Section titled “Parameters”schema
Section titled “schema”TSchema
TBase
Returns
Section titled “Returns”ZodRequestClass<TSchema, TBase>
Examples
Section titled “Examples”Defining a Command with a base class
abstract class AppCommand { constructor(readonly actorId?: string) {}}
const CreateUserSchema = z.object({ username: z.string().min(3), email: z.email(),});
export class CreateUserCommand extends createCommand(CreateUserSchema, AppCommand) {}const command = new CreateUserCommand(input, actorId);Defining a Query without a base class
const GetUserSchema = z.object({ id: z.uuid() });export class GetUserQuery extends createQuery(GetUserSchema) {}