From 9ceda0dd025e677eda003702a3f08486cf2b1c1b Mon Sep 17 00:00:00 2001 From: Kezia Date: Tue, 3 Jun 2025 01:08:07 -0400 Subject: [PATCH 1/2] fix: validate str length --- .../src/controllers/application_controller.ts | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/functions/src/controllers/application_controller.ts b/functions/src/controllers/application_controller.ts index ff70971..4d4355d 100644 --- a/functions/src/controllers/application_controller.ts +++ b/functions/src/controllers/application_controller.ts @@ -496,18 +496,41 @@ function validateStringValue(fieldValue: string | any, question: Question) { return errors; } + function countWords(text: string): number { + if (!text || text.trim() === "") return 0; + return text.trim().split(/\s+/).length; + } + // check length - if (validation.minLength && fieldValue.length < validation.minLength) { + if (validation.minLength && countWords(fieldValue) < validation.minLength) { errors.push({ field_id: `${question.id}`, - message: `Must be at least ${validation.minLength} character(s)`, + message: `Must be at least ${validation.minLength} word(s)`, }); - } else if (validation.maxLength && fieldValue.length > validation.maxLength) { + } else if (validation.maxLength && countWords(fieldValue) > validation.maxLength) { errors.push({ field_id: `${question.id}`, - message: `Must be less than ${validation.maxLength} character(s)`, + message: `Must be less than ${validation.maxLength} word(s)`, }); } + + // check regex pattern + // if (validation.pattern) { + // try { + // const regex = new RegExp(validation.pattern); + // if (!regex.test(fieldValue)) { + // errors.push({ + // field_id: `${question.id}`, + // message: `Value does not match the required pattern`, + // }); + // } + // } catch (regexError) { + // errors.push({ + // field_id: `${question.id}`, + // message: `Invalid validation pattern configured`, + // }); + // } + // } return errors; } From 3cefa95e3b2322fd0478baff02ca81f9cf161a9c Mon Sep 17 00:00:00 2001 From: Hibatullah Fawwaz Hana Date: Wed, 4 Jun 2025 00:58:30 +0700 Subject: [PATCH 2/2] fix: missing jsdoc --- functions/src/controllers/application_controller.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/functions/src/controllers/application_controller.ts b/functions/src/controllers/application_controller.ts index 4d4355d..4f15c93 100644 --- a/functions/src/controllers/application_controller.ts +++ b/functions/src/controllers/application_controller.ts @@ -496,6 +496,11 @@ function validateStringValue(fieldValue: string | any, question: Question) { return errors; } + /** + * Counts the number of words in a given text string. + * @param {string} text - The text to count words from + * @returns {number} The number of words in the text + */ function countWords(text: string): number { if (!text || text.trim() === "") return 0; return text.trim().split(/\s+/).length;