Skip to content

Commit e836881

Browse files
committed
refactor: update parameter types and enhance documentation for passwordStrengthTester and validateUsername functions
1 parent daf4dbf commit e836881

File tree

2 files changed

+67
-86
lines changed
  • src/app/[locale]/(doc-session)/documentation/js/functions

2 files changed

+67
-86
lines changed

src/app/[locale]/(doc-session)/documentation/js/functions/passwordStrengthTester/page.tsx

+39-74
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { LocaleParams } from "@/types/Params";
1010

1111
export default async function PasswordStrengthTester({
1212
params: { locale },
13-
}: LocaleParams) {
13+
}: Readonly<LocaleParams>) {
1414
setStaticParamsLocale(locale);
1515

1616
const t = await getScopedI18n("DocumentationJsFunctions");
@@ -23,12 +23,6 @@ export default async function PasswordStrengthTester({
2323
<h1 className="title">
2424
Password Strength Tester {t("Function Documentation")}
2525
</h1>
26-
<p>
27-
{t("The")} <code>passwordStrengthTester</code>{" "}
28-
{t(
29-
'function evaluates the strength of a given password and returns the type of password strength as a string. The possible strength types are: "veryWeak", "weak", "regular", "strong", or "veryStrong", based on specific criteria.',
30-
)}
31-
</p>
3226

3327
<h2 className="subtitle">Import</h2>
3428
<p>
@@ -53,88 +47,59 @@ export default async function PasswordStrengthTester({
5347
</SyntaxHighlighter>
5448

5549
<h2 className="subtitle">{t("Parameters")}</h2>
56-
<p>
57-
{t(
58-
"The function takes one parameter, which must be a string representing the password to be evaluated.",
59-
)}
60-
</p>
6150
<ul>
6251
<li>
6352
<code>password</code> (string) -{" "}
6453
{t("The password to be evaluated for strength.")}
6554
</li>
55+
<li>
56+
<code>options</code> (object) - An optional object that can be
57+
passed to the function to customize the strength criteria.
58+
<ul>
59+
<li>
60+
<code>isVeryWeak</code> (password: string, passwordLength:
61+
number) -&gt; boolean
62+
</li>
63+
<li>
64+
<code>isWeak</code> (password: string, passwordLength: number)
65+
-&gt; boolean
66+
</li>
67+
<li>
68+
<code>isRegular</code> (password: string, passwordLength:
69+
number) -&gt; boolean
70+
</li>
71+
<li>
72+
<code>isStrong</code> (password: string, passwordLength: number)
73+
-&gt; boolean
74+
</li>
75+
<li>
76+
<code>isVeryStrong</code> (password: string, passwordLength:
77+
number) -&gt; boolean{" "}
78+
</li>
79+
</ul>
80+
</li>
6681
</ul>
6782

6883
<h2 className="subtitle">{t("Examples")}</h2>
6984

7085
<SyntaxHighlighter language="javascript" style={a11yDark}>
71-
{`const result1 = passwordStrengthTester("12345");
72-
console.log(result1); // Output: veryWeak
86+
{`import { passwordStrengthTester } from 'multiform-validator';
7387
74-
const result2 = passwordStrengthTester("abcdef");
75-
console.log(result2); // Output: weak
88+
const password = "P@ssw0rd123";
7689
77-
const result3 = passwordStrengthTester("abc12345");
78-
console.log(result3); // Output: regular
90+
const customOptions = {
91+
isVeryWeak: (password, passwordLength) => passwordLength < 6,
92+
isWeak: (password, passwordLength) => passwordLength >= 6 && passwordLength < 8,
93+
isRegular: (password, passwordLength) => passwordLength >= 8 && passwordLength < 10,
94+
isStrong: (password, passwordLength) => passwordLength >= 10 && passwordLength < 12,
95+
isVeryStrong: (password, passwordLength) => passwordLength >= 12,
96+
};
7997
80-
const result4 = passwordStrengthTester("Abc123awdasd");
81-
console.log(result4); // Output: strong
98+
const customResult = passwordStrengthTester(password, customOptions);
8299
83-
const result5 = passwordStrengthTester("SuperSecurePassword123!@");
84-
console.log(result5); // Output: veryStrong`}
100+
console.log(customResult); // "Strong"
101+
`}
85102
</SyntaxHighlighter>
86-
87-
<h2 className="subtitle">{t("Notes")}</h2>
88-
<p>
89-
{t(
90-
"The function checks the length of the password and applies certain criteria to classify the password strength. The returned strength type is based on the following criteria:",
91-
)}
92-
</p>
93-
<ul>
94-
<li>
95-
{t(
96-
"'veryWeak' - Password with less than 6 characters, consisting only of numbers",
97-
)}
98-
</li>
99-
<li>
100-
{t(
101-
"'weak' - Password with less than 6 characters, consisting of numbers and letters",
102-
)}
103-
</li>
104-
<li>
105-
{t(
106-
"'weak' - Password that repeats the same character more than 3 times in a row and is less than 10 characters long",
107-
)}
108-
</li>
109-
<li>
110-
{t(
111-
"'weak' - Password between 5 and 8 characters, consisting only of numbers",
112-
)}
113-
</li>
114-
<li>{t("'regular' - Password between 9 and 12 characters")}</li>
115-
<li>
116-
{t(
117-
"'regular' - Password greater than or equal to 6 and less than 8 characters, containing at least one number and one letter",
118-
)}
119-
</li>
120-
<li>
121-
{t(
122-
"'regular' - Password greater than 10 and has characters that are repeated more than 5 times in sequence",
123-
)}
124-
</li>
125-
<li>{t("'strong' - Password between 13 and 16 characters")}</li>
126-
<li>
127-
{t(
128-
"'strong' - Password with 8 or more characters, containing at least one uppercase letter, one number and one lowercase letter",
129-
)}
130-
</li>
131-
<li>{t("'veryStrong' - Password longer than 16 characters")}</li>
132-
<li>
133-
{t(
134-
"'veryStrong' - Password with 8 or more characters, containing at least one uppercase letter, one number, one special character and one lowercase letter",
135-
)}
136-
</li>
137-
</ul>
138103
</div>
139104
</div>
140105
);

src/app/[locale]/(doc-session)/documentation/js/functions/validateUsername/page.tsx

+28-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { LocaleParams } from "@/types/Params";
1010

1111
export default async function ValidateUsername({
1212
params: { locale },
13-
}: LocaleParams) {
13+
}: Readonly<LocaleParams>) {
1414
setStaticParamsLocale(locale);
1515

1616
const t = await getScopedI18n("DocumentationJsFunctions");
@@ -58,18 +58,25 @@ export default async function ValidateUsername({
5858
{`interface OptionsParams {
5959
minLength?: number;
6060
maxLength?: number;
61+
cbValidate?: (username: string) => boolean;
6162
errorMsg?: (string | null)[];
6263
}
6364
6465
const defaultOptionsParams: OptionsParams = {
6566
minLength: 1,
6667
maxLength: infinity,
68+
cbValidate: undefined,
6769
errorMsg: defaultErrorMsg,
6870
};
6971
7072
function validateUsername(
71-
username: string,
72-
{ minLength, maxLength, errorMsg }: OptionsParams = defaultOptionsParams,
73+
username: string,
74+
{
75+
minLength,
76+
maxLength,
77+
cbValidate,
78+
errorMsg,
79+
}: OptionsParams = defaultOptionsParams,
7380
): { isValid: boolean, errorMsg: string | null };`}
7481
</SyntaxHighlighter>
7582

@@ -91,6 +98,11 @@ function validateUsername(
9198
"[optional] - The maximum length of the username. Default is Infinity.",
9299
)}
93100
</li>
101+
<li>
102+
<code>cbValidate</code> ((username: string) -&gt; boolean){" "}
103+
[optional] - A custom validation function that takes the username as
104+
an argument and returns a boolean. Default is undefined.
105+
</li>
94106
<li>
95107
<code>errorMsg</code> (string[]){" "}
96108
{t(
@@ -103,14 +115,11 @@ function validateUsername(
103115

104116
<SyntaxHighlighter language="javascript" style={a11yDark}>
105117
{`[
106-
'Invalid value passed',
107-
'Username too short',
108-
'This username is too long',
109-
'Username cannot contain spaces',
110-
'Cannot start with a number',
111-
'Cannot contain only numbers',
112-
'Unknown error'
113-
]`}
118+
"Username cannot be empty",
119+
"Username too short",
120+
"This username is too long",
121+
"Invalid username",
122+
];`}
114123
</SyntaxHighlighter>
115124

116125
<h2 className="subtitle">{t("Examples")}</h2>
@@ -130,7 +139,14 @@ const result2 = validateUsername("User999", {
130139
errorMsg: customErrorMsg,
131140
});
132141
console.log(result2);
133-
// Output: { isValid: false, errorMsg: 'Username too short' }`}
142+
// Output: { isValid: false, errorMsg: 'Username too short' }
143+
144+
console.log(validateUsername('user123', { minLength: 5, maxLength: 10 }));
145+
// Output: { isValid: true, errorMsg: null }
146+
147+
console.log(validateUsername('user1', {
148+
cbValidate: (value) => value.length > 5
149+
})); // Output: { isValid: false, errorMsg: 'Invalid username' }`}
134150
</SyntaxHighlighter>
135151
</div>
136152
</div>

0 commit comments

Comments
 (0)