Skip to content

Commit 3706b11

Browse files
committed
Add character limit validation for question description
1 parent b9db564 commit 3706b11

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

backend/question-service/src/controllers/questionController.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { Request, Response } from "express";
22
import Question from "../models/Question.ts";
33
import { checkIsExistingQuestion } from "../utils/utils.ts";
4-
import { DUPLICATE_QUESTION_RESPONSE_MESSAGE } from "../utils/constants.ts";
4+
import {
5+
DUPLICATE_QUESTION_RESPONSE_MESSAGE,
6+
QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE,
7+
QN_DESC_CHAR_LIMIT,
8+
} from "../utils/constants.ts";
59

610
export const createQuestion = async (
711
req: Request,
@@ -18,6 +22,13 @@ export const createQuestion = async (
1822
return;
1923
}
2024

25+
if (description.length > QN_DESC_CHAR_LIMIT) {
26+
res.status(400).json({
27+
message: QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE,
28+
});
29+
return;
30+
}
31+
2132
const newQuestion = new Question({
2233
title,
2334
description,
@@ -42,7 +53,7 @@ export const updateQuestion = async (
4253
): Promise<void> => {
4354
try {
4455
const { id } = req.params;
45-
const { title } = req.body;
56+
const { title, description } = req.body;
4657

4758
const currentQuestion = await Question.findById(id);
4859
if (!currentQuestion) {
@@ -58,6 +69,13 @@ export const updateQuestion = async (
5869
return;
5970
}
6071

72+
if (description && description.length > QN_DESC_CHAR_LIMIT) {
73+
res.status(400).json({
74+
message: QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE,
75+
});
76+
return;
77+
}
78+
6179
const updatedQuestion = await Question.findByIdAndUpdate(id, req.body, {
6280
new: true,
6381
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
export const QN_DESC_CHAR_LIMIT = 6000;
2+
3+
export const QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE =
4+
"Question description must be at most 6000 characters";
5+
16
export const DUPLICATE_QUESTION_RESPONSE_MESSAGE =
27
"Duplicate question: A question with the same title already exists.";

0 commit comments

Comments
 (0)