Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 147 additions & 31 deletions backend/middleware/validation.middleware.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,181 @@
import validator from "validator";
import xss from "xss";

const VALID_TASK_STATUS = ["todo", "in_progress", "done"];
const VALID_TASK_STATUS = [
"todo",
"in_progress",
"done",
];

function sanitizeText(value = "") {
return xss(String(value).trim());
}

export function validateTask(req, res, next) {
const { title, description, status, position } = req.body;
function validateTaskStatus(
status
) {
return (
!status ||
VALID_TASK_STATUS.includes(
status
)
);
}

function validateTaskPosition(
position
) {
return (
position === undefined ||
(Number.isInteger(position) &&
position >= 0)
);
}

export function validateTask(
req,
res,
next
) {
const {
title,
description,
status,
position,
} = req.body;

if (!title || !validator.isLength(title.trim(), { min: 1, max: 120 })) {
if (
!title ||
!validator.isLength(
title.trim(),
{
min: 1,
max: 120,
}
)
) {
return res.status(400).json({
error: "Task title must be between 1 and 120 characters",
error:
"Task title must be between 1 and 120 characters",
});
}

if (
description &&
!validator.isLength(description.trim(), { max: 1000 })
!validator.isLength(
description.trim(),
{ max: 1000 }
)
) {
return res.status(400).json({
error: "Description too long",
error:
"Description too long",
});
}

if (status && !VALID_TASK_STATUS.includes(status)) {
if (
!validateTaskStatus(
status
)
) {
return res.status(400).json({
error: "Invalid task status",
error:
"Invalid task status",
});
}

if (
position !== undefined &&
(!Number.isInteger(position) || position < 0)
!validateTaskPosition(
position
)
) {
return res.status(400).json({
error: "Invalid task position",
error:
"Invalid task position",
});
}

req.body.title = sanitizeText(title);
req.body.description = sanitizeText(description || "");
req.body.title =
sanitizeText(title);

req.body.description =
sanitizeText(
description || ""
);

next();
}

export function validateMessage(req, res, next) {
const { text, username } = req.body;
export function validateMessage(
req,
res,
next
) {
const {
text,
username,
} = req.body;

if (
username &&
!validator.isLength(username.trim(), { min: 2, max: 40 })
!validator.isLength(
username.trim(),
{
min: 2,
max: 40,
}
)
) {
return res.status(400).json({
error: "Invalid username length",
error:
"Invalid username length",
});
}

if (
text &&
!validator.isLength(text.trim(), { max: 2000 })
!validator.isLength(
text.trim(),
{ max: 2000 }
)
) {
return res.status(400).json({
error: "Message too long",
error:
"Message too long",
});
}

req.body.username = sanitizeText(username || "");
req.body.text = sanitizeText(text || "");
req.body.username =
sanitizeText(
username || ""
);

req.body.text =
sanitizeText(text || "");

next();
}

export function validateFeedItem(req, res, next) {
const { title, body, type } = req.body;
export function validateFeedItem(
req,
res,
next
) {
const {
title,
body,
type,
} = req.body;

if (
!title ||
!validator.isLength(title.trim(), { min: 1, max: 120 })
!validator.isLength(
title.trim(),
{
min: 1,
max: 120,
}
)
) {
return res.status(400).json({
error: "Invalid title",
Expand All @@ -87,23 +184,42 @@ export function validateFeedItem(req, res, next) {

if (
!body ||
!validator.isLength(body.trim(), { min: 1, max: 1500 })
!validator.isLength(
body.trim(),
{
min: 1,
max: 1500,
}
)
) {
return res.status(400).json({
error: "Invalid body",
});
}

const allowedTypes = ["discussion", "code", "milestone"];
const allowedTypes = [
"discussion",
"code",
"milestone",
];

if (type && !allowedTypes.includes(type)) {
if (
type &&
!allowedTypes.includes(
type
)
) {
return res.status(400).json({
error: "Invalid feed type",
error:
"Invalid feed type",
});
}

req.body.title = sanitizeText(title);
req.body.body = sanitizeText(body);
req.body.title =
sanitizeText(title);

req.body.body =
sanitizeText(body);

next();
}
Loading