Skip to content

Commit

Permalink
Add Class Validator
Browse files Browse the repository at this point in the history
  • Loading branch information
brubluenel committed Sep 12, 2024
1 parent e82270e commit 51a2e0a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
35 changes: 35 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/random-quote/quote.entity.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
import { IsNotEmpty } from 'class-validator';

@Entity()
export class Quote {
@PrimaryGeneratedColumn()
id: number;

@Column()
@IsNotEmpty()
content: string;

@Column()
@IsNotEmpty()
author: string;

@Column({ nullable: true })
Expand Down
8 changes: 5 additions & 3 deletions src/random-quote/random-quote.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { Quote } from './quote.entity';
export class RandomQuoteController {

constructor(private readonly randomQuoteService: RandomQuoteService) {}

@Post()
create(@Body() quote: Partial<Quote>): Promise<Quote> {
return this.randomQuoteService.create(quote);
async create(@Body() quote: Partial<Quote>): Promise<Quote> {
if (!quote.content || !quote.author) {
throw new Error('Content and author are required.');
}
return this.randomQuoteService.create(quote);
}

@Get()
Expand Down

0 comments on commit 51a2e0a

Please sign in to comment.