node_yupform 1.0.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.
- package/YupValidation.js +169 -0
- package/package.json +13 -0
package/YupValidation.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
//npm install react-hook-form yup @hookform/resolvers
|
|
2
|
+
|
|
3
|
+
// import * as Yup from "yup";
|
|
4
|
+
// import { useForm } from "react-hook-form";
|
|
5
|
+
// import { yupResolver } from "@hookform/resolvers/yup";
|
|
6
|
+
// import * as yup from "yup";
|
|
7
|
+
// interface FormData {
|
|
8
|
+
// name: string;
|
|
9
|
+
// email: string;
|
|
10
|
+
// password: string;
|
|
11
|
+
// confirmPassword: string;
|
|
12
|
+
// age: number;
|
|
13
|
+
// phone: string;
|
|
14
|
+
// }
|
|
15
|
+
// const schema = yup.object({
|
|
16
|
+
// name: yup
|
|
17
|
+
// .string()
|
|
18
|
+
// .required("Name is required")
|
|
19
|
+
// .min(3, "Minimum 3 characters")
|
|
20
|
+
// .max(20, "Maximum 20 characters"),
|
|
21
|
+
// email: yup
|
|
22
|
+
// .string()
|
|
23
|
+
// .required("Email is required")
|
|
24
|
+
// .email("Invalid email"),
|
|
25
|
+
// password: yup
|
|
26
|
+
// .string()
|
|
27
|
+
// .required("Password is required")
|
|
28
|
+
// .min(8, "Minimum 8 characters")
|
|
29
|
+
// .matches(
|
|
30
|
+
// /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).*$/,
|
|
31
|
+
// "Must contain Uppercase, Lowercase & Number"
|
|
32
|
+
// ),
|
|
33
|
+
// confirmPassword: yup
|
|
34
|
+
// .string()
|
|
35
|
+
// .required("Confirm Password required")
|
|
36
|
+
// .oneOf(
|
|
37
|
+
// [yup.ref("password")],
|
|
38
|
+
// "Passwords must match"
|
|
39
|
+
// ),
|
|
40
|
+
// age: yup
|
|
41
|
+
// .number()
|
|
42
|
+
// .required()
|
|
43
|
+
// .min(18, "Minimum age 18")
|
|
44
|
+
// .max(60, "Maximum age 60"),
|
|
45
|
+
// phone: yup
|
|
46
|
+
// .string()
|
|
47
|
+
// .required()
|
|
48
|
+
// .matches(
|
|
49
|
+
// /^[0-9]{10}$/,
|
|
50
|
+
// "Phone must be 10 digits"
|
|
51
|
+
// ),
|
|
52
|
+
// }).required();
|
|
53
|
+
// export default function App() {
|
|
54
|
+
// //type FormData = yup.InferType<typeof schema>;
|
|
55
|
+
// const {register,handleSubmit,formState: { errors },} = useForm<FormData>({
|
|
56
|
+
// resolver: yupResolver(schema),
|
|
57
|
+
// });
|
|
58
|
+
// const onSubmit = (data: FormData) => {
|
|
59
|
+
// console.log(data);
|
|
60
|
+
// };
|
|
61
|
+
// return (
|
|
62
|
+
// <form onSubmit={handleSubmit(onSubmit)}>
|
|
63
|
+
// <input
|
|
64
|
+
// placeholder="Name"
|
|
65
|
+
// {...register("name")}
|
|
66
|
+
// />
|
|
67
|
+
// <p>{errors.name?.message}</p>
|
|
68
|
+
// <input
|
|
69
|
+
// placeholder="Email"
|
|
70
|
+
// {...register("email")}
|
|
71
|
+
// />
|
|
72
|
+
// <p>{errors.email?.message}</p>
|
|
73
|
+
// <button type="submit">
|
|
74
|
+
// Submit
|
|
75
|
+
// </button>
|
|
76
|
+
// </form>
|
|
77
|
+
// );
|
|
78
|
+
// }
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
// **************************
|
|
82
|
+
|
|
83
|
+
//npm install react-hook-form zod @hookform/resolvers
|
|
84
|
+
// import { useForm } from "react-hook-form";
|
|
85
|
+
// import { z } from "zod";
|
|
86
|
+
// import { zodResolver } from "@hookform/resolvers/zod";
|
|
87
|
+
// // Validation Schema
|
|
88
|
+
// const registerSchema = z
|
|
89
|
+
// .object({
|
|
90
|
+
// fullName: z
|
|
91
|
+
// .string()
|
|
92
|
+
// .min(2, "Full name must be at least 2 characters"),
|
|
93
|
+
// email: z
|
|
94
|
+
// .string()
|
|
95
|
+
// .email("Invalid email address"),
|
|
96
|
+
// password: z
|
|
97
|
+
// .string()
|
|
98
|
+
// .min(6, "Password must be at least 6 characters"),
|
|
99
|
+
// confirmPassword: z.string(),
|
|
100
|
+
// age: z.coerce
|
|
101
|
+
// .number()
|
|
102
|
+
// .min(19, "Age must be above 18"),
|
|
103
|
+
// })
|
|
104
|
+
// .refine((data) => data.password === data.confirmPassword, {
|
|
105
|
+
// message: "Passwords do not match",
|
|
106
|
+
// path: ["confirmPassword"],
|
|
107
|
+
// });
|
|
108
|
+
// Type automatically generated from schema
|
|
109
|
+
// type FormData = z.infer<typeof registerSchema>;
|
|
110
|
+
// export default function RegisterForm() {
|
|
111
|
+
// const {
|
|
112
|
+
// register,
|
|
113
|
+
// handleSubmit,
|
|
114
|
+
// formState: { errors },
|
|
115
|
+
// } = useForm<FormData>({
|
|
116
|
+
// resolver: zodResolver(registerSchema),
|
|
117
|
+
// });
|
|
118
|
+
// const onSubmit = (data: FormData) => {
|
|
119
|
+
// console.log(data);
|
|
120
|
+
// alert("Form Submitted Successfully!");
|
|
121
|
+
// };
|
|
122
|
+
// return (
|
|
123
|
+
// <form onSubmit={handleSubmit(onSubmit)}>
|
|
124
|
+
// <div>
|
|
125
|
+
// <input
|
|
126
|
+
// type="text"
|
|
127
|
+
// placeholder="Full Name"
|
|
128
|
+
// {...register("fullName")}
|
|
129
|
+
// />
|
|
130
|
+
// <p>{errors.fullName?.message}</p>
|
|
131
|
+
// </div>
|
|
132
|
+
// <div>
|
|
133
|
+
// <input
|
|
134
|
+
// type="email"
|
|
135
|
+
// placeholder="Email"
|
|
136
|
+
// {...register("email")}
|
|
137
|
+
// />
|
|
138
|
+
// <p>{errors.email?.message}</p>
|
|
139
|
+
// </div>
|
|
140
|
+
// <div>
|
|
141
|
+
// <input
|
|
142
|
+
// type="password"
|
|
143
|
+
// placeholder="Password"
|
|
144
|
+
// {...register("password")}
|
|
145
|
+
// />
|
|
146
|
+
// <p>{errors.password?.message}</p>
|
|
147
|
+
// </div>
|
|
148
|
+
// <div>
|
|
149
|
+
// <input
|
|
150
|
+
// type="password"
|
|
151
|
+
// placeholder="Confirm Password"
|
|
152
|
+
// {...register("confirmPassword")}
|
|
153
|
+
// />
|
|
154
|
+
// <p>{errors.confirmPassword?.message}</p>
|
|
155
|
+
// </div>
|
|
156
|
+
// <div>
|
|
157
|
+
// <input
|
|
158
|
+
// type="number"
|
|
159
|
+
// placeholder="Age"
|
|
160
|
+
// {...register("age")}
|
|
161
|
+
// />
|
|
162
|
+
// <p>{errors.age?.message}</p>
|
|
163
|
+
// </div>
|
|
164
|
+
// <button type="submit">
|
|
165
|
+
// Register
|
|
166
|
+
// </button>
|
|
167
|
+
// </form>
|
|
168
|
+
// );
|
|
169
|
+
// }
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "node_yupform",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "YupValidation.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [],
|
|
10
|
+
"author": "",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"type": "commonjs"
|
|
13
|
+
}
|