Skip to content
Merged
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
36 changes: 32 additions & 4 deletions functions/src/controllers/application_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,18 +496,46 @@ 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;
}

// 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;
}
Expand Down