forked from earthkingman/42Swim
-
Notifications
You must be signed in to change notification settings - Fork 1
Validation
๋ฐ์ง์จ edited this page Oct 16, 2021
·
2 revisions
์์ฑ์ | ์์ฑ์ผ์ |
---|---|
earthkingman | 2021-10-16 |
-
๋ชจ๋ ์ค์น npm install class-validator --save
-
DTO ํ์ผ ์์ฑ
import { Min, Max, Length } from "class-validator";
export class Question {
@Length(1, 20)
private readonly title: string;
@Length(1, 5000)
private readonly text: string;
}
- ์ ํจ์ฑ ๊ฒ์ฌ ๋ฏธ๋ค์จ์ด CreatePostDto ํด๋์ค์์ ๋ชจ๋ ํ๋๊ฐ ํ์์ด๋ฉฐ ๋๋จธ์ง ์์ฑ์ ์ ๋ฌํ์ง ์๊ณ ์ผ๋ถ ์์ฑ๋ง ์ ๋ฐ์ดํธํ ์ ์๋ HTTP PATCH๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. skipMissingProperties ์ต์ ์ ์ฌ์ฉํ๋ฉด ์ํฉ์ ์๋ง๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.
import { plainToClass } from 'class-transformer';
import { validate, ValidationError } from 'class-validator';
import * as express from 'express';
import { HttpException } from '../exception/http_exception';
export function validationMiddleware<T>(type: any, skipMissingProperties = false): express.RequestHandler {
return (req, res, next) => {
console.log('-----validator------')
console.log(req.body);
validate(plainToClass(type, req.body), { skipMissingProperties })
.then((errors: ValidationError[]) => {
if (errors.length > 0) {
const message = errors.map((error: ValidationError) => {
return Object.values(error.constraints)
}).join(', ');
next(new HttpException(400, message))
} else
next();
})
}
}
- ๋ผ์ฐํฐ class-validator ํจํค์ง๋ฅผ ์ฌ์ฉํ์ฌ POST ๋ฐ PATCH ์ฒ๋ฆฌ๊ธฐ์์ ๋ค์ด์ค๋ ๋ฐ์ดํฐ์ ์ ํจ์ฑ์ ๊ฒ์ฌ
import express from "express";
import { s3Util } from "../aws/s3_utils";
import { authJwt } from "../middlewares/auth.middleware";
import { QuestionController } from "../controllers/question"
import { AnswerController } from "../controllers/answer";
import { CommentController } from "../controllers/comment";
import { LikeController } from "../controllers/like";
import { Question } from "../controllers/dto/question"
import { Answer } from "../controllers/dto/answer"
import { Comment } from "../controllers/dto/comment"
import { validationMiddleware } from "../middlewares/validation.middleware"
export const postRouter = express.Router();
postRouter.delete('/answer', authJwt, s3Util.s3DeletePhoto, AnswerController.deleteAnswer)
postRouter.post('/answer', authJwt, s3Util.s3ImageUpload({ folder: 'author' }).array("imgFile"), validationMiddleware(Answer, true), AnswerController.uploadAnswer)
postRouter.patch('/answer', authJwt, s3Util.s3ImageUpload({ folder: 'author' }).array("imgFile"), validationMiddleware(Answer), AnswerController.updateAnswer)
postRouter.delete('/question', authJwt, s3Util.s3DeletePhoto, QuestionController.deleteQuestion)
postRouter.post('/question', authJwt, s3Util.s3ImageUpload({ folder: 'author' }).array("imgFile"), validationMiddleware(Question, true), QuestionController.uploadQuestion)
postRouter.patch('/question', authJwt, s3Util.s3ImageUpload({ folder: 'author' }).array("imgFile"), validationMiddleware(Question), QuestionController.updateQuestion)
postRouter.delete('/comment', authJwt, CommentController.deleteComment)
postRouter.post('/comment', authJwt, validationMiddleware(Comment, true), CommentController.uploadComment)
postRouter.patch('/comment', authJwt, validationMiddleware(Comment), CommentController.updateComment)
https://wanago.io/2018/12/17/typescript-express-error-handling-validation/