Skip to content

Commit

Permalink
Merge pull request #50 from boostcampwm-2022/feat/validation
Browse files Browse the repository at this point in the history
feat: class-validator & class-transformer ์ ์šฉ
  • Loading branch information
pushedrumex authored Nov 21, 2022
2 parents e7f5f15 + f4dfdad commit 28d37c9
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 28 deletions.
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);
}
}

0 comments on commit 28d37c9

Please sign in to comment.