Skip to content

Commit 4386806

Browse files
committed
feat: enhance cbValidate function to return structured validation results and update tests
1 parent af59e8b commit 4386806

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

src/validateUsername.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ const defaultErrorMsg: string[] = [
44
"Username cannot be empty",
55
"Username too short",
66
"This username is too long",
7-
"Invalid username",
87
];
98

109
interface OptionsParams {
1110
minLength?: number;
1211
maxLength?: number;
13-
cbValidate?: (username: string) => boolean;
12+
cbValidate?: (username: string) => ValidateFunctions;
1413
errorMsg?: (string | null)[];
1514
}
1615

@@ -31,7 +30,7 @@ const defaultOptionsParams: OptionsParams = {
3130
* @default maxLength number: Infinity
3231
* @default cbValidate function: undefined
3332
* @info minLength cannot be greater than maxLength
34-
* @description This function returns 4 errors in the following order,
33+
* @description This function returns 3 errors in the following order,
3534
*
3635
* If you want to use a default parameter, use null.
3736
*
@@ -40,7 +39,6 @@ const defaultOptionsParams: OptionsParams = {
4039
"Username cannot be empty",
4140
"Username must be between ${maxLenthUsername} and ${maxLenthUsername} characters",
4241
"Username must be between ${maxLenthUsername} and ${maxLenthUsername} characters",
43-
"Invalid username",
4442
];
4543
*
4644
* Create a list of errors separated by commas in strings
@@ -101,16 +99,11 @@ function validateUsername(
10199
};
102100
}
103101

104-
if (cbValidate && !cbValidate(username)) {
105-
return {
106-
isValid: false,
107-
errorMsg: getErrorMessage(
108-
3,
109-
errorMsg,
110-
minLenthUsername,
111-
maxLenthUsername,
112-
),
113-
};
102+
const cbValidateResult: ValidateFunctions | undefined =
103+
cbValidate?.(username);
104+
105+
if (cbValidateResult && !cbValidateResult.isValid) {
106+
return cbValidateResult;
114107
}
115108

116109
return {

tests/src/validateUsername.test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,50 @@ describe("validateUsername", () => {
118118
const result = validateUsername("User1232", {
119119
minLength: 3,
120120
maxLength: 25,
121-
cbValidate: (username: string) => username === "User123",
121+
cbValidate: (username: string) => {
122+
if (username !== "User123") {
123+
return {
124+
isValid: false,
125+
errorMsg: "Invalid username",
126+
};
127+
}
128+
129+
return {
130+
isValid: true,
131+
errorMsg: null,
132+
};
133+
},
122134
});
123135
expect(result).toEqual({
124136
isValid: false,
125137
errorMsg: "Invalid username",
126138
});
127139
});
128140

141+
it("should return true based on the cbValidation function", () => {
142+
const result = validateUsername("User123", {
143+
minLength: 3,
144+
maxLength: 25,
145+
cbValidate: (username: string) => {
146+
if (username !== "User123") {
147+
return {
148+
isValid: false,
149+
errorMsg: "Invalid username",
150+
};
151+
}
152+
153+
return {
154+
isValid: true,
155+
errorMsg: null,
156+
};
157+
},
158+
});
159+
expect(result).toEqual({
160+
isValid: true,
161+
errorMsg: null,
162+
});
163+
});
164+
129165
it("should return defaultErrorMsg when passed null to errorMsg index", () => {
130166
const result = validateUsername("", {
131167
minLength: 3,

0 commit comments

Comments
 (0)