diff --git a/src/modules/patterns.js b/src/modules/patterns.js index 2fd1e4dc..0d526dd8 100644 --- a/src/modules/patterns.js +++ b/src/modules/patterns.js @@ -15,5 +15,7 @@ module.exports = { chat: { chatImgType: RegExp("^(image/png|image/jpg|image/jpeg)$"), chatSendType: RegExp("^(text|account)$"), + chatContent: RegExp("^\\s{0,}\\S{1}[\\s\\S]{0,}$"), // 왼쪽 공백 제외 최소 1개 문자 + chatContentLength: RegExp("^[\\s\\S]{1,140}$"), // 공백 포함 최대 140문자 }, }; diff --git a/src/routes/chats.js b/src/routes/chats.js index f689348c..f20de503 100644 --- a/src/routes/chats.js +++ b/src/routes/chats.js @@ -1,6 +1,8 @@ const express = require("express"); const { body } = require("express-validator"); const validator = require("../middlewares/validator"); +const { validateBody } = require("../middlewares/zod"); +const { chatsZod } = require("./docs/schemas/chatsSchema"); const patterns = require("../modules/patterns"); const router = express.Router(); @@ -47,10 +49,7 @@ router.post( */ router.post( "/send", - body("roomId").isMongoId(), - body("type").matches(patterns.chat.chatSendType), - body("content").isString(), - validator, + validateBody(chatsZod.sendChatHandler), chatsHandlers.sendChatHandler ); diff --git a/src/routes/docs/schemas/chatsSchema.js b/src/routes/docs/schemas/chatsSchema.js new file mode 100644 index 00000000..fa0ae257 --- /dev/null +++ b/src/routes/docs/schemas/chatsSchema.js @@ -0,0 +1,15 @@ +const { z } = require("zod"); +const { zodToSchemaObject } = require("../utils"); +const { objectId, chat } = require("../../../modules/patterns"); + +const chatsZod = { + sendChatHandler: z.object({ + roomId: z.string().regex(objectId), + type: z.string().regex(chat.chatSendType), + content: z.string().regex(chat.chatContent).regex(chat.chatContentLength), + }), +}; + +const chatsSchema = zodToSchemaObject(chatsZod); + +module.exports = { chatsZod, chatsSchema }; diff --git a/src/routes/docs/swaggerDocs.js b/src/routes/docs/swaggerDocs.js index 95fd8982..e1f1090b 100644 --- a/src/routes/docs/swaggerDocs.js +++ b/src/routes/docs/swaggerDocs.js @@ -1,6 +1,7 @@ const { reportsSchema } = require("./schemas/reportsSchema"); const { roomsSchema } = require("./schemas/roomsSchema"); const { fareSchema } = require("./schemas/fareSchema"); +const { chatsSchema } = require("./schemas/chatsSchema"); const reportsDocs = require("./reports"); const logininfoDocs = require("./logininfo"); const locationsDocs = require("./locations"); @@ -93,6 +94,7 @@ const swaggerDocs = { ...reportsSchema, ...roomsSchema, ...fareSchema, + ...chatsSchema, }, }, };