From 0910b0f1a7b33482a100cb85e9d5b7ec91da4b3e Mon Sep 17 00:00:00 2001 From: pandeymangg Date: Mon, 3 Nov 2025 10:59:58 +0530 Subject: [PATCH] fix: sonar issues --- apps/web/lib/survey/utils.ts | 109 ++++++++++++++----- apps/web/modules/survey/editor/lib/blocks.ts | 8 +- 2 files changed, 86 insertions(+), 31 deletions(-) diff --git a/apps/web/lib/survey/utils.ts b/apps/web/lib/survey/utils.ts index 2bb7ba5926..5a754fb834 100644 --- a/apps/web/lib/survey/utils.ts +++ b/apps/web/lib/survey/utils.ts @@ -1,9 +1,11 @@ import "server-only"; import { Result, err, ok } from "@formbricks/types/error-handlers"; import { InvalidInputError } from "@formbricks/types/errors"; +import { TI18nString } from "@formbricks/types/i18n"; import { TJsEnvironmentStateSurvey } from "@formbricks/types/js"; import { TSegment } from "@formbricks/types/segment"; import { TSurveyBlock } from "@formbricks/types/surveys/blocks"; +import { TSurveyElement, TSurveyPictureChoice } from "@formbricks/types/surveys/elements"; import { TSurvey, TSurveyQuestion, TSurveyQuestionTypeEnum } from "@formbricks/types/surveys/types"; import { isValidImageFile } from "@/modules/storage/utils"; @@ -59,6 +61,83 @@ export const checkForInvalidImagesInQuestions = (questions: TSurveyQuestion[]) = }); }; +/** + * Validates a single choice's image URL + * @param choice - Choice to validate + * @param choiceIdx - Index of the choice for error reporting + * @param elementId - Element ID for error reporting + * @param blockName - Block name for error reporting + * @returns Result with void data on success or Error on failure + */ +const validateChoiceImage = ( + choice: TSurveyPictureChoice | { id: string; label: TI18nString; imageUrl?: string }, + choiceIdx: number, + elementId: string, + blockName: string +): Result => { + if ("imageUrl" in choice && choice.imageUrl && !isValidImageFile(choice.imageUrl)) { + return err( + new Error( + `Invalid image URL in choice ${choiceIdx + 1} of element "${elementId}" in block "${blockName}"` + ) + ); + } + return ok(undefined); +}; + +/** + * Validates all choices in an element + * @param element - Element with choices to validate + * @param elementId - Element ID for error reporting + * @param blockName - Block name for error reporting + * @returns Result with void data on success or Error on failure + */ +const validateElementChoices = ( + element: TSurveyElement, + elementId: string, + blockName: string +): Result => { + if (!("choices" in element) || !Array.isArray(element.choices)) { + return ok(undefined); + } + + for (let choiceIdx = 0; choiceIdx < element.choices.length; choiceIdx++) { + const result = validateChoiceImage(element.choices[choiceIdx], choiceIdx, elementId, blockName); + if (!result.ok) { + return result; + } + } + + return ok(undefined); +}; + +/** + * Validates a single element's image URL and choices + * @param element - Element to validate + * @param elementIdx - Index of the element for error reporting + * @param blockIdx - Index of the block for error reporting + * @param blockName - Block name for error reporting + * @returns Result with void data on success or Error on failure + */ +const validateElement = ( + element: TSurveyElement, + elementIdx: number, + blockIdx: number, + blockName: string +): Result => { + // Check element imageUrl + if (element.imageUrl && !isValidImageFile(element.imageUrl)) { + return err( + new Error( + `Invalid image URL in element "${element.id}" (element ${elementIdx + 1}) of block "${blockName}" (block ${blockIdx + 1})` + ) + ); + } + + // Check choices + return validateElementChoices(element, element.id, blockName); +}; + /** * Validates that all image URLs in blocks (elements and their choices) are valid * @param blocks - Array of survey blocks to validate @@ -69,33 +148,9 @@ export const checkForInvalidImagesInBlocks = (blocks: TSurveyBlock[]): Result blocks.length) { return err(new Error(`Invalid index ${index}. Must be between 0 and ${blocks.length}`)); } + blocks.splice(index, 0, newBlock); } else { blocks.push(newBlock); @@ -123,7 +124,6 @@ export const deleteBlock = (survey: TSurvey, blockId: string): Result elements.length) { return err(new Error(`Invalid index ${index}. Must be between 0 and ${elements.length}`)); }