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 packagenpm run lint
- run linternpm 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.