kmod-cli 1.8.1 → 1.8.2

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.
@@ -96,6 +96,9 @@ export type ValidationRules<T> = Partial<{
96
96
  [K in keyof T]: ValidationRule<T[K]>;
97
97
  }>;
98
98
 
99
+ type SetValuesAction<T> =
100
+ | Partial<T>
101
+ | ((prev: T) => Partial<T>);
99
102
 
100
103
  type ValidationErrors<T> = {
101
104
  [K in keyof T]?: string;
@@ -111,7 +114,7 @@ export const useFormValidator = <T extends Record<string, any>>(
111
114
  const [touched, setTouched] = useState<Touched<T>>({});
112
115
 
113
116
 
114
- const validateField = <K extends keyof T>(
117
+ const validateField = <K extends keyof T>(
115
118
  field: K,
116
119
  value: T[K],
117
120
  isTouched = false,
@@ -185,7 +188,7 @@ const validateField = <K extends keyof T>(
185
188
 
186
189
 
187
190
  // validate array of fields
188
- const validateFields = (fields: readonly (keyof T)[]): boolean => {
191
+ const validateFields = (fields: readonly (keyof T)[]): boolean => {
189
192
  let isValid = true;
190
193
  const newErrors: ValidationErrors<T> = {};
191
194
 
@@ -216,6 +219,66 @@ const validateFields = (fields: readonly (keyof T)[]): boolean => {
216
219
  setErrors({});
217
220
  setTouched({});
218
221
  };
222
+ const setValuesWithTouched = (
223
+ action: SetValuesAction<T>,
224
+ options?: {
225
+ validate?: boolean;
226
+ touchAll?: boolean;
227
+ isSubmit?: boolean;
228
+ }
229
+ ) => {
230
+ const {
231
+ validate = true,
232
+ touchAll = false,
233
+ isSubmit = false,
234
+ } = options || {};
235
+
236
+ setValues((prev) => {
237
+ const nextPartial =
238
+ typeof action === "function"
239
+ ? action(prev)
240
+ : action;
241
+
242
+ const merged = { ...prev, ...nextPartial };
243
+
244
+ const newTouched: Touched<T> = {};
245
+ const newErrors: ValidationErrors<T> = {};
246
+
247
+ const fields = touchAll
248
+ ? (Object.keys(merged) as (keyof T)[])
249
+ : (Object.keys(nextPartial) as (keyof T)[]);
250
+
251
+ fields.forEach((field) => {
252
+ newTouched[field] = true;
253
+
254
+ if (validate) {
255
+ const error = validateField(
256
+ field,
257
+ merged[field],
258
+ true,
259
+ isSubmit
260
+ );
261
+ newErrors[field] = error;
262
+ }
263
+ });
264
+
265
+ setTouched((prevTouched) => ({
266
+ ...prevTouched,
267
+ ...newTouched,
268
+ }));
269
+
270
+ if (validate) {
271
+ setErrors((prevErrors) => ({
272
+ ...prevErrors,
273
+ ...newErrors,
274
+ }));
275
+ }
276
+
277
+ return merged;
278
+ });
279
+ };
280
+
281
+
219
282
 
220
283
 
221
284
  return {
@@ -227,19 +290,21 @@ const validateFields = (fields: readonly (keyof T)[]): boolean => {
227
290
  validateAllFields,
228
291
  setValues,
229
292
  resetForm,
293
+ setValuesWithTouched,
230
294
  };
231
295
  };
232
296
 
233
297
 
298
+
234
299
  // import { useFormValidator, REGEXS } from '@/utils/validate/simple-validate';
235
300
 
236
301
  // const validationRules = {
237
- // username: { required: true, pattern: REGEXS.username, errorMessage: "Username must be 3-20 alphanumeric characters." },
238
- // password: { required: true, pattern: REGEXS.password, errorMessage: "Password must contain letters, numbers, and be at least 8 characters." },
302
+ // email: { required: true, pattern: REGEXS.email, errorMessage: "Please enter a valid email." },
303
+ // password: { required: true, minLength: 6, errorMessage: "Password must be at least 6 characters." },
239
304
  // };
240
305
 
241
306
  // const { values, errors, handleChange, validateAllFields } = useFormValidator(
242
- // { username: '', password: '' },
307
+ // { email: '', password: '' },
243
308
  // validationRules
244
309
  // );
245
310
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kmod-cli",
3
- "version": "1.8.1",
3
+ "version": "1.8.2",
4
4
  "description": "Stack components utilities fast setup in projects",
5
5
  "author": "kumo_d",
6
6
  "license": "MIT",