Skip to content

Commit

Permalink
Install nestjs-zod
Browse files Browse the repository at this point in the history
Configure Swagger on all applications to understand Zod DTOs.
  • Loading branch information
wcalderipe committed Mar 27, 2024
1 parent b4703d6 commit ed0a915
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
| ------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [@app/armory](./apps/armory/README.md) | <a href="https://github.com/narval-xyz/narval/actions/workflows/armory.yml" target="_blank"><img src="https://github.com/narval-xyz/narval/actions/workflows/armory.yml/badge.svg?branch=main" alt="@app/armory CI status" /></a> |
| [@app/policy-engine](./apps/policy-engine/README.md) | <a href="https://github.com/narval-xyz/narval/actions/workflows/policy-engine.yml" target="_blank"><img src="https://github.com/narval-xyz/narval/actions/workflows/policy-engine.yml/badge.svg?branch=main" alt="@app/policy-engine CI status" /></a> |
| [@app/vault](./apps/vault/README.md) | <a href="https://github.com/narval-xyz/armory/actions/workflows/vault.yml" target="_blank"><img src="https://github.com/narval-xyz/armory/actions/workflows/vault.yml/badge.svg" alt="@app/vault CI status" /></a> |
| [@narval/encryption](./packages/encryption/README.md) | <a href="https://github.com/narval-xyz/armory/actions/workflows/packages.yml" target="_blank"><img src="https://github.com/narval-xyz/armory/actions/workflows/packages.yml/badge.svg?branch=main" alt="Packages CI status" /></a> |
| [@narval/policy-engine-shared](./packages/policy-engine-shared/README.md) | <a href="https://github.com/narval-xyz/armory/actions/workflows/packages.yml" target="_blank"><img src="https://github.com/narval-xyz/armory/actions/workflows/packages.yml/badge.svg?branch=main" alt="Packages CI status" /></a> |
| [@narval/signature](./packages/signature/README.md) | <a href="https://github.com/narval-xyz/armory/actions/workflows/packages.yml" target="_blank"><img src="https://github.com/narval-xyz/armory/actions/workflows/packages.yml/badge.svg?branch=main" alt="Packages CI status" /></a> |
Expand Down
7 changes: 7 additions & 0 deletions apps/armory/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ClassSerializerInterceptor, INestApplication, Logger, ValidationPipe }
import { ConfigService } from '@nestjs/config'
import { NestFactory, Reflector } from '@nestjs/core'
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
import { patchNestJsSwagger } from 'nestjs-zod'
import { lastValueFrom, map, of, switchMap } from 'rxjs'
import { Config } from './armory.config'
import { ArmoryModule } from './armory.module'
Expand All @@ -16,6 +17,11 @@ import { ZodExceptionFilter } from './shared/filter/zod-exception.filter'
* @returns The modified INestApplication instance.
*/
const withSwagger = (app: INestApplication): INestApplication => {
// IMPORTANT: This modifies the Nest Swagger module to be compatible with
// DTOs created by Zod schemas. The patch MUST be done before the
// configuration process.
patchNestJsSwagger()

const document = SwaggerModule.createDocument(
app,
new DocumentBuilder()
Expand All @@ -24,6 +30,7 @@ const withSwagger = (app: INestApplication): INestApplication => {
.setVersion('1.0')
.build()
)

SwaggerModule.setup('docs', app, document, {
customSiteTitle: 'Armory API'
})
Expand Down
6 changes: 6 additions & 0 deletions apps/armory/src/orchestration/orchestration.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BullModule } from '@nestjs/bull'
import { ClassSerializerInterceptor, Module, ValidationPipe } from '@nestjs/common'
import { ConfigModule } from '@nestjs/config'
import { APP_FILTER, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core'
import { ZodValidationPipe } from 'nestjs-zod'
import { AUTHORIZATION_REQUEST_PROCESSING_QUEUE } from '../armory.constant'
import { DataFeedModule } from '../data-feed/data-feed.module'
import { PriceModule } from '../price/price.module'
Expand Down Expand Up @@ -63,8 +64,13 @@ import { AuthorizationRequestProcessingProducer } from './queue/producer/authori
useClass: ClassSerializerInterceptor
},
{
// DEPRECATE: Use Zod generated DTOs to validate request and responses.
provide: APP_PIPE,
useClass: ValidationPipe
},
{
provide: APP_PIPE,
useClass: ZodValidationPipe
}
],
exports: [AuthorizationRequestGateway]
Expand Down
6 changes: 6 additions & 0 deletions apps/policy-engine/src/engine/engine.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EncryptionModule } from '@narval/encryption-module'
import { HttpModule } from '@nestjs/axios'
import { Module, ValidationPipe } from '@nestjs/common'
import { APP_PIPE } from '@nestjs/core'
import { ZodValidationPipe } from 'nestjs-zod'
import { load } from '../policy-engine.config'
import { EncryptionModuleOptionFactory } from '../shared/factory/encryption-module-option.factory'
import { AdminApiKeyGuard } from '../shared/guard/admin-api-key.guard'
Expand Down Expand Up @@ -52,8 +53,13 @@ import { TenantRepository } from './persistence/repository/tenant.repository'
TenantService,
EvaluationService,
{
// DEPRECATE: Use Zod generated DTOs to validate request and responses.
provide: APP_PIPE,
useClass: ValidationPipe
},
{
provide: APP_PIPE,
useClass: ZodValidationPipe
}
],
exports: [EngineService, ProvisionService, BootstrapService]
Expand Down
7 changes: 7 additions & 0 deletions apps/policy-engine/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ConfigService } from '@narval/config-module'
import { INestApplication, Logger, ValidationPipe } from '@nestjs/common'
import { NestFactory } from '@nestjs/core'
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
import { patchNestJsSwagger } from 'nestjs-zod'
import { lastValueFrom, map, of, switchMap } from 'rxjs'
import { Config } from './policy-engine.config'
import { PolicyEngineModule } from './policy-engine.module'
Expand All @@ -15,6 +16,11 @@ import { HttpExceptionFilter } from './shared/filter/http-exception.filter'
* @returns The modified INestApplication instance.
*/
const withSwagger = (app: INestApplication): INestApplication => {
// IMPORTANT: This modifies the Nest Swagger module to be compatible with
// DTOs created by Zod schemas. The patch MUST be done before the
// configuration process.
patchNestJsSwagger()

const document = SwaggerModule.createDocument(
app,
new DocumentBuilder()
Expand All @@ -23,6 +29,7 @@ const withSwagger = (app: INestApplication): INestApplication => {
.setVersion('1.0')
.build()
)

SwaggerModule.setup('docs', app, document)

return app
Expand Down
6 changes: 6 additions & 0 deletions apps/policy-engine/src/policy-engine.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ConfigModule, ConfigService } from '@narval/config-module'
import { EncryptionModule } from '@narval/encryption-module'
import { Module, OnApplicationBootstrap, ValidationPipe } from '@nestjs/common'
import { APP_PIPE } from '@nestjs/core'
import { ZodValidationPipe } from 'nestjs-zod'
import { BootstrapService } from './engine/core/service/bootstrap.service'
import { EngineService } from './engine/core/service/engine.service'
import { ProvisionService } from './engine/core/service/provision.service'
Expand All @@ -27,8 +28,13 @@ import { EncryptionModuleOptionFactory } from './shared/factory/encryption-modul
],
providers: [
{
// DEPRECATE: Use Zod generated DTOs to validate request and responses.
provide: APP_PIPE,
useClass: ValidationPipe
},
{
provide: APP_PIPE,
useClass: ZodValidationPipe
}
]
})
Expand Down
6 changes: 6 additions & 0 deletions apps/vault/src/main.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EncryptionModule } from '@narval/encryption-module'
import { Module, ValidationPipe, forwardRef } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { APP_PIPE } from '@nestjs/core'
import { ZodValidationPipe } from 'nestjs-zod'
import { load } from './main.config'
import { EncryptionModuleOptionFactory } from './shared/factory/encryption-module-option.factory'
import { TenantModule } from './tenant/tenant.module'
Expand All @@ -27,8 +28,13 @@ import { VaultModule } from './vault/vault.module'
],
providers: [
{
// DEPRECATE: Use Zod generated DTOs to validate request and responses.
provide: APP_PIPE,
useClass: ValidationPipe
},
{
provide: APP_PIPE,
useClass: ZodValidationPipe
}
]
})
Expand Down
7 changes: 7 additions & 0 deletions apps/vault/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { INestApplication, Logger, ValidationPipe } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { NestFactory } from '@nestjs/core'
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
import { patchNestJsSwagger } from 'nestjs-zod'
import { lastValueFrom, map, of, switchMap } from 'rxjs'
import { MainModule } from './main.module'

Expand All @@ -12,6 +13,11 @@ import { MainModule } from './main.module'
* @returns The modified INestApplication instance.
*/
const withSwagger = (app: INestApplication): INestApplication => {
// IMPORTANT: This modifies the Nest Swagger module to be compatible with
// DTOs created by Zod schemas. The patch MUST be done before the
// configuration process.
patchNestJsSwagger()

const document = SwaggerModule.createDocument(
app,
new DocumentBuilder()
Expand All @@ -20,6 +26,7 @@ const withSwagger = (app: INestApplication): INestApplication => {
.setVersion('1.0')
.build()
)

SwaggerModule.setup('docs', app, document)

return app
Expand Down
6 changes: 6 additions & 0 deletions apps/vault/src/tenant/tenant.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpModule } from '@nestjs/axios'
import { Module, OnApplicationBootstrap, ValidationPipe, forwardRef } from '@nestjs/common'
import { APP_PIPE } from '@nestjs/core'
import { ZodValidationPipe } from 'nestjs-zod'
import { AdminApiKeyGuard } from '../shared/guard/admin-api-key.guard'
import { KeyValueModule } from '../shared/module/key-value/key-value.module'
import { VaultModule } from '../vault/vault.module'
Expand All @@ -19,8 +20,13 @@ import { TenantRepository } from './persistence/repository/tenant.repository'
TenantRepository,
TenantService,
{
// DEPRECATE: Use Zod generated DTOs to validate request and responses.
provide: APP_PIPE,
useClass: ValidationPipe
},
{
provide: APP_PIPE,
useClass: ZodValidationPipe
}
],
exports: [TenantService, TenantRepository]
Expand Down
Loading

0 comments on commit ed0a915

Please sign in to comment.