From 57765aa7592baee5b7af33f66252f7e093d92f61 Mon Sep 17 00:00:00 2001 From: sirkenedy Date: Mon, 10 Jan 2022 02:37:20 +0100 Subject: [PATCH] [IMP] class validator --- package-lock.json | 53 +++++++++++++++++++++ package.json | 2 + src/components/books/dto/create-book.dto.ts | 14 ++++++ src/components/users/dto/create-user.dto.ts | 7 +++ src/main.ts | 5 ++ 5 files changed, 81 insertions(+) diff --git a/package-lock.json b/package-lock.json index 5c779dd..62ec0fb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,8 @@ "@nestjs/platform-express": "^8.0.0", "@nestjs/typeorm": "^8.0.2", "bcrypt": "^5.0.1", + "class-transformer": "^0.5.1", + "class-validator": "^0.13.2", "mysql2": "^2.3.3", "passport": "^0.4.1", "passport-jwt": "^4.0.0", @@ -3386,6 +3388,20 @@ "integrity": "sha512-cOU9usZw8/dXIXKtwa8pM0OTJQuJkxMN6w30csNRUerHfeQ5R6U3kkU/FtJeIf3M202OHfY2U8ccInBG7/xogA==", "dev": true }, + "node_modules/class-transformer": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/class-transformer/-/class-transformer-0.5.1.tgz", + "integrity": "sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw==" + }, + "node_modules/class-validator": { + "version": "0.13.2", + "resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.13.2.tgz", + "integrity": "sha512-yBUcQy07FPlGzUjoLuUfIOXzgynnQPPruyK1Ge2B74k9ROwnle1E+NxLWnUv5OLU8hA/qL5leAE9XnXq3byaBw==", + "dependencies": { + "libphonenumber-js": "^1.9.43", + "validator": "^13.7.0" + } + }, "node_modules/cli-boxes": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/cli-boxes/-/cli-boxes-2.2.1.tgz", @@ -7027,6 +7043,11 @@ "node": ">= 0.8.0" } }, + "node_modules/libphonenumber-js": { + "version": "1.9.44", + "resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.9.44.tgz", + "integrity": "sha512-zhw8nUMJuQf7jG1dZfEOKKOS6M3QYIv3HnvB/vGohNd0QfxIQcObH3a6Y6s350H+9xgBeOXClOJkS0hJ0yvS3g==" + }, "node_modules/lines-and-columns": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz", @@ -10082,6 +10103,14 @@ "node": ">=10.12.0" } }, + "node_modules/validator": { + "version": "13.7.0", + "resolved": "https://registry.npmjs.org/validator/-/validator-13.7.0.tgz", + "integrity": "sha512-nYXQLCBkpJ8X6ltALua9dRrZDHVYxjJ1wgskNt1lH9fzGjs3tgojGSCBjmEPwkWS1y29+DrizMTW19Pr9uB2nw==", + "engines": { + "node": ">= 0.10" + } + }, "node_modules/vary": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", @@ -13125,6 +13154,20 @@ "integrity": "sha512-cOU9usZw8/dXIXKtwa8pM0OTJQuJkxMN6w30csNRUerHfeQ5R6U3kkU/FtJeIf3M202OHfY2U8ccInBG7/xogA==", "dev": true }, + "class-transformer": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/class-transformer/-/class-transformer-0.5.1.tgz", + "integrity": "sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw==" + }, + "class-validator": { + "version": "0.13.2", + "resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.13.2.tgz", + "integrity": "sha512-yBUcQy07FPlGzUjoLuUfIOXzgynnQPPruyK1Ge2B74k9ROwnle1E+NxLWnUv5OLU8hA/qL5leAE9XnXq3byaBw==", + "requires": { + "libphonenumber-js": "^1.9.43", + "validator": "^13.7.0" + } + }, "cli-boxes": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/cli-boxes/-/cli-boxes-2.2.1.tgz", @@ -15912,6 +15955,11 @@ "type-check": "~0.4.0" } }, + "libphonenumber-js": { + "version": "1.9.44", + "resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.9.44.tgz", + "integrity": "sha512-zhw8nUMJuQf7jG1dZfEOKKOS6M3QYIv3HnvB/vGohNd0QfxIQcObH3a6Y6s350H+9xgBeOXClOJkS0hJ0yvS3g==" + }, "lines-and-columns": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz", @@ -18151,6 +18199,11 @@ "source-map": "^0.7.3" } }, + "validator": { + "version": "13.7.0", + "resolved": "https://registry.npmjs.org/validator/-/validator-13.7.0.tgz", + "integrity": "sha512-nYXQLCBkpJ8X6ltALua9dRrZDHVYxjJ1wgskNt1lH9fzGjs3tgojGSCBjmEPwkWS1y29+DrizMTW19Pr9uB2nw==" + }, "vary": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", diff --git a/package.json b/package.json index ca61d58..9efcd75 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,8 @@ "@nestjs/platform-express": "^8.0.0", "@nestjs/typeorm": "^8.0.2", "bcrypt": "^5.0.1", + "class-transformer": "^0.5.1", + "class-validator": "^0.13.2", "mysql2": "^2.3.3", "passport": "^0.4.1", "passport-jwt": "^4.0.0", diff --git a/src/components/books/dto/create-book.dto.ts b/src/components/books/dto/create-book.dto.ts index 7b0cfc8..37a3781 100644 --- a/src/components/books/dto/create-book.dto.ts +++ b/src/components/books/dto/create-book.dto.ts @@ -1,8 +1,22 @@ import { Users as User } from '../../users/entities/user.entity'; +import { IsNotEmpty, IsString, isArray } from 'class-validator'; + export class CreateBookDto { + @IsNotEmpty({"message" : "Book title cannot be empty"}) + @IsString({"message" : "Book title must be of type string"}) title: string; + + @IsNotEmpty({"message" : "Book description cannot be empty"}) + @IsString({"message" : "Book desciption must be of type string"}) description: number; + + @IsNotEmpty({"message" : "Book thumnail cannot be empty"}) + @IsString({"message" : "Book thumbnail must be of type string"}) //@isUrl can be used to verify that it is a valid url thumbnail: string; + + @IsNotEmpty({"message" : "Book author cannot be empty"}) author: string[]; + + @IsNotEmpty({"message" : "Seems like something went wrong! please try again"}) user : User } \ No newline at end of file diff --git a/src/components/users/dto/create-user.dto.ts b/src/components/users/dto/create-user.dto.ts index 1b3bd80..b8b458d 100644 --- a/src/components/users/dto/create-user.dto.ts +++ b/src/components/users/dto/create-user.dto.ts @@ -1,5 +1,12 @@ +import { IsNotEmpty, IsEmail } from 'class-validator'; + export class CreateUserDto { + @IsNotEmpty({"message" : "Name field cannot be empty"}) name: string; + + @IsEmail({"message" : "Enter a valid email adress"}) email: number; + + @IsNotEmpty({"message" : "Password field cannot be empty"}) password: string; } diff --git a/src/main.ts b/src/main.ts index 13cad38..7c979ff 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,8 +1,13 @@ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; +import { ValidationPipe } from '@nestjs/common'; async function bootstrap() { const app = await NestFactory.create(AppModule); + app.useGlobalPipes(new ValidationPipe({ + whitelist: true, + transform: true, + })); await app.listen(3000); } bootstrap();