diff --git a/package-lock.json b/package-lock.json index d1809df..665a613 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,8 @@ "@nestjs/core": "^10.0.0", "@nestjs/platform-express": "^10.0.0", "@nestjs/typeorm": "^10.0.2", + "class-transformer": "^0.5.1", + "class-validator": "^0.14.1", "pg": "^8.12.0", "reflect-metadata": "^0.1.13", "rxjs": "^7.8.1", @@ -2122,6 +2124,11 @@ "@types/superagent": "^8.1.0" } }, + "node_modules/@types/validator": { + "version": "13.12.1", + "resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.12.1.tgz", + "integrity": "sha512-w0URwf7BQb0rD/EuiG12KP0bailHKHP5YVviJG9zw3ykAokL0TuxU2TUqMB7EwZ59bDHYdeTIvjI5m0S7qHfOA==" + }, "node_modules/@types/yargs": { "version": "17.0.33", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.33.tgz", @@ -3206,6 +3213,21 @@ "integrity": "sha512-N1NGmowPlGBLsOZLPvm48StN04V4YvQRL0i6b7ctrVY3epjP/ct7hFLOItz6pDIvRjwpfPxi52a2UWV2ziir8g==", "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.14.1", + "resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.14.1.tgz", + "integrity": "sha512-2VEG9JICxIqTpoK1eMzZqaV+u/EiwEJkMGzTrZf6sU/fwsnOITVgYJ8yojSy6CaXtO9V0Cc6ZQZ8h8m4UBuLwQ==", + "dependencies": { + "@types/validator": "^13.11.8", + "libphonenumber-js": "^1.10.53", + "validator": "^13.9.0" + } + }, "node_modules/cli-cursor": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-3.1.0.tgz", @@ -6124,6 +6146,11 @@ "node": ">= 0.8.0" } }, + "node_modules/libphonenumber-js": { + "version": "1.11.8", + "resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.11.8.tgz", + "integrity": "sha512-0fv/YKpJBAgXKy0kaS3fnqoUVN8901vUYAKIGD/MWZaDfhJt1nZjPL3ZzdZBt/G8G8Hw2J1xOIrXWdNHFHPAvg==" + }, "node_modules/lines-and-columns": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz", @@ -8711,6 +8738,14 @@ "node": ">=10.12.0" } }, + "node_modules/validator": { + "version": "13.12.0", + "resolved": "https://registry.npmjs.org/validator/-/validator-13.12.0.tgz", + "integrity": "sha512-c1Q0mCiPlgdTVVVIJIrBuxNicYE+t/7oKeI9MWLj3fh/uq2Pxh/3eeWbVZ4OcGW1TUf53At0njHw5SMdA3tmMg==", + "engines": { + "node": ">= 0.10" + } + }, "node_modules/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 2db9eeb..25e7980 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,8 @@ "@nestjs/core": "^10.0.0", "@nestjs/platform-express": "^10.0.0", "@nestjs/typeorm": "^10.0.2", + "class-transformer": "^0.5.1", + "class-validator": "^0.14.1", "pg": "^8.12.0", "reflect-metadata": "^0.1.13", "rxjs": "^7.8.1", diff --git a/src/random-quote/quote.entity.ts b/src/random-quote/quote.entity.ts index 4faaa3b..dd761a3 100644 --- a/src/random-quote/quote.entity.ts +++ b/src/random-quote/quote.entity.ts @@ -1,4 +1,5 @@ import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; +import { IsNotEmpty } from 'class-validator'; @Entity() export class Quote { @@ -6,9 +7,11 @@ export class Quote { id: number; @Column() + @IsNotEmpty() content: string; @Column() + @IsNotEmpty() author: string; @Column({ nullable: true }) diff --git a/src/random-quote/random-quote.controller.ts b/src/random-quote/random-quote.controller.ts index 77ef1fc..c6018f2 100644 --- a/src/random-quote/random-quote.controller.ts +++ b/src/random-quote/random-quote.controller.ts @@ -6,10 +6,12 @@ import { Quote } from './quote.entity'; export class RandomQuoteController { constructor(private readonly randomQuoteService: RandomQuoteService) {} - @Post() - create(@Body() quote: Partial): Promise { - return this.randomQuoteService.create(quote); + async create(@Body() quote: Partial): Promise { + if (!quote.content || !quote.author) { + throw new Error('Content and author are required.'); + } + return this.randomQuoteService.create(quote); } @Get()