Gathering detailed insights and metrics for @anatine/zod-nestjs
Gathering detailed insights and metrics for @anatine/zod-nestjs
Gathering detailed insights and metrics for @anatine/zod-nestjs
Gathering detailed insights and metrics for @anatine/zod-nestjs
npm install @anatine/zod-nestjs
Typescript
Module System
Node Version
NPM Version
zod-nestjs-2.0.12
Updated on Apr 05, 2025
zod-nestjs-2.0.11
Updated on Apr 04, 2025
zod-mock-3.14.0
Updated on Apr 04, 2025
zod-openapi-2.2.8
Updated on Apr 04, 2025
zod-mock-3.13.5
Updated on Jan 20, 2025
zod-nestjs-2.0.10
Updated on Jan 20, 2025
TypeScript (99.51%)
JavaScript (0.49%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
759 Stars
525 Commits
108 Forks
8 Watchers
12 Branches
53 Contributors
Updated on Jul 13, 2025
Latest Version
2.0.12
Package Id
@anatine/zod-nestjs@2.0.12
Unpacked Size
37.66 kB
Size
11.75 kB
File Count
22
NPM Version
10.8.2
Node Version
18.20.7
Published on
Apr 05, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
5
Helper methods for using Zod in a NestJS project.
@anatine/zod-openapi, openapi3-ts, and zod are peer dependencies instead of dependant packages.
While zod
is necessary for operation, openapi3-ts
is for type-casting. @anatine/zod-openapi
does the actual conversion
1npm install openapi3-ts zod @anatine/zod-openapi @anatine/zod-nestjs
Use Zod to generate a schema. Additionally, use @anatidae/zod-openapi to extend a schema for OpenAPI and Swagger UI.
Example schema:
1import { createZodDto } from '@anatine/zod-nestjs'; 2import { extendApi } from '@anatine/zod-openapi'; 3import { z } from 'zod'; 4export const CatZ = extendApi( 5 z.object({ 6 name: z.string(), 7 age: z.number(), 8 breed: z.string(), 9 }), 10 { 11 title: 'Cat', 12 description: 'A cat', 13 } 14); 15export class CatDto extends createZodDto(CatZ) {} 16export class UpdateCatDto extends createZodDto(CatZ.omit({ name: true })) {} 17export const GetCatsZ = extendApi( 18 z.object({ 19 cats: extendApi(z.array(z.string()), { description: 'List of cats' }), 20 }), 21 { title: 'Get Cat Response' } 22); 23export class GetCatsDto extends createZodDto(GetCatsZ) {} 24export const CreateCatResponseZ = z.object({ 25 success: z.boolean(), 26 message: z.string(), 27 name: z.string(), 28}); 29export class CreateCatResponseDto extends createZodDto(CreateCatResponseZ) {} 30export class UpdateCatResponseDto extends createZodDto( 31 CreateCatResponseZ.omit({ name: true }) 32) {}
This follows the standard NestJS method of creating controllers.
@nestjs/swagger
decorators should work normally.
Example Controller
1import { ZodValidationPipe } from '@anatine/zod-nestjs'; 2import { 3 Body, 4 Controller, 5 Get, 6 Param, 7 Patch, 8 Post, 9 UsePipes, 10} from '@nestjs/common'; 11import { ApiCreatedResponse } from '@nestjs/swagger'; 12import { 13 CatDto, 14 CreateCatResponseDto, 15 GetCatsDto, 16 UpdateCatDto, 17 UpdateCatResponseDto, 18} from './cats.dto'; 19@Controller('cats') 20@UsePipes(ZodValidationPipe) 21export class CatsController { 22 @Get() 23 @ApiCreatedResponse({ 24 type: GetCatsDto, 25 }) 26 async findAll(): Promise<GetCatsDto> { 27 return { cats: ['Lizzie', 'Spike'] }; 28 } 29 @Get(':id') 30 @ApiCreatedResponse({ 31 type: CatDto, 32 }) 33 async findOne(@Param() { id }: { id: string }): Promise<CatDto> { 34 return { 35 name: `Cat-${id}`, 36 age: 8, 37 breed: 'Unknown', 38 }; 39 } 40 @Post() 41 @ApiCreatedResponse({ 42 description: 'The record has been successfully created.', 43 type: CreateCatResponseDto, 44 }) 45 async create(@Body() createCatDto: CatDto): Promise<CreateCatResponseDto> { 46 return { 47 success: true, 48 message: 'Cat created', 49 name: createCatDto.name, 50 }; 51 } 52 @Patch() 53 async update( 54 @Body() updateCatDto: UpdateCatDto 55 ): Promise<UpdateCatResponseDto> { 56 return { 57 success: true, 58 message: `Cat's age of ${updateCatDto.age} updated`, 59 }; 60 } 61}
NOTE: Responses have to use the ApiCreatedResponse
decorator when using the @nestjs/swagger
module.
Patch the swagger so that it can use Zod types before you create the document.
Example Main App
1import { Logger } from '@nestjs/common'; 2import { NestFactory } from '@nestjs/core'; 3import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; 4import { CatsModule } from './app/cats.module'; 5import { patchNestjsSwagger } from '@anatine/zod-nestjs'; 6async function bootstrap() { 7 const app = await NestFactory.create(CatsModule); 8 const globalPrefix = 'api'; 9 app.setGlobalPrefix(globalPrefix); 10 const config = new DocumentBuilder() 11 .setTitle('Cats example') 12 .setDescription('The cats API description') 13 .setVersion('1.0') 14 .addTag('cats') 15 .build(); 16 patchNestjsSwagger(); // <--- This is the hacky patch using prototypes (for now) 17 const document = SwaggerModule.createDocument(app, config); 18 SwaggerModule.setup('api', app, document); 19 const port = process.env.PORT || 3333; 20 await app.listen(port, () => { 21 Logger.log('Listening at http://localhost:' + port + '/' + globalPrefix); 22 }); 23} 24bootstrap();
@nestjs/swagger
by providing a Swagger UI.Extensive use and inspiration from zod-dto.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
Found 12/20 approved changesets -- score normalized to 6
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
license file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
11 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More