Skip to content

createZodMapper

createZodMapper<TInput, TOutput>(schema): ZodMapper<TInput, TOutput>

Defined in: packages/pipeline-zod/src/create-zod-mapper.ts:41

Creates a mapper that parses input through a Zod schema, typically turning a validated request body into a command at the HTTP edge.

It parses synchronously, so the schema must not use async refinements or transforms. A failure throws ZodValidationError, whose details hold { formErrors, fieldErrors }; toHttpResponse of @cqrs-ddd/pipeline-zod/http answers it with HTTP 400.

TInput

TOutput

ZodType<TOutput, TInput>

Schema whose output is the mapped value.

ZodMapper<TInput, TOutput>

A ZodMapper over schema.

Map a request body to a command

export const CreateUserMapper = createZodMapper(
CreateUserDtoSchema.transform(
({ name, email }) => new CreateUserCommand({ username: name, email }),
),
);
app.post('/users', async (req, res) => {
res.json(await commandBus.execute(CreateUserMapper.map(req.body)));
});