Skip to content

Commit

Permalink
feat : dto validation 구현 #35
Browse files Browse the repository at this point in the history
- 핸들러 레벨 파이프 적용
- dto에 decorator를 통해 validation 규칙 적용

Co-authored-by: GeunH <[email protected]>
  • Loading branch information
LeeTH916 and GeunH committed Nov 15, 2023
1 parent 7e92e84 commit 7693d88
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 27 deletions.
44 changes: 37 additions & 7 deletions be/src/user/dto/userInfo.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
import {
IsBoolean,
IsString,
IsNotEmpty,
IsEmail,
IsInt,
MaxLength,
} from "class-validator";

export class UserInfoDto {
email : string;
password : string;
provider : string;
nickName: string;
age: number;
gender: string;
}
@IsEmail()
@IsNotEmpty()
@MaxLength(50)
email: string;

@IsString()
@IsNotEmpty()
@MaxLength(50)
password: string;

@IsString()
@IsNotEmpty()
@MaxLength(20)
provider: string;

@IsString()
@IsNotEmpty()
@MaxLength(20)
nickName: string;

@IsInt()
@IsNotEmpty()
age: number;

@IsBoolean()
@IsNotEmpty()
gender: boolean;
}
19 changes: 13 additions & 6 deletions be/src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { Controller, Get, Post, Body, UsePipes } from "@nestjs/common";
import {
Body,
Controller,
Get,
Post,
UsePipes,
ValidationPipe,
} from "@nestjs/common";
import { UserInfoDto } from "./dto/userInfo.dto";
import { UserService } from "./user.service";
import { validateHeaderName } from "http";

@Controller("user")
export class UserController {
constructor(private userService:UserService) {}
constructor(private userService: UserService) {}

@Post()
singup(@Body() userInfoDto : UserInfoDto) {
@UsePipes(new ValidationPipe())
singup(@Body() userInfoDto: UserInfoDto) {
try {
const result = this.userService.signup(userInfoDto);
return { message: 'User created successfully', data: result };
return { message: "User created successfully", data: result };
} catch (error) {
return { message: 'User creation failed', error: error.message };
return { message: "User creation failed", error: error.message };
}
}
}
2 changes: 1 addition & 1 deletion be/src/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Module } from "@nestjs/common";
import { UserController } from "./user.controller";
import { UserService } from './user.service';
import { UserService } from "./user.service";

@Module({
controllers: [UserController],
Expand Down
8 changes: 4 additions & 4 deletions be/src/user/user.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UserService } from './user.service';
import { Test, TestingModule } from "@nestjs/testing";
import { UserService } from "./user.service";

describe('UserService', () => {
describe("UserService", () => {
let service: UserService;

beforeEach(async () => {
Expand All @@ -12,7 +12,7 @@ describe('UserService', () => {
service = module.get<UserService>(UserService);
});

it('should be defined', () => {
it("should be defined", () => {
expect(service).toBeDefined();
});
});
17 changes: 8 additions & 9 deletions be/src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Injectable } from '@nestjs/common';
import { UserInfoDto } from './dto/userInfo.dto';
import { Injectable } from "@nestjs/common";
import { UserInfoDto } from "./dto/userInfo.dto";
@Injectable()
export class UserService {
signup( userInfoDto : UserInfoDto){
const { email, password, provider, nickName, age, gender } = userInfoDto;
signup(userInfoDto: UserInfoDto) {
const { email, password, provider, nickName, age, gender } = userInfoDto;

// DB에 저장

return userInfoDto;

}
// DB에 저장

return userInfoDto;
}
}

0 comments on commit 7693d88

Please sign in to comment.