Skip to content

Commit

Permalink
Refactor: 기한별 습관 조회 api 쿼리 validation 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hamsterster committed Sep 25, 2023
1 parent 3b109c3 commit 5796c67
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/middlewares/validateHabit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { body, param } = require('express-validator');
const { body, param, query } = require('express-validator');
const { ERRORS } = require('../lib/ERRORS');

const isHabitId = () =>
Expand All @@ -7,6 +7,24 @@ const isHabitId = () =>
const isUserId = () =>
param('userId').isMongoId().withMessage(ERRORS.INVALID_MONGO_ID.MESSAGE);

const isStartDateQuery = () =>
query('startDate')
.matches(/^\d{4}-\d{2}-\d{2}$/)
.withMessage(ERRORS.INVALID_HABIT_START_DATE_FORMAT)
.optional();

const isEndDateQuery = () =>
query('endDate')
.matches(/^\d{4}-\d{2}-\d{2}$/)
.withMessage(ERRORS.INVALID_HABIT_END_DATE_FORMAT)
.optional()
.custom((value, { req }) => {
if (req.query.startDate && value < req.query.startDate) {
throw new Error(ERRORS.HABIT_END_DATE_BEFORE_START);
}
return true;
});

const isHabitTitle = (isRequired = true) =>
body('habitTitle')
.optional(!isRequired)
Expand All @@ -29,7 +47,7 @@ const isHabitStartDate = (isRequired = true) =>
.matches(/^\d{4}-\d{2}-\d{2}$/)
.withMessage(ERRORS.INVALID_HABIT_START_DATE_FORMAT);

const ishabitEndDate = (isRequired = true) =>
const isHabitEndDate = (isRequired = true) =>
body('habitEndDate')
.optional(!isRequired)
.matches(/^\d{4}-\d{2}-\d{2}$/)
Expand Down Expand Up @@ -117,7 +135,7 @@ const postRequest = [
isHabitTitle(),
isHabitContent(),
isHabitStartDate(false),
ishabitEndDate(false),
isHabitEndDate(false),
isDoDay(),
isStartTime(),
isEndTime(),
Expand All @@ -132,7 +150,7 @@ const patchRequest = [
isHabitTitle(false),
isHabitContent(false),
isHabitStartDate(false),
ishabitEndDate(false),
isHabitEndDate(false),
isDoDay(false),
isStartTime(false),
isEndTime(false),
Expand All @@ -144,7 +162,11 @@ const patchRequest = [

const deleteRequest = [isHabitId()];

const getPeriodicHabitsByUserIdRequest = [isUserId()];
const getPeriodicHabitsByUserIdRequest = [
isUserId(),
isStartDateQuery(),
isEndDateQuery(),
];

const subscribeHabitRequest = [isHabitId(), isWatcherId()];

Expand Down

0 comments on commit 5796c67

Please sign in to comment.