@abitia/zod-dto

Create DTO classes out of Zod's schema definitions

Usage no npm install needed!

<script type="module">
  import abitiaZodDto from 'https://cdn.skypack.dev/@abitia/zod-dto';
</script>

README

zod-dto

Use zod v3 schemas to validate requests in Nest.js. Supports generating OpenApi too!

Getting Started

rush add -p @abitia/zod-dto

Setup a validation pipe (ZodValidationPipe)

In order to validate incoming requests, the ZodValidationPipe needs to be registered.

import { ZodValidationPipe } from '@abitia/zod-dto';

@Controller()
@UsePipes(ZodValidationPipe)
export class TestController {
    // ... your methods here
}

Create a DTO

const createAuctionDtoSchema = z.object({
    item: z.string(),
    price: z.number(),
    type: z.enum(['buy-it-now', 'auction'])
        .default('buy-it-now'),
});

export class CreateAuctionDto extends createZodDto(createAuctionDtoSchema) {}

Use the DTO

@Controller()
@UsePipes(ZodValidationPipe)
export class TestController {
    @Post('/auctions')
    public createAuction(
        @Body() dto: CreateAuctionDto,
    ) {
        // dto is of type { item: string, price: number, type: 'buy-it-now' | 'auction' }
    }
}

Setup OpenAPI (Swagger) support

Add the following snippet to your application's bootstrap function:

import { patchNestjsSwagger } from '@abitia/zod-dto';

patchNestjsSwagger();

Then follow the Nest.js' Swagger Module Guide.

Local Development

Please add tests for every new feature.

  • npm run build - build the package
  • npm run lint - run linter
  • npm run test - run tests

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning.

Authors

  • Jakub Kisielewski - Initial work - kbkk

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License.