Skip to content

Commit

Permalink
refactor(validation): Update task name and description regex patterns
Browse files Browse the repository at this point in the history
- Updated the regex pattern for TASK_NAME to ensure:
  - Minimum of 3 characters and a maximum of 50 characters.
  - No leading or trailing spaces.
  - No consecutive spaces.

- Updated the regex pattern for TASK_DESCRIPTION to ensure:
  - A maximum of 500 characters.
  - No leading or trailing spaces.
  - No consecutive spaces.

New patterns:
- const TASK_NAME_REGEX = /^(?!.*\s{2,})(?!\s)(.{2,50}?)(?<!\s)$/;
- const TASK_DESCRIPTION_REGEX = /^(?!.*\s{2,})(?!\s)(.{0,500}?)(?<!\s)$/;
  • Loading branch information
TKanX committed Oct 7, 2024
1 parent 5fc15dc commit b843de8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/utils/validationUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const DISPLAY_NAME_REGEX = /^(?!.*\s{2,})[^\s](.{0,18}[^\s])?$|^$/; // Check if
const CLASS_CODE_REGEX = /^[A-Z0-9]{6}$/; // Check if the class code is valid (exactly 6 characters, uppercase letters and numbers only)
const CLASS_NAME_REGEX = /^(?!\s*$)[a-zA-Z0-9\s]{1,50}$/; // Check if the class name is valid (at least 1 character, alphanumeric characters and spaces only, cannot be empty or just spaces or symbols)
const STUDENT_NAME_REGEX = /^(?!.*\s{2,})[^\s](.{0,18}[^\s])?$|^$/; // Check if the student name is valid (no more than 20 characters, no leading or trailing spaces, no consecutive spaces)
const TASK_NAME_REGEX = /^[a-zA-Z0-9\s]{1,50}$/; // Check if the task name is valid (at least 1 character, alphanumeric characters and spaces only)
const TASK_DESCRIPTION_REGEX = /^[a-zA-Z0-9\s]{1,500}$/; // Check if the task description is valid (at least 1 character, alphanumeric characters and spaces only)
const TASK_NAME_REGEX = /^(?!.*\s{2,})(?!\s)(.{2,50}?)(?<!\s)$/; // Check if the task name is valid (at least 3 characters, no more than 50 characters, no leading or trailing spaces, no consecutive spaces)
const TASK_DESCRIPTION_REGEX = /^(?!.*\s{2,})(?!\s)(.{0,500}?)(?<!\s)$/; // Check if the task description is valid (no more than 500 characters, no leading or trailing spaces, no consecutive spaces)

/**
* @function validateEmail - Validate an email address.
Expand Down
24 changes: 20 additions & 4 deletions tests/utils/validationUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,16 @@ describe("validateTaskName", () => {
expect(validateTaskName("a".repeat(51))).toBe(false);
});

test("should return false for a task name with special characters", () => {
expect(validateTaskName("Task@Name!")).toBe(false);
test("should return false for a task name with leading spaces", () => {
expect(validateTaskName(" leading")).toBe(false);
});

test("should return false for a task name with consecutive spaces", () => {
expect(validateTaskName("consecutive spaces")).toBe(false);
});

test("should return true for a task name with special characters", () => {
expect(validateTaskName("Task-Name | 123")).toBe(true);
});

test("should return true for a valid task name with alphanumeric characters", () => {
Expand Down Expand Up @@ -419,8 +427,16 @@ describe("validateTaskDescription", () => {
expect(validateTaskDescription("a".repeat(501))).toBe(false);
});

test("should return false for a task description with special characters", () => {
expect(validateTaskDescription("Task@Description!")).toBe(false);
test("should return false for a task description with leading spaces", () => {
expect(validateTaskDescription(" leading")).toBe(false);
});

test("should return false for a task description with consecutive spaces", () => {
expect(validateTaskDescription("consecutive spaces")).toBe(false);
});

test("should return true for a task description with special characters", () => {
expect(validateTaskDescription("Task@Description!")).toBe(true);
});

test("should return true for a valid task description with alphanumeric characters", () => {
Expand Down

0 comments on commit b843de8

Please sign in to comment.