Skip to content

Commit

Permalink
feat: config validation pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
leosuncin committed Sep 5, 2022
1 parent 2d7dd11 commit 2250718
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
13 changes: 3 additions & 10 deletions src/common/build-test-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
type ForwardReference,
type INestApplication,
type Type,
HttpStatus,
ValidationPipe,
} from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
Expand All @@ -14,7 +13,7 @@ import cookieParser from 'cookie-parser';
import type { DataSource } from 'typeorm';

import { database } from '~common/database';
import { configuration } from '~config/configuration';
import { type ConfigObject, configuration } from '~config/configuration';
import { dataSource } from '~config/data-source';

export async function buildTestApplication(
Expand Down Expand Up @@ -44,15 +43,9 @@ export async function buildTestApplication(
],
}).compile();
const app = module.createNestApplication();
const config = app.get(ConfigService);
const config = app.get<ConfigService<ConfigObject>>(ConfigService);

app.useGlobalPipes(
new ValidationPipe({
transform: true,
whitelist: true,
errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
}),
);
app.useGlobalPipes(new ValidationPipe(config.get('validation')));
app.use(cookieParser(config.get('secret', 's€cr3to')));
useContainer(module, { fallbackOnErrors: true });

Expand Down
6 changes: 6 additions & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type ValidationPipeOptions, HttpStatus } from '@nestjs/common';
import invariant from 'tiny-invariant';

export type ConfigObject = ReturnType<typeof configuration>;
Expand All @@ -8,5 +9,10 @@ export function configuration() {
return {
port: Number.parseInt(process.env.PORT, 10) || 3000,
secret: process.env.SECRET,
validation: {
transform: true,
whitelist: true,
errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
} as ValidationPipeOptions,
};
}
10 changes: 2 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpStatus, ValidationPipe } from '@nestjs/common';
import { ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { useContainer } from 'class-validator';
Expand All @@ -11,13 +11,7 @@ async function bootstrap() {
const app = await NestFactory.create(AppModule);
const config = app.get<ConfigService<ConfigObject, true>>(ConfigService);

app.useGlobalPipes(
new ValidationPipe({
transform: true,
whitelist: true,
errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
}),
);
app.useGlobalPipes(new ValidationPipe(config.get('validation')));
app.use(cookieParser(config.getOrThrow('secret')));
useContainer(app.select(AppModule), { fallbackOnErrors: true });

Expand Down

0 comments on commit 2250718

Please sign in to comment.