Skip to content

Commit 15f246a

Browse files
authored
Merge pull request #43 from GeneralMagicio/check-type
added check and validations using class-transformer
2 parents 02fc3fa + aa0ef21 commit 15f246a

File tree

7 files changed

+36
-8
lines changed

7 files changed

+36
-8
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@nestjs/platform-express": "^11.0.1",
3030
"@prisma/client": "^6.5.0",
3131
"@worldcoin/minikit-js": "^1.8.0",
32+
"class-transformer": "^0.5.1",
3233
"class-validator": "^0.14.1",
3334
"cookie-parser": "^1.4.7",
3435
"crypto": "^1.0.1",
@@ -47,6 +48,7 @@
4748
"@nestjs/testing": "^11.0.1",
4849
"@swc/cli": "^0.6.0",
4950
"@swc/core": "^1.10.7",
51+
"@types/cookie-parser": "^1.4.8",
5052
"@types/express": "^5.0.0",
5153
"@types/jest": "^29.5.14",
5254
"@types/node": "^22.10.7",

src/main.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import { NestFactory } from '@nestjs/core';
22
import { AppModule } from './app.module';
3-
import * as cookieParser from 'cookie-parser';
3+
import cookieParser from 'cookie-parser';
4+
import { ValidationPipe } from '@nestjs/common';
45

56
async function bootstrap() {
67
const app = await NestFactory.create(AppModule);
8+
9+
app.useGlobalPipes(
10+
new ValidationPipe({
11+
transform: true,
12+
}),
13+
);
714
app.use(cookieParser());
815

916
await app.listen(process.env.PORT ?? 3000);

src/poll/Poll.dto.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Transform, Type } from 'class-transformer';
12
import {
23
IsArray,
34
IsBoolean,
@@ -16,8 +17,8 @@ export class CreatePollDto {
1617
title: string;
1718

1819
@IsString()
19-
@IsOptional()
20-
description?: string;
20+
@IsNotEmpty()
21+
description: string;
2122

2223
@IsArray()
2324
@IsString({ each: true })
@@ -34,8 +35,8 @@ export class CreatePollDto {
3435

3536
@IsArray()
3637
@IsString({ each: true })
37-
@IsOptional()
38-
tags?: string[];
38+
@IsNotEmpty()
39+
tags: string[];
3940

4041
@IsBoolean()
4142
@IsOptional()
@@ -55,14 +56,17 @@ export class GetPollsDto {
5556

5657
@IsOptional()
5758
@IsBoolean()
59+
@Transform(({ value }) => value === 'true')
5860
isActive?: boolean;
5961

6062
@IsOptional()
6163
@IsBoolean()
64+
@Transform(({ value }) => value === 'true')
6265
userVoted?: boolean;
6366

6467
@IsOptional()
6568
@IsBoolean()
69+
@Transform(({ value }) => value === 'true')
6670
userCreated?: boolean;
6771

6872
@IsOptional()

src/poll/poll.controller.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
Query,
99
Res,
1010
Delete,
11+
UsePipes,
12+
ValidationPipe,
1113
} from '@nestjs/common';
1214
import { Response } from 'express';
1315
import { PollService } from './poll.service';
@@ -18,12 +20,14 @@ export class PollController {
1820
constructor(private readonly pollService: PollService) {}
1921

2022
@Post()
23+
@UsePipes(ValidationPipe)
2124
create(@Body() createPollDto: CreatePollDto) {
2225
const userId = 1; // need to implement Auth
2326
return this.pollService.createPoll(userId, createPollDto);
2427
}
2528

2629
@Get()
30+
@UsePipes(ValidationPipe)
2731
getPolls(@Req() req, @Query() query: GetPollsDto) {
2832
const userId = 1;
2933
return this.pollService.getPolls(userId, query);

src/poll/poll.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,17 @@ export class PollService {
7878
const skip = (page - 1) * limit;
7979
const now = new Date();
8080
const filters: any = {};
81-
if (isActive === true) {
81+
if (isActive) {
8282
filters.startDate = { lte: now };
8383
filters.endDate = { gt: now };
8484
}
85-
if (userCreated === true) {
85+
if (userCreated) {
8686
filters.authorUserId = userId;
8787
}
8888

8989
// Get polls user voted in
9090
let votedPollIds: number[] = [];
91-
if (userVoted === true) {
91+
if (userVoted) {
9292
const userVotes = await this.databaseService.vote.findMany({
9393
where: { userId },
9494
select: { pollId: true },

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"emitDecoratorMetadata": true,
77
"experimentalDecorators": true,
88
"allowSyntheticDefaultImports": true,
9+
"esModuleInterop": true,
910
"target": "ES2021",
1011
"sourceMap": true,
1112
"outDir": "./dist",

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,11 @@
14911491
dependencies:
14921492
"@types/node" "*"
14931493

1494+
"@types/cookie-parser@^1.4.8":
1495+
version "1.4.8"
1496+
resolved "https://registry.yarnpkg.com/@types/cookie-parser/-/cookie-parser-1.4.8.tgz#d2215e7915f624fbfe4233da8f063f511679f1f3"
1497+
integrity sha512-l37JqFrOJ9yQfRQkljb41l0xVphc7kg5JTjjr+pLRZ0IyZ49V4BQ8vbF4Ut2C2e+WH4al3xD3ZwYwIUfnbT4NQ==
1498+
14941499
"@types/cookiejar@^2.1.5":
14951500
version "2.1.5"
14961501
resolved "https://registry.yarnpkg.com/@types/cookiejar/-/cookiejar-2.1.5.tgz#14a3e83fa641beb169a2dd8422d91c3c345a9a78"
@@ -2515,6 +2520,11 @@ cjs-module-lexer@^1.0.0:
25152520
resolved "https://registry.yarnpkg.com/cjs-module-lexer/-/cjs-module-lexer-1.4.3.tgz#0f79731eb8cfe1ec72acd4066efac9d61991b00d"
25162521
integrity sha512-9z8TZaGM1pfswYeXrUpzPrkx8UnWYdhJclsiYMm6x/w5+nN+8Tf/LnAgfLGQCm59qAOxU8WwHEq2vNwF6i4j+Q==
25172522

2523+
class-transformer@^0.5.1:
2524+
version "0.5.1"
2525+
resolved "https://registry.yarnpkg.com/class-transformer/-/class-transformer-0.5.1.tgz#24147d5dffd2a6cea930a3250a677addf96ab336"
2526+
integrity sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw==
2527+
25182528
class-validator@^0.14.1:
25192529
version "0.14.1"
25202530
resolved "https://registry.yarnpkg.com/class-validator/-/class-validator-0.14.1.tgz#ff2411ed8134e9d76acfeb14872884448be98110"

0 commit comments

Comments
 (0)