Skip to content

Commit

Permalink
feat: Add class transform
Browse files Browse the repository at this point in the history
  • Loading branch information
Rowan-Paul committed Jul 12, 2024
1 parent c31a761 commit f305e13
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 25 deletions.
2 changes: 2 additions & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"@nestjs/platform-express": "^10.0.0",
"@nestjs/platform-fastify": "^10.3.10",
"@nestjs/swagger": "^7.4.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"drizzle-orm": "^0.32.0",
"pg": "^8.12.0",
"reflect-metadata": "^0.2.0",
Expand Down
24 changes: 17 additions & 7 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,30 @@ import {
} from '@nestjs/platform-fastify';

import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';

async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter(),
);

const config = new DocumentBuilder()
.setTitle('Tracktr')
.setDescription('The Tracktr API description')
.setVersion('0.1')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('', app, document, { jsonDocumentUrl: 'json' });
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
}),
);

if (process.env.NODE_ENV !== 'development') {
const config = new DocumentBuilder()
.setTitle('Tracktr')
.setDescription('The Tracktr API description')
.setVersion('0.1')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('', app, document, { jsonDocumentUrl: 'json' });
}

await app.listen(3000);
}
Expand Down
13 changes: 11 additions & 2 deletions apps/api/src/movies/dto/create-movies.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { IsNotEmpty, IsString, IsOptional, IsNumber } from 'class-validator';

export class CreateMoviesDto {
id: number;
@IsNotEmpty()
@IsString()
title: string;

@IsOptional()
@IsString()
poster: string;
year: number;

@IsOptional()
@IsNumber()
year?: number;
}
14 changes: 12 additions & 2 deletions apps/api/src/movies/dto/update-movies.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { IsNumber, IsOptional, IsString } from 'class-validator';

export class UpdateMoviesDto {
title?: string;
poster?: string;
@IsOptional()
@IsString()
title: string;

@IsOptional()
@IsString()
poster: string;

@IsOptional()
@IsNumber()
year?: number;
}
60 changes: 46 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f305e13

Please sign in to comment.