From a5ca38cd0cb622f74dcd486ee1bc3e33475587b8 Mon Sep 17 00:00:00 2001 From: hemamanju173-svg Date: Wed, 4 Nov 2026 19:01:57 +0530 Subject: [PATCH] Fix achievement validation in achievements.js Add input validation for achievement titles. Handle leading and trailing whitespace. Add a reusable isValidAchievement() helper. Preserve the existing VALID_ACHIEVEMENTS export for compatibility. Improve validation safety and code readability. --- backend/constants/achievements.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/backend/constants/achievements.js b/backend/constants/achievements.js index 7da2b777..6185c18a 100644 --- a/backend/constants/achievements.js +++ b/backend/constants/achievements.js @@ -1,7 +1,7 @@ -// Single source of truth for all valid achievement IDs. -// Frontend derives display metadata (description) from the title; -// the backend only needs to know which titles are legitimate. +"use strict"; + +// Single source of truth for valid achievement titles. const VALID_ACHIEVEMENTS = new Set([ "First Interview", "Interview Pro", @@ -16,4 +16,19 @@ const VALID_ACHIEVEMENTS = new Set([ "30-Day Streak", ]); -module.exports = { VALID_ACHIEVEMENTS }; \ No newline at end of file +/** + * Check whether an achievement title is valid. + * @param {string} title + * @returns {boolean} + */ +const isValidAchievement = (title) => { + return ( + typeof title === "string" && + VALID_ACHIEVEMENTS.has(title.trim()) + ); +}; + +module.exports = { + VALID_ACHIEVEMENTS, + isValidAchievement, +};