kmod-cli 1.1.0 → 1.2.0

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.
@@ -0,0 +1,97 @@
1
+ import { useState } from 'react';
2
+
3
+ export const REGEXS = {
4
+ email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
5
+ phone: /^[0-9]{10,11}$/,
6
+ url: /^(https?:\/\/)?([\w-]+(\.[\w-]+)+)(\/[\w-]*)*(\?.*)?$/,
7
+ username: /^[a-zA-Z0-9_]{3,20}$/,
8
+ password: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d@$!%*?&]{8,}$/,
9
+ postalCode: /^[0-9]{5,6}$/,
10
+ number: /^\d+$/,
11
+ decimal: /^\d+(\.\d{1,2})?$/,
12
+ name: /^[a-zA-ZÀ-ỹ\s]{1,50}$/,
13
+ date: /^\d{4}-\d{2}-\d{2}$/,
14
+ };
15
+
16
+ interface ValidationRule {
17
+ required?: boolean;
18
+ pattern?: RegExp;
19
+ custom?: (value: string) => boolean;
20
+ minLength?: number;
21
+ maxLength?: number;
22
+ errorMessage?: string;
23
+ }
24
+
25
+ type ValidationRules<T> = {
26
+ [K in keyof T]: ValidationRule;
27
+ };
28
+
29
+ type ValidationErrors<T> = {
30
+ [K in keyof T]?: string;
31
+ };
32
+
33
+ export const useFormValidator = <T extends Record<string, any>>(
34
+ initialValues: T,
35
+ validationRules: ValidationRules<T>
36
+ ) => {
37
+ const [values, setValues] = useState<T>(initialValues);
38
+ const [errors, setErrors] = useState<ValidationErrors<T>>({});
39
+
40
+ const validateField = (field: keyof T, value: T[keyof T]): string | undefined => {
41
+ const rules = validationRules[field];
42
+ if (!rules) return undefined;
43
+
44
+ if (rules.required && !value) {
45
+ return rules.errorMessage || "This field is required.";
46
+ }
47
+
48
+ if (rules.pattern && !rules.pattern.test(value)) {
49
+ return rules.errorMessage || "Invalid format.";
50
+ }
51
+
52
+ if (rules.minLength && value.length < rules.minLength) {
53
+ return rules.errorMessage || `Minimum length is ${rules.minLength}.`;
54
+ }
55
+
56
+ if (rules.maxLength && value.length > rules.maxLength) {
57
+ return rules.errorMessage || `Maximum length is ${rules.maxLength}.`;
58
+ }
59
+
60
+ if (rules.custom && !rules.custom(value)) {
61
+ return rules.errorMessage || "Invalid value.";
62
+ }
63
+
64
+ return undefined;
65
+ };
66
+
67
+ const validateAllFields = (): boolean => {
68
+ const newErrors: ValidationErrors<T> = {};
69
+ let isValid = true;
70
+
71
+ for (const field in validationRules) {
72
+ const value = values[field];
73
+ const error = validateField(field, value);
74
+ if (error) {
75
+ isValid = false;
76
+ newErrors[field] = error;
77
+ }
78
+ }
79
+
80
+ setErrors(newErrors);
81
+ return isValid;
82
+ };
83
+
84
+ const handleChange = <K extends keyof T>(field: K, value: T[K]) => {
85
+ setValues({...values, [field]: value});
86
+ const error = validateField(field, value);
87
+ setErrors({...errors, [field]: error});
88
+ };
89
+
90
+ return {
91
+ values,
92
+ errors,
93
+ handleChange,
94
+ validateAllFields,
95
+ setValues,
96
+ };
97
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kmod-cli",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Stack components utilities fast setup in projects",
5
5
  "author": "kumo_d",
6
6
  "license": "MIT",