@@ -54,11 +54,21 @@ export default async function ValidateUsername({
5454
5555 < h2 className = "subtitle" > { t ( "Function Signature" ) } </ h2 >
5656
57- < SyntaxHighlighter language = "javascript" style = { a11yDark } >
58- { `interface OptionsParams {
57+ < SyntaxHighlighter language = "typescript" style = { a11yDark } >
58+ { `type ValidateFunctions =
59+ | {
60+ isValid: true;
61+ errorMsg: null;
62+ }
63+ | {
64+ isValid: false;
65+ errorMsg: string;
66+ };
67+
68+ interface OptionsParams {
5969 minLength?: number;
6070 maxLength?: number;
61- cbValidate?: (username: string) => boolean ;
71+ cbValidate?: (username: string) => ValidateFunctions ;
6272 errorMsg?: (string | null)[];
6373}
6474
@@ -77,7 +87,7 @@ function validateUsername(
7787 cbValidate,
7888 errorMsg,
7989 }: OptionsParams = defaultOptionsParams,
80- ): { isValid: boolean, errorMsg: string | null }; ` }
90+ ): ValidateFunctions {} ` }
8191 </ SyntaxHighlighter >
8292
8393 < h2 className = "subtitle" > { t ( "Parameters" ) } </ h2 >
@@ -99,9 +109,9 @@ function validateUsername(
99109 ) }
100110 </ li >
101111 < li >
102- < code > cbValidate</ code > ((username: string) -> boolean ){ " " }
112+ < code > cbValidate</ code > ((username: string) -> ValidateFunctions ){ " " }
103113 [optional] - A custom validation function that takes the username as
104- an argument and returns a boolean . Default is undefined.
114+ an argument and returns a ValidateFunctions . Default is undefined.
105115 </ li >
106116 < li >
107117 < code > errorMsg</ code > (string[]){ " " }
@@ -118,7 +128,6 @@ function validateUsername(
118128 "Username cannot be empty",
119129 "Username too short",
120130 "This username is too long",
121- "Invalid username",
122131];` }
123132 </ SyntaxHighlighter >
124133
@@ -145,7 +154,19 @@ console.log(validateUsername('user123', { minLength: 5, maxLength: 10 }));
145154// Output: { isValid: true, errorMsg: null }
146155
147156console.log(validateUsername('user1', {
148- cbValidate: (value) => value.length > 5
157+ cbValidate: (username: string) => {
158+ if (username !== "User123") {
159+ return {
160+ isValid: false,
161+ errorMsg: "Invalid username",
162+ };
163+ }
164+
165+ return {
166+ isValid: true,
167+ errorMsg: null,
168+ };
169+ },
149170})); // Output: { isValid: false, errorMsg: 'Invalid username' }` }
150171 </ SyntaxHighlighter >
151172 </ div >
0 commit comments