1
1
import { ValidateFunctions } from "./types" ;
2
2
3
- const regexHasSpaces : RegExp = / \s / ;
4
- const regexOnlyNumbers : RegExp = / ^ \d + $ / ;
5
- const regexStartsWithNumber : RegExp = / ^ \d / ;
6
3
const defaultErrorMsg : string [ ] = [
7
4
"Username cannot be empty" ,
8
5
"Username too short" ,
9
6
"This username is too long" ,
10
- "Username cannot contain spaces" ,
11
- "Cannot start with a number" ,
12
- "Cannot contain only numbers" ,
7
+ "Invalid username" ,
13
8
] ;
14
9
15
10
interface OptionsParams {
16
11
minLength ?: number ;
17
12
maxLength ?: number ;
13
+ cbValidate ?: ( username : string ) => boolean ;
18
14
errorMsg ?: ( string | null ) [ ] ;
19
15
}
20
16
21
17
const defaultOptionsParams : OptionsParams = {
22
18
minLength : undefined ,
23
19
maxLength : undefined ,
20
+ cbValidate : undefined ,
24
21
errorMsg : defaultErrorMsg ,
25
22
} ;
26
23
27
24
/**
28
25
* @param username
29
26
* @param minLength optional
30
27
* @param maxLength optional
28
+ * @param cbValidate optional
31
29
* @param errorMsg optional
32
30
* @default minLength number: 1
33
31
* @default maxLength number: Infinity
34
- * @example validateUsername('User999', { minLength: 8, maxLength: 20 });
35
- * @example validateUsername('User999', { minLength: 8, maxLength: 20, errorMsg: ['My own errorsMsg'] });
32
+ * @default cbValidate function: undefined
36
33
* @info minLength cannot be greater than maxLength
37
- * @description This function returns 7 errors in the following order,
34
+ * @description This function returns 4 errors in the following order,
38
35
*
39
36
* If you want to use a default parameter, use null.
40
37
*
41
38
* Default:
42
- * [
43
- 'Username cannot be empty',
44
- 'Username must be between ${maxLenthUsername} and ${maxLenthUsername} characters',
45
- 'Username must be between ${maxLenthUsername} and ${maxLenthUsername} characters',
46
- 'Username cannot contain spaces',
47
- 'Cannot start with a number',
48
- 'Cannot contain only numbers',
39
+ * [
40
+ "Username cannot be empty",
41
+ "Username must be between ${maxLenthUsername} and ${maxLenthUsername} characters",
42
+ "Username must be between ${maxLenthUsername} and ${maxLenthUsername} characters",
43
+ "Invalid username",
49
44
];
50
45
*
51
46
* Create a list of errors separated by commas in strings
52
- * @returns An object with ' isValid' (boolean) and ' errorMsg' (string) properties.
47
+ * @returns An object with " isValid" (boolean) and " errorMsg" (string) properties.
53
48
*/
54
49
function validateUsername (
55
50
username : string ,
56
- { minLength, maxLength, errorMsg } : OptionsParams = defaultOptionsParams ,
51
+ {
52
+ minLength,
53
+ maxLength,
54
+ cbValidate,
55
+ errorMsg,
56
+ } : OptionsParams = defaultOptionsParams ,
57
57
) : ValidateFunctions {
58
58
if ( typeof username !== "string" ) {
59
59
throw new TypeError ( "The input should be a string." ) ;
@@ -78,39 +78,6 @@ function validateUsername(
78
78
79
79
validateLengthParams ( minLenthUsername , maxLenthUsername ) ;
80
80
81
- if ( regexHasSpaces . test ( username ) ) {
82
- return {
83
- isValid : false ,
84
- errorMsg : getErrorMessage (
85
- 3 ,
86
- errorMsg ,
87
- minLenthUsername ,
88
- maxLenthUsername ,
89
- ) ,
90
- } ;
91
- }
92
- if ( regexOnlyNumbers . test ( username ) ) {
93
- return {
94
- isValid : false ,
95
- errorMsg : getErrorMessage (
96
- 5 ,
97
- errorMsg ,
98
- minLenthUsername ,
99
- maxLenthUsername ,
100
- ) ,
101
- } ;
102
- }
103
- if ( regexStartsWithNumber . test ( username ) ) {
104
- return {
105
- isValid : false ,
106
- errorMsg : getErrorMessage (
107
- 4 ,
108
- errorMsg ,
109
- minLenthUsername ,
110
- maxLenthUsername ,
111
- ) ,
112
- } ;
113
- }
114
81
if ( username . length < minLenthUsername ) {
115
82
return {
116
83
isValid : false ,
@@ -134,10 +101,15 @@ function validateUsername(
134
101
} ;
135
102
}
136
103
137
- if ( containsMultipleSpecialChars ( username ) ) {
104
+ if ( cbValidate && ! cbValidate ( username ) ) {
138
105
return {
139
106
isValid : false ,
140
- errorMsg : "Username cannot contain multiple special characters" ,
107
+ errorMsg : getErrorMessage (
108
+ 3 ,
109
+ errorMsg ,
110
+ minLenthUsername ,
111
+ maxLenthUsername ,
112
+ ) ,
141
113
} ;
142
114
}
143
115
@@ -171,9 +143,11 @@ function validateLengthParams(
171
143
) {
172
144
throw new Error ( "maxLength or minLength must be a number" ) ;
173
145
}
146
+
174
147
if ( minLenthUsername > maxLenthUsername ) {
175
148
throw new Error ( "Minimum cannot be greater than maximum" ) ;
176
149
}
150
+
177
151
if ( minLenthUsername < 1 || maxLenthUsername < 1 ) {
178
152
throw new Error ( "Size parameters cannot be less than one" ) ;
179
153
}
@@ -197,50 +171,4 @@ function getErrorMessage(
197
171
return errorMessage ?? defaultErrorMsg [ index ] ;
198
172
}
199
173
200
- function containsMultipleSpecialChars ( username : string ) : boolean {
201
- const specialChars : string [ ] = [
202
- "!" ,
203
- "@" ,
204
- "#" ,
205
- "$" ,
206
- "%" ,
207
- "^" ,
208
- "&" ,
209
- "*" ,
210
- "(" ,
211
- ")" ,
212
- "-" ,
213
- "_" ,
214
- "=" ,
215
- "+" ,
216
- "[" ,
217
- "]" ,
218
- "{" ,
219
- "}" ,
220
- "|" ,
221
- "\\" ,
222
- ";" ,
223
- ":" ,
224
- "'" ,
225
- '"' ,
226
- "," ,
227
- "." ,
228
- "<" ,
229
- ">" ,
230
- "/" ,
231
- "?" ,
232
- ] ;
233
-
234
- const charCount : { [ key : string ] : number } = { } ;
235
-
236
- for ( const char of username ) {
237
- if ( specialChars . includes ( char ) ) {
238
- charCount [ char ] = ( charCount [ char ] || 0 ) + 1 ;
239
- if ( charCount [ char ] > 2 ) {
240
- return true ;
241
- }
242
- }
243
- }
244
- return false ;
245
- }
246
174
export default validateUsername ;
0 commit comments