From cd7e4e4337a126b32c5023f378fd9ab8d226849b Mon Sep 17 00:00:00 2001 From: samaradel Date: Tue, 10 Sep 2024 17:06:47 +0300 Subject: [PATCH 01/18] Remove isDiscourse check and apply adding email or username for all apps with smtp --- .../playground/src/components/smtp_server.vue | 16 +++++++--------- packages/playground/src/utils/validators.ts | 6 ++++++ packages/playground/src/weblets/tf_discourse.vue | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/playground/src/components/smtp_server.vue b/packages/playground/src/components/smtp_server.vue index 3f91660dda..93a657cca2 100644 --- a/packages/playground/src/components/smtp_server.vue +++ b/packages/playground/src/components/smtp_server.vue @@ -20,17 +20,16 @@ - + From 6f50303ad36b0dc1650480a8ceee15f8489ba74f Mon Sep 17 00:00:00 2001 From: Samar Adel Date: Wed, 11 Sep 2024 14:27:25 +0300 Subject: [PATCH 02/18] Update packages/playground/src/components/smtp_server.vue Co-authored-by: Zainab Elgohary <40770501+zaelgohary@users.noreply.github.com> --- packages/playground/src/components/smtp_server.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/playground/src/components/smtp_server.vue b/packages/playground/src/components/smtp_server.vue index 93a657cca2..040e0f7598 100644 --- a/packages/playground/src/components/smtp_server.vue +++ b/packages/playground/src/components/smtp_server.vue @@ -45,7 +45,7 @@ :rules="[ validators.required('Password is required.'), validators.minLength('Password must be at least 6 characters.', 6), - validators.maxLength('Password cannot exceed 50 characters.', 69), + validators.maxLength('Password cannot exceed 69 characters.', 69), validators.pattern('Password should not contain whitespaces.', { pattern: /^[^\s]+$/, }), From f994a5405b89d0f0ef4d761ac0491fcea31644da Mon Sep 17 00:00:00 2001 From: Samar Adel Date: Wed, 11 Sep 2024 14:29:42 +0300 Subject: [PATCH 03/18] Update packages/playground/src/weblets/tf_discourse.vue Co-authored-by: Zainab Elgohary <40770501+zaelgohary@users.noreply.github.com> --- packages/playground/src/weblets/tf_discourse.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/playground/src/weblets/tf_discourse.vue b/packages/playground/src/weblets/tf_discourse.vue index 76ba5d6f33..5496e05b40 100644 --- a/packages/playground/src/weblets/tf_discourse.vue +++ b/packages/playground/src/weblets/tf_discourse.vue @@ -94,7 +94,7 @@ From b7a783640134c850acee12d1c607550f77c12c96 Mon Sep 17 00:00:00 2001 From: samaradel Date: Thu, 19 Sep 2024 11:28:18 +0300 Subject: [PATCH 04/18] Validate SMTP username/email --- packages/playground/src/utils/validators.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/playground/src/utils/validators.ts b/packages/playground/src/utils/validators.ts index 549b40a401..d475d1c467 100644 --- a/packages/playground/src/utils/validators.ts +++ b/packages/playground/src/utils/validators.ts @@ -780,6 +780,9 @@ export async function isAvailableName(grid: GridClient, name: string) { export function validateSmtp(v: string) { const emailValidation = isEmail("Please provide a valid email address.")(v); - const username = isAlpha("Name must start with an alphabetical character.")(v[0]); - return emailValidation && username; + const username = !v.includes("@"); + if (username) { + return username; + } + return emailValidation; } From 81af97c27b038d08889ea39d841d359d6eaebfce Mon Sep 17 00:00:00 2001 From: samaradel Date: Thu, 19 Sep 2024 12:40:36 +0300 Subject: [PATCH 05/18] Fix build --- packages/playground/src/utils/validators.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/playground/src/utils/validators.ts b/packages/playground/src/utils/validators.ts index d475d1c467..0fff15f959 100644 --- a/packages/playground/src/utils/validators.ts +++ b/packages/playground/src/utils/validators.ts @@ -780,9 +780,8 @@ export async function isAvailableName(grid: GridClient, name: string) { export function validateSmtp(v: string) { const emailValidation = isEmail("Please provide a valid email address.")(v); - const username = !v.includes("@"); - if (username) { - return username; + const username = /[!@#$%^&*()_+-={}:<>?,./]/.test(v); + if (username && emailValidation) { + return { message: "Please provide a valid input" }; } - return emailValidation; } From bc4c149855de3bc7e3595daddd615107c9db0c41 Mon Sep 17 00:00:00 2001 From: Samar Adel Date: Thu, 19 Sep 2024 12:45:04 +0300 Subject: [PATCH 06/18] Apply code scanning fix for overly permissive regular expression range Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> --- packages/playground/src/utils/validators.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/playground/src/utils/validators.ts b/packages/playground/src/utils/validators.ts index 0fff15f959..125b8378c0 100644 --- a/packages/playground/src/utils/validators.ts +++ b/packages/playground/src/utils/validators.ts @@ -780,7 +780,7 @@ export async function isAvailableName(grid: GridClient, name: string) { export function validateSmtp(v: string) { const emailValidation = isEmail("Please provide a valid email address.")(v); - const username = /[!@#$%^&*()_+-={}:<>?,./]/.test(v); + const username = /[!@#$%^&*()_+\-={}:<>?,./]/.test(v); if (username && emailValidation) { return { message: "Please provide a valid input" }; } From 4c1c95526b4a7d57e3d92e7a74171394369b5e70 Mon Sep 17 00:00:00 2001 From: Samar Adel Date: Mon, 23 Sep 2024 12:25:27 +0300 Subject: [PATCH 07/18] Update packages/playground/src/utils/validators.ts Co-authored-by: Omar Kassem --- packages/playground/src/utils/validators.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/playground/src/utils/validators.ts b/packages/playground/src/utils/validators.ts index 125b8378c0..41f80c697d 100644 --- a/packages/playground/src/utils/validators.ts +++ b/packages/playground/src/utils/validators.ts @@ -782,6 +782,6 @@ export function validateSmtp(v: string) { const emailValidation = isEmail("Please provide a valid email address.")(v); const username = /[!@#$%^&*()_+\-={}:<>?,./]/.test(v); if (username && emailValidation) { - return { message: "Please provide a valid input" }; + return { message: "Please provide a valid username or email" }; } } From f2e0fc8bd974a81f58384059376efb9c5d233fa5 Mon Sep 17 00:00:00 2001 From: samaradel Date: Mon, 23 Sep 2024 12:31:28 +0300 Subject: [PATCH 08/18] rn validate smtp function --- packages/playground/src/components/smtp_server.vue | 2 +- packages/playground/src/utils/validators.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/playground/src/components/smtp_server.vue b/packages/playground/src/components/smtp_server.vue index 040e0f7598..ec546fa833 100644 --- a/packages/playground/src/components/smtp_server.vue +++ b/packages/playground/src/components/smtp_server.vue @@ -22,7 +22,7 @@ :rules="[ validators.required('Email or Username is required.'), (v: string) => { - return validators.validateSmtp(v); + return validators.isValidSmtp(v); } ]" #="{ props }" diff --git a/packages/playground/src/utils/validators.ts b/packages/playground/src/utils/validators.ts index 41f80c697d..3303bdf06d 100644 --- a/packages/playground/src/utils/validators.ts +++ b/packages/playground/src/utils/validators.ts @@ -778,7 +778,7 @@ export async function isAvailableName(grid: GridClient, name: string) { } } -export function validateSmtp(v: string) { +export function isValidSmtp(v: string) { const emailValidation = isEmail("Please provide a valid email address.")(v); const username = /[!@#$%^&*()_+\-={}:<>?,./]/.test(v); if (username && emailValidation) { From f3bbf8d81e93649e5a5cd82766559d6d7a208ecb Mon Sep 17 00:00:00 2001 From: samaradel Date: Wed, 25 Sep 2024 15:38:31 +0300 Subject: [PATCH 09/18] Rename validate function parameter --- packages/playground/src/utils/validators.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/playground/src/utils/validators.ts b/packages/playground/src/utils/validators.ts index 3303bdf06d..d69b73e5ab 100644 --- a/packages/playground/src/utils/validators.ts +++ b/packages/playground/src/utils/validators.ts @@ -778,9 +778,9 @@ export async function isAvailableName(grid: GridClient, name: string) { } } -export function isValidSmtp(v: string) { - const emailValidation = isEmail("Please provide a valid email address.")(v); - const username = /[!@#$%^&*()_+\-={}:<>?,./]/.test(v); +export function isValidSmtp(input: string) { + const emailValidation = isEmail("Please provide a valid email address.")(input); + const username = /[!@#$%^&*()_+\-={}:<>?,./]/.test(input); if (username && emailValidation) { return { message: "Please provide a valid username or email" }; } From a05eb5569ac1760870c94653dda5342d1cdc46e4 Mon Sep 17 00:00:00 2001 From: samaradel Date: Wed, 25 Sep 2024 15:44:57 +0300 Subject: [PATCH 10/18] Add doc string for validate smtp function --- packages/playground/src/utils/validators.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/playground/src/utils/validators.ts b/packages/playground/src/utils/validators.ts index d69b73e5ab..61f8362755 100644 --- a/packages/playground/src/utils/validators.ts +++ b/packages/playground/src/utils/validators.ts @@ -778,6 +778,15 @@ export async function isAvailableName(grid: GridClient, name: string) { } } +/** + * Validates an SMTP input string. + * + * Checks if the input string is a valid email address and does not contain special characters. + * + * @param {string} input - The input string to validate. + * @returns {{ message: string }} - An object with an error message if the input is invalid, or undefined if the input is valid. + */ + export function isValidSmtp(input: string) { const emailValidation = isEmail("Please provide a valid email address.")(input); const username = /[!@#$%^&*()_+\-={}:<>?,./]/.test(input); From 0674977593f3272e662d0f266736a18cc96d9f0f Mon Sep 17 00:00:00 2001 From: samaradel Date: Sun, 29 Sep 2024 17:38:45 +0300 Subject: [PATCH 11/18] - Set validation to limit username - Add space checker in validator function - Add unit test for smtp validation function --- .../playground/src/components/smtp_server.vue | 1 + packages/playground/src/utils/validators.ts | 2 +- .../tests/utils/isValidSmtp.test.ts | 24 +++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 packages/playground/tests/utils/isValidSmtp.test.ts diff --git a/packages/playground/src/components/smtp_server.vue b/packages/playground/src/components/smtp_server.vue index ec546fa833..c1f53c664c 100644 --- a/packages/playground/src/components/smtp_server.vue +++ b/packages/playground/src/components/smtp_server.vue @@ -21,6 +21,7 @@ :value="$props.modelValue.username" :rules="[ validators.required('Email or Username is required.'), + validators.minLength('Email or Username must be at least 2 characters.', 2), (v: string) => { return validators.isValidSmtp(v); } diff --git a/packages/playground/src/utils/validators.ts b/packages/playground/src/utils/validators.ts index 61f8362755..fcd288342f 100644 --- a/packages/playground/src/utils/validators.ts +++ b/packages/playground/src/utils/validators.ts @@ -789,7 +789,7 @@ export async function isAvailableName(grid: GridClient, name: string) { export function isValidSmtp(input: string) { const emailValidation = isEmail("Please provide a valid email address.")(input); - const username = /[!@#$%^&*()_+\-={}:<>?,./]/.test(input); + const username = /[!@#$%^&*()\s_+\-={}:<>?,./]/.test(input); if (username && emailValidation) { return { message: "Please provide a valid username or email" }; } diff --git a/packages/playground/tests/utils/isValidSmtp.test.ts b/packages/playground/tests/utils/isValidSmtp.test.ts new file mode 100644 index 0000000000..69147869dd --- /dev/null +++ b/packages/playground/tests/utils/isValidSmtp.test.ts @@ -0,0 +1,24 @@ +import { describe, expect, it } from "vitest"; + +import { isValidSmtp } from "../../src/utils/validators"; + +describe("isValidSmtp", () => { + it("returns nothing for valid username and email", () => { + const input = "valid.username@example.com"; + expect(isValidSmtp(input)).toBeUndefined(); + }); + + it("returns an error message for special characters in username", () => { + const input = "invalid_email"; + expect(isValidSmtp(input)).toEqual({ + message: "Please provide a valid username or email", + }); + }); + + it("returns an error message for white spaces in username", () => { + const input = "invalid username"; + expect(isValidSmtp(input)).toEqual({ + message: "Please provide a valid username or email", + }); + }); +}); From 788077bec98320be7c1c8f41f144361ba474cc96 Mon Sep 17 00:00:00 2001 From: samaradel Date: Wed, 2 Oct 2024 13:07:30 +0300 Subject: [PATCH 12/18] - Add min length and max length for the username and Email - Accept usernames that contain dashs and underscores and it must start with an alphabetical character and validate them --- .../playground/src/components/smtp_server.vue | 12 ++++++--- packages/playground/src/utils/validators.ts | 25 ++++++------------- .../tests/utils/isValidSmtp.test.ts | 24 ------------------ 3 files changed, 17 insertions(+), 44 deletions(-) delete mode 100644 packages/playground/tests/utils/isValidSmtp.test.ts diff --git a/packages/playground/src/components/smtp_server.vue b/packages/playground/src/components/smtp_server.vue index c1f53c664c..839d8ffbe6 100644 --- a/packages/playground/src/components/smtp_server.vue +++ b/packages/playground/src/components/smtp_server.vue @@ -21,10 +21,16 @@ :value="$props.modelValue.username" :rules="[ validators.required('Email or Username is required.'), - validators.minLength('Email or Username must be at least 2 characters.', 2), + validators.minLength('Username must be at least 2 characters.', 2), + validators.maxLength('Username must be at least 50 characters.', 50), (v: string) => { - return validators.isValidSmtp(v); - } + return ( + validators.isEmail('Please provide a valid email address.')(v) && + (validators.IsAlphaExpectDashAndUnderscore( + 'Username should consist of letters, dashs and underscores only.' + )(v) || validators.isAlpha('Username must start with an alphabetical character.')(v[0])) + ); + }, ]" #="{ props }" > diff --git a/packages/playground/src/utils/validators.ts b/packages/playground/src/utils/validators.ts index fcd288342f..644efa9e5a 100644 --- a/packages/playground/src/utils/validators.ts +++ b/packages/playground/src/utils/validators.ts @@ -117,6 +117,14 @@ export function IsAlphanumericExpectUnderscore(msg: string) { }; } +export function IsAlphaExpectDashAndUnderscore(msg: string) { + return (value: string) => { + if (!/^[a-zA-Z0-9_-]+$/.test(value)) { + return { message: msg, requiredTrue: true }; + } + }; +} + export function isAfter(msg: string, date?: string) { return (value: string) => { if (!validator.isAfter(value, date)) { @@ -777,20 +785,3 @@ export async function isAvailableName(grid: GridClient, name: string) { return { message: "Name is already taken." }; } } - -/** - * Validates an SMTP input string. - * - * Checks if the input string is a valid email address and does not contain special characters. - * - * @param {string} input - The input string to validate. - * @returns {{ message: string }} - An object with an error message if the input is invalid, or undefined if the input is valid. - */ - -export function isValidSmtp(input: string) { - const emailValidation = isEmail("Please provide a valid email address.")(input); - const username = /[!@#$%^&*()\s_+\-={}:<>?,./]/.test(input); - if (username && emailValidation) { - return { message: "Please provide a valid username or email" }; - } -} diff --git a/packages/playground/tests/utils/isValidSmtp.test.ts b/packages/playground/tests/utils/isValidSmtp.test.ts deleted file mode 100644 index 69147869dd..0000000000 --- a/packages/playground/tests/utils/isValidSmtp.test.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { describe, expect, it } from "vitest"; - -import { isValidSmtp } from "../../src/utils/validators"; - -describe("isValidSmtp", () => { - it("returns nothing for valid username and email", () => { - const input = "valid.username@example.com"; - expect(isValidSmtp(input)).toBeUndefined(); - }); - - it("returns an error message for special characters in username", () => { - const input = "invalid_email"; - expect(isValidSmtp(input)).toEqual({ - message: "Please provide a valid username or email", - }); - }); - - it("returns an error message for white spaces in username", () => { - const input = "invalid username"; - expect(isValidSmtp(input)).toEqual({ - message: "Please provide a valid username or email", - }); - }); -}); From 0d6a1b3536e0f8d51fe80bf0308a3e5e89944e3b Mon Sep 17 00:00:00 2001 From: samaradel Date: Wed, 2 Oct 2024 15:02:46 +0300 Subject: [PATCH 13/18] Add doc string for IsAlphaExpectDashAndUnderscore validator --- packages/playground/src/utils/validators.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/playground/src/utils/validators.ts b/packages/playground/src/utils/validators.ts index 644efa9e5a..cce255c217 100644 --- a/packages/playground/src/utils/validators.ts +++ b/packages/playground/src/utils/validators.ts @@ -117,6 +117,14 @@ export function IsAlphanumericExpectUnderscore(msg: string) { }; } +/** + * Returns a validation function that checks if the value only contains alphanumeric characters, dashes, and underscores. + * + * @param {string} msg - The error message to return if the validation fails. + * + * @returns {(value: string) => { message: string, requiredTrue: boolean }} - A function that takes a string value as input and returns an object with an error message and a requiredTrue flag if the validation fails. + */ + export function IsAlphaExpectDashAndUnderscore(msg: string) { return (value: string) => { if (!/^[a-zA-Z0-9_-]+$/.test(value)) { From 9fb68758d02aca0fc4cfa0cf66ea7d731a17d9e7 Mon Sep 17 00:00:00 2001 From: samaradel Date: Wed, 2 Oct 2024 15:47:27 +0300 Subject: [PATCH 14/18] Add unit test for IsAlphaExpectDashAndUnderscore function --- .../IsAlphaExpectDashAndUnderscore.test.ts | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 packages/playground/tests/utils/validators/IsAlphaExpectDashAndUnderscore.test.ts diff --git a/packages/playground/tests/utils/validators/IsAlphaExpectDashAndUnderscore.test.ts b/packages/playground/tests/utils/validators/IsAlphaExpectDashAndUnderscore.test.ts new file mode 100644 index 0000000000..70278be7c1 --- /dev/null +++ b/packages/playground/tests/utils/validators/IsAlphaExpectDashAndUnderscore.test.ts @@ -0,0 +1,38 @@ +import { describe, expect, it } from "vitest"; + +import { IsAlphaExpectDashAndUnderscore } from "../../../src/utils/validators"; + +const validator = IsAlphaExpectDashAndUnderscore("Username should consist of letters, dashs and underscores only."); + +describe("IsAlphaExpectDashAndUnderscore", () => { + it("returns an error message for input with spaces", () => { + const result = validator("hello world!"); + expect(result).toEqual({ + message: "Username should consist of letters, dashs and underscores only.", + requiredTrue: true, + }); + }); + + it("returns an error message for input with special characters", () => { + const result = validator("hello@world"); + expect(result).toEqual({ + message: "Username should consist of letters, dashs and underscores only.", + requiredTrue: true, + }); + }); + + it("returns undefined for valid username that contains underscore", () => { + const result = validator("hello_world"); + expect(result).toBeUndefined(); + }); + + it("returns undefined for valid username that contains dash", () => { + const result = validator("hello-world"); + expect(result).toBeUndefined(); + }); + + it("returns undefined for valid username", () => { + const result = validator("hello"); + expect(result).toBeUndefined(); + }); +}); From 4ad30a6d8d6dff0cffd05e25e4a55ada9fcd6f32 Mon Sep 17 00:00:00 2001 From: samaradel Date: Wed, 9 Oct 2024 13:35:21 +0300 Subject: [PATCH 15/18] Update SMTP input validation msg --- packages/playground/src/components/smtp_server.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/playground/src/components/smtp_server.vue b/packages/playground/src/components/smtp_server.vue index 839d8ffbe6..e166916f4c 100644 --- a/packages/playground/src/components/smtp_server.vue +++ b/packages/playground/src/components/smtp_server.vue @@ -22,7 +22,7 @@ :rules="[ validators.required('Email or Username is required.'), validators.minLength('Username must be at least 2 characters.', 2), - validators.maxLength('Username must be at least 50 characters.', 50), + validators.maxLength('Name cannot exceed 50 characters.', 50), (v: string) => { return ( validators.isEmail('Please provide a valid email address.')(v) && From 51465dcabe5ecd160a2fdd6e68935b24d322e9fa Mon Sep 17 00:00:00 2001 From: samaradel Date: Wed, 9 Oct 2024 13:35:54 +0300 Subject: [PATCH 16/18] Update SMTP input validation msg --- packages/playground/src/components/smtp_server.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/playground/src/components/smtp_server.vue b/packages/playground/src/components/smtp_server.vue index e166916f4c..a29a4fa3e8 100644 --- a/packages/playground/src/components/smtp_server.vue +++ b/packages/playground/src/components/smtp_server.vue @@ -22,7 +22,7 @@ :rules="[ validators.required('Email or Username is required.'), validators.minLength('Username must be at least 2 characters.', 2), - validators.maxLength('Name cannot exceed 50 characters.', 50), + validators.maxLength('Email or Username cannot exceed 50 characters.', 50), (v: string) => { return ( validators.isEmail('Please provide a valid email address.')(v) && From b9ff25edd2ca0e3f58bb8809d460dc702d22b5b4 Mon Sep 17 00:00:00 2001 From: samaradel Date: Sun, 13 Oct 2024 14:09:05 +0300 Subject: [PATCH 17/18] Remove number validator of first char and add test case for it --- packages/playground/src/components/smtp_server.vue | 2 +- .../utils/validators/IsAlphaExpectDashAndUnderscore.test.ts | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/playground/src/components/smtp_server.vue b/packages/playground/src/components/smtp_server.vue index a29a4fa3e8..0ec0f099a8 100644 --- a/packages/playground/src/components/smtp_server.vue +++ b/packages/playground/src/components/smtp_server.vue @@ -28,7 +28,7 @@ validators.isEmail('Please provide a valid email address.')(v) && (validators.IsAlphaExpectDashAndUnderscore( 'Username should consist of letters, dashs and underscores only.' - )(v) || validators.isAlpha('Username must start with an alphabetical character.')(v[0])) + )(v)) ); }, ]" diff --git a/packages/playground/tests/utils/validators/IsAlphaExpectDashAndUnderscore.test.ts b/packages/playground/tests/utils/validators/IsAlphaExpectDashAndUnderscore.test.ts index 70278be7c1..765efc6b88 100644 --- a/packages/playground/tests/utils/validators/IsAlphaExpectDashAndUnderscore.test.ts +++ b/packages/playground/tests/utils/validators/IsAlphaExpectDashAndUnderscore.test.ts @@ -35,4 +35,9 @@ describe("IsAlphaExpectDashAndUnderscore", () => { const result = validator("hello"); expect(result).toBeUndefined(); }); + + it("returns undefined for valid username/email that starts with numbers", () => { + const result = validator("4me"); + expect(result).toBeUndefined(); + }); }); From 17e8dba727d34a433260906382c10dfc8517533a Mon Sep 17 00:00:00 2001 From: samaradel Date: Thu, 17 Oct 2024 13:53:07 +0300 Subject: [PATCH 18/18] Modify the valitator function to accept numbers too --- packages/playground/src/components/smtp_server.vue | 4 ++-- packages/playground/src/utils/validators.ts | 2 +- .../IsAlphaExpectDashAndUnderscore.test.ts | 12 +++++++----- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/playground/src/components/smtp_server.vue b/packages/playground/src/components/smtp_server.vue index 0ec0f099a8..3896d48cd4 100644 --- a/packages/playground/src/components/smtp_server.vue +++ b/packages/playground/src/components/smtp_server.vue @@ -26,8 +26,8 @@ (v: string) => { return ( validators.isEmail('Please provide a valid email address.')(v) && - (validators.IsAlphaExpectDashAndUnderscore( - 'Username should consist of letters, dashs and underscores only.' + (validators.IsAlphanumericExpectDashAndUnderscore( + 'Username should consist of letters, numbers, dashs and underscores only.' )(v)) ); }, diff --git a/packages/playground/src/utils/validators.ts b/packages/playground/src/utils/validators.ts index cce255c217..fa9c86db60 100644 --- a/packages/playground/src/utils/validators.ts +++ b/packages/playground/src/utils/validators.ts @@ -125,7 +125,7 @@ export function IsAlphanumericExpectUnderscore(msg: string) { * @returns {(value: string) => { message: string, requiredTrue: boolean }} - A function that takes a string value as input and returns an object with an error message and a requiredTrue flag if the validation fails. */ -export function IsAlphaExpectDashAndUnderscore(msg: string) { +export function IsAlphanumericExpectDashAndUnderscore(msg: string) { return (value: string) => { if (!/^[a-zA-Z0-9_-]+$/.test(value)) { return { message: msg, requiredTrue: true }; diff --git a/packages/playground/tests/utils/validators/IsAlphaExpectDashAndUnderscore.test.ts b/packages/playground/tests/utils/validators/IsAlphaExpectDashAndUnderscore.test.ts index 765efc6b88..01d98227fe 100644 --- a/packages/playground/tests/utils/validators/IsAlphaExpectDashAndUnderscore.test.ts +++ b/packages/playground/tests/utils/validators/IsAlphaExpectDashAndUnderscore.test.ts @@ -1,14 +1,16 @@ import { describe, expect, it } from "vitest"; -import { IsAlphaExpectDashAndUnderscore } from "../../../src/utils/validators"; +import { IsAlphanumericExpectDashAndUnderscore } from "../../../src/utils/validators"; -const validator = IsAlphaExpectDashAndUnderscore("Username should consist of letters, dashs and underscores only."); +const validator = IsAlphanumericExpectDashAndUnderscore( + "Username should consist of letters, numbers, dashs and underscores only.", +); -describe("IsAlphaExpectDashAndUnderscore", () => { +describe("IsAlphanumericExpectDashAndUnderscore", () => { it("returns an error message for input with spaces", () => { const result = validator("hello world!"); expect(result).toEqual({ - message: "Username should consist of letters, dashs and underscores only.", + message: "Username should consist of letters, numbers, dashs and underscores only.", requiredTrue: true, }); }); @@ -16,7 +18,7 @@ describe("IsAlphaExpectDashAndUnderscore", () => { it("returns an error message for input with special characters", () => { const result = validator("hello@world"); expect(result).toEqual({ - message: "Username should consist of letters, dashs and underscores only.", + message: "Username should consist of letters, numbers, dashs and underscores only.", requiredTrue: true, }); });