Skip to content

Commit

Permalink
Merge pull request #12 from sirkenedy/class-validator
Browse files Browse the repository at this point in the history
[IMP] class validator
  • Loading branch information
sirkenedy authored Jan 10, 2022
2 parents e4fd46d + 57765aa commit ef84c0e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
53 changes: 53 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 @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions src/components/books/dto/create-book.dto.ts
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions src/components/users/dto/create-user.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
5 changes: 5 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit ef84c0e

Please sign in to comment.