Skip to content

Commit

Permalink
Modify the valitator function to accept numbers too
Browse files Browse the repository at this point in the history
  • Loading branch information
samaradel committed Oct 17, 2024
1 parent b9ff25e commit 17e8dba
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/playground/src/components/smtp_server.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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))
);
},
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/src/utils/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
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,
});
});

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

0 comments on commit 17e8dba

Please sign in to comment.