From 4386806695d8ea3ae77e5dc84564e2cccb211efe Mon Sep 17 00:00:00 2001 From: Gabriel Logan Date: Mon, 13 Jan 2025 15:15:01 -0300 Subject: [PATCH 1/4] feat: enhance cbValidate function to return structured validation results and update tests --- src/validateUsername.ts | 21 ++++++----------- tests/src/validateUsername.test.ts | 38 +++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/validateUsername.ts b/src/validateUsername.ts index 507f6a1..7fa66da 100644 --- a/src/validateUsername.ts +++ b/src/validateUsername.ts @@ -4,13 +4,12 @@ const defaultErrorMsg: string[] = [ "Username cannot be empty", "Username too short", "This username is too long", - "Invalid username", ]; interface OptionsParams { minLength?: number; maxLength?: number; - cbValidate?: (username: string) => boolean; + cbValidate?: (username: string) => ValidateFunctions; errorMsg?: (string | null)[]; } @@ -31,7 +30,7 @@ const defaultOptionsParams: OptionsParams = { * @default maxLength number: Infinity * @default cbValidate function: undefined * @info minLength cannot be greater than maxLength - * @description This function returns 4 errors in the following order, + * @description This function returns 3 errors in the following order, * * If you want to use a default parameter, use null. * @@ -40,7 +39,6 @@ const defaultOptionsParams: OptionsParams = { "Username cannot be empty", "Username must be between ${maxLenthUsername} and ${maxLenthUsername} characters", "Username must be between ${maxLenthUsername} and ${maxLenthUsername} characters", - "Invalid username", ]; * * Create a list of errors separated by commas in strings @@ -101,16 +99,11 @@ function validateUsername( }; } - if (cbValidate && !cbValidate(username)) { - return { - isValid: false, - errorMsg: getErrorMessage( - 3, - errorMsg, - minLenthUsername, - maxLenthUsername, - ), - }; + const cbValidateResult: ValidateFunctions | undefined = + cbValidate?.(username); + + if (cbValidateResult && !cbValidateResult.isValid) { + return cbValidateResult; } return { diff --git a/tests/src/validateUsername.test.ts b/tests/src/validateUsername.test.ts index 21a5f60..090f697 100644 --- a/tests/src/validateUsername.test.ts +++ b/tests/src/validateUsername.test.ts @@ -118,7 +118,19 @@ describe("validateUsername", () => { const result = validateUsername("User1232", { minLength: 3, maxLength: 25, - cbValidate: (username: string) => username === "User123", + cbValidate: (username: string) => { + if (username !== "User123") { + return { + isValid: false, + errorMsg: "Invalid username", + }; + } + + return { + isValid: true, + errorMsg: null, + }; + }, }); expect(result).toEqual({ isValid: false, @@ -126,6 +138,30 @@ describe("validateUsername", () => { }); }); + it("should return true based on the cbValidation function", () => { + const result = validateUsername("User123", { + minLength: 3, + maxLength: 25, + cbValidate: (username: string) => { + if (username !== "User123") { + return { + isValid: false, + errorMsg: "Invalid username", + }; + } + + return { + isValid: true, + errorMsg: null, + }; + }, + }); + expect(result).toEqual({ + isValid: true, + errorMsg: null, + }); + }); + it("should return defaultErrorMsg when passed null to errorMsg index", () => { const result = validateUsername("", { minLength: 3, From 34a4290eb088103486793f108278944a491925e8 Mon Sep 17 00:00:00 2001 From: Gabriel Logan Date: Mon, 13 Jan 2025 15:16:34 -0300 Subject: [PATCH 2/4] chore: update version to 2.4.0 in README and package.json --- README.md | 22 ++++++---------------- package.json | 2 +- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 192d888..8a08ceb 100644 --- a/README.md +++ b/README.md @@ -20,35 +20,25 @@ Feel free to find bugs and report them to me. Your feedback is highly appreciate jsDelivr: ```bash -https://cdn.jsdelivr.net/npm/multiform-validator@2.3.1/+esm +https://cdn.jsdelivr.net/npm/multiform-validator@2.4.0/+esm ``` ```html ``` ### CJS: -jsDelivr: - -```bash -https://cdn.jsdelivr.net/npm/multiform-validator@2.2.0/dist/cjs/index.min.js -``` - -```html - -``` - unpkg: ```bash -https://unpkg.com/multiform-validator@2.3.1/dist/cjs/index.cjs +https://unpkg.com/multiform-validator@2.4.0/dist/cjs/index.cjs ``` ```html - + ``` ### Example of use with CDN @@ -56,7 +46,7 @@ https://unpkg.com/multiform-validator@2.3.1/dist/cjs/index.cjs using cjs: ```html - +