kmod-cli 1.2.3 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,17 +1,87 @@
|
|
|
1
1
|
import { useState } from 'react';
|
|
2
2
|
|
|
3
3
|
export const REGEXS = {
|
|
4
|
+
/**
|
|
5
|
+
* /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
|
|
6
|
+
* @example "7K7dI@ex.com"
|
|
7
|
+
*/
|
|
4
8
|
email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
|
|
9
|
+
/**
|
|
10
|
+
* /^[0-9]{10,11}$/
|
|
11
|
+
* @example "0123456789"
|
|
12
|
+
*/
|
|
5
13
|
phone: /^[0-9]{10,11}$/,
|
|
14
|
+
/**
|
|
15
|
+
* /^(https?:\/\/)?([\w-]+(\.[\w-]+)+)(\/[\w-]*)*(\?.*)?$/
|
|
16
|
+
* @example "https://google.com"
|
|
17
|
+
*/
|
|
6
18
|
url: /^(https?:\/\/)?([\w-]+(\.[\w-]+)+)(\/[\w-]*)*(\?.*)?$/,
|
|
19
|
+
/**
|
|
20
|
+
* /^[a-zA-Z0-9_]{3,20}$/
|
|
21
|
+
* @example "abc123"
|
|
22
|
+
*/
|
|
7
23
|
username: /^[a-zA-Z0-9_]{3,20}$/,
|
|
24
|
+
/**
|
|
25
|
+
* /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d@$!%*?&]{8,}$/
|
|
26
|
+
* @example "Abc123@"
|
|
27
|
+
*/
|
|
8
28
|
password: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d@$!%*?&]{8,}$/,
|
|
29
|
+
/**
|
|
30
|
+
* /^[0-9]{5,6}$/
|
|
31
|
+
* @example "123456"
|
|
32
|
+
*/
|
|
9
33
|
postalCode: /^[0-9]{5,6}$/,
|
|
34
|
+
/**
|
|
35
|
+
* /^\d+$/
|
|
36
|
+
* @example "123"
|
|
37
|
+
*/
|
|
10
38
|
number: /^\d+$/,
|
|
39
|
+
/**
|
|
40
|
+
* /^\d+(\.\d{1,2})?$/
|
|
41
|
+
* @example "123.45"
|
|
42
|
+
*/
|
|
11
43
|
decimal: /^\d+(\.\d{1,2})?$/,
|
|
44
|
+
/**
|
|
45
|
+
* /^[a-zA-Z0-9\s]{1,50}$/
|
|
46
|
+
* @example "Lily Nguyen"
|
|
47
|
+
*/
|
|
12
48
|
name: /^[a-zA-ZÀ-ỹ\s]{1,50}$/,
|
|
13
|
-
|
|
14
|
-
}
|
|
49
|
+
/**
|
|
50
|
+
* /^\d{4}-\d{2}-\d{2}$/
|
|
51
|
+
* @example "2023-12-01"
|
|
52
|
+
*/
|
|
53
|
+
date_4_2_2: /^\d{4}-\d{2}-\d{2}$/,
|
|
54
|
+
/**
|
|
55
|
+
* /^\d{2}-\d{4}-\d{2}$/
|
|
56
|
+
* @example "12-2023-01"
|
|
57
|
+
*/
|
|
58
|
+
date_2_4_2: /^\d{2}-\d{4}-\d{2}$/,
|
|
59
|
+
/**
|
|
60
|
+
* /^\d{2}-\d{2}-\d{4}$/
|
|
61
|
+
* @example "12-01-2023"
|
|
62
|
+
*/
|
|
63
|
+
date_2_2_4: /^\d{2}-\d{2}-\d{4}$/,
|
|
64
|
+
/**
|
|
65
|
+
* /^[a-zA-ZÀ-ỹ0-9\s]+$/,
|
|
66
|
+
* @example "Hello Thế Giới"
|
|
67
|
+
*/
|
|
68
|
+
text: /^[a-zA-ZÀ-ỹ0-9\s]+$/,
|
|
69
|
+
/**
|
|
70
|
+
* /^[a-zA-Z0-9\s.,#\/-]+$/
|
|
71
|
+
* @example "123 Main St, Anytown, USA"
|
|
72
|
+
*/
|
|
73
|
+
address: /^[a-zA-Z0-9\s.,#\/-]+$/,
|
|
74
|
+
/**
|
|
75
|
+
* /^(true|false|0|1)$/
|
|
76
|
+
* @example "true"
|
|
77
|
+
*/
|
|
78
|
+
boolean: /^(true|false|0|1)$/,
|
|
79
|
+
/**
|
|
80
|
+
* /^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$/
|
|
81
|
+
* @example "12345678-1234-1234-1234-123456789012"
|
|
82
|
+
*/
|
|
83
|
+
uuid: /[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}/,
|
|
84
|
+
}
|
|
15
85
|
|
|
16
86
|
interface ValidationRule {
|
|
17
87
|
required?: boolean;
|
|
@@ -22,7 +92,7 @@ interface ValidationRule {
|
|
|
22
92
|
errorMessage?: string;
|
|
23
93
|
}
|
|
24
94
|
|
|
25
|
-
type ValidationRules<T> = {
|
|
95
|
+
export type ValidationRules<T> = {
|
|
26
96
|
[K in keyof T]: ValidationRule;
|
|
27
97
|
};
|
|
28
98
|
|
|
@@ -81,6 +151,24 @@ export const useFormValidator = <T extends Record<string, any>>(
|
|
|
81
151
|
return isValid;
|
|
82
152
|
};
|
|
83
153
|
|
|
154
|
+
// validate array of fields
|
|
155
|
+
const validateFields = (fields: Array<keyof T>): boolean => {
|
|
156
|
+
const newErrors: ValidationErrors<T> = {};
|
|
157
|
+
let isValid = true;
|
|
158
|
+
|
|
159
|
+
for (const field of fields) {
|
|
160
|
+
const value = values[field];
|
|
161
|
+
const error = validateField(field, value);
|
|
162
|
+
if (error) {
|
|
163
|
+
isValid = false;
|
|
164
|
+
newErrors[field] = error;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
setErrors(newErrors);
|
|
169
|
+
return isValid;
|
|
170
|
+
};
|
|
171
|
+
|
|
84
172
|
const handleChange = <K extends keyof T>(field: K, value: T[K]) => {
|
|
85
173
|
setValues({...values, [field]: value});
|
|
86
174
|
const error = validateField(field, value);
|
|
@@ -88,10 +176,59 @@ export const useFormValidator = <T extends Record<string, any>>(
|
|
|
88
176
|
};
|
|
89
177
|
|
|
90
178
|
return {
|
|
179
|
+
/**
|
|
180
|
+
* Form values
|
|
181
|
+
* - ex: values.email
|
|
182
|
+
*/
|
|
91
183
|
values,
|
|
184
|
+
/**
|
|
185
|
+
* Form errors
|
|
186
|
+
* - ex: errors.email
|
|
187
|
+
*/
|
|
92
188
|
errors,
|
|
189
|
+
/**
|
|
190
|
+
* Update the form values
|
|
191
|
+
* - ex: handleChange('email', 'a@example.com')
|
|
192
|
+
*/
|
|
93
193
|
handleChange,
|
|
194
|
+
/**
|
|
195
|
+
* Validate all form fields
|
|
196
|
+
* - ex: validateAllFields()
|
|
197
|
+
* @returns boolean
|
|
198
|
+
*/
|
|
94
199
|
validateAllFields,
|
|
200
|
+
/**
|
|
201
|
+
* Update entire the form values
|
|
202
|
+
* @param newValues
|
|
203
|
+
* - ex: setValues({ email: 'a@example.com', password: '123456' })
|
|
204
|
+
*/
|
|
95
205
|
setValues,
|
|
206
|
+
/**
|
|
207
|
+
* Validate array of fields
|
|
208
|
+
* - ex: validateFields(['email', 'password'])
|
|
209
|
+
* @returns boolean
|
|
210
|
+
*/
|
|
211
|
+
validateFields
|
|
96
212
|
};
|
|
97
213
|
};
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
// import { useFormValidator, REGEXS } from '@/utils/validate/simple-validate';
|
|
218
|
+
|
|
219
|
+
// const validationRules = {
|
|
220
|
+
// email: { required: true, pattern: REGEXS.email, errorMessage: "Please enter a valid email." },
|
|
221
|
+
// password: { required: true, minLength: 6, errorMessage: "Password must be at least 6 characters." },
|
|
222
|
+
// };
|
|
223
|
+
|
|
224
|
+
// const { values, errors, handleChange, validateAllFields } = useFormValidator(
|
|
225
|
+
// { email: '', password: '' },
|
|
226
|
+
// validationRules
|
|
227
|
+
// );
|
|
228
|
+
|
|
229
|
+
// // On form submit
|
|
230
|
+
// const handleSubmit = () => {
|
|
231
|
+
// if (validateAllFields()) {
|
|
232
|
+
// // proceed with form submission
|
|
233
|
+
// }
|
|
234
|
+
// };
|