Important
The following package was originally ported from https://github.com/kbkk/abitia/tree/master/packages/zod-dto due to lack of activity.
Use zod v3 schemas to validate requests in Nest.js. Supports generating OpenApi too!
yarn add zod-dto
In order to validate incoming requests, the ZodValidationPipe needs to be registered.
import { ZodValidationPipe } from "zod-dto";
@Controller()
@UsePipes(ZodValidationPipe)
export class TestController {
// ... your methods here
}
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) {}
@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' }
}
}
Add the following snippet to your application's bootstrap function:
import { patchNestjsSwagger } from "@eberhm/zod-dto";
patchNestjsSwagger();
Then follow the Nest.js' Swagger Module Guide.
Please add tests for every new feature.
yarn build
- build the packageyarn lint
- run linteryarn test
- run tests
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning.
See also the list of contributors who participated in this project.
This project is licensed under the MIT License.