Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: class-validator & class-transformer 적용 #50

Merged
merged 2 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions server/src/common/decorator/id.validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
registerDecorator,
ValidationOptions,
ValidatorConstraint,
ValidatorConstraintInterface,
} from "class-validator";

@ValidatorConstraint({ name: "isValidId", async: false })
class isValidIdConstraint implements ValidatorConstraintInterface {
public validate(value: string) {
return (
typeof value === "string" && value.length >= 6 && value.length <= 20 && value.search(/^[a-zA-Z0-9]*$/) >= 0
);
}

public defaultMessage(): string {
return `user id must be between 6 and 20 character long, only letters and numbers allowed`;
}
}

export function IsValidId(validationOptions?: ValidationOptions) {
return function (object: any, propertyName: string) {
registerDecorator({
name: "isValidId",
target: object.constructor,
propertyName: propertyName,
constraints: [],
options: validationOptions,
validator: isValidIdConstraint,
});
};
}
4 changes: 4 additions & 0 deletions server/src/common/decorator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { IsValidId } from "./id.validator";
import { IsValidPassword } from "./pw.validator";

export { IsValidId, IsValidPassword };
30 changes: 30 additions & 0 deletions server/src/common/decorator/pw.validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
registerDecorator,
ValidationOptions,
ValidatorConstraint,
ValidatorConstraintInterface,
} from "class-validator";

@ValidatorConstraint({ name: "isValidPassword", async: false })
class isValidPwConstraint implements ValidatorConstraintInterface {
public validate(value: string) {
return typeof value === "string" && value.length >= 10 && value.length <= 100;
}

public defaultMessage(): string {
return `user password must be between 10 and 100 character long`;
}
}

export function IsValidPassword(validationOptions?: ValidationOptions) {
return function (object: any, propertyName: string) {
registerDecorator({
name: "isValidPassword",
target: object.constructor,
propertyName: propertyName,
constraints: [],
options: validationOptions,
validator: isValidPwConstraint,
});
};
}
24 changes: 0 additions & 24 deletions server/src/common/decorator/validator.ts

This file was deleted.

7 changes: 7 additions & 0 deletions server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NestFactory } from "@nestjs/core";
import { SwaggerModule, DocumentBuilder } from "@nestjs/swagger";
import { AppModule } from "./app.module";
import * as cookieParser from "cookie-parser";
import { ValidationPipe } from "@nestjs/common";

async function bootstrap() {
const app = await NestFactory.create(AppModule);
Expand All @@ -14,6 +15,12 @@ async function bootstrap() {
const document = SwaggerModule.createDocument(app, options);
app.enableCors({ origin: true, methods: "GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS", credentials: true });
app.use(cookieParser());
app.useGlobalPipes(
new ValidationPipe({
forbidUnknownValues: true,
transform: true,
}),
);
SwaggerModule.setup("api", app, document);

await app.listen(4000);
Expand Down
14 changes: 11 additions & 3 deletions server/src/user/dto/create-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { IsString, MinLength } from "class-validator";
import { Type } from "class-transformer";
import { IsNumber, IsNumberString } from "class-validator";
import { IsValidId, IsValidPassword } from "src/common/decorator";
import { User } from "../../entities/user.entity";

export class CreateUserDto {
@IsString()
@MinLength(6)
@IsValidId()
private userId: string;

@IsValidPassword()
private password: string;

@Type(() => Number)
@IsNumber()
private pace: number;

@IsNumberString()
private zipCode: string;

getUserId() {
Expand Down
1 change: 0 additions & 1 deletion server/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export class UserController {

@Post()
async create(@Body() createUserDto: CreateUserDto) {
createUserDto = plainToClass(CreateUserDto, createUserDto);
return this.userService.create(createUserDto);
}
}