formaze 2.0.0 → 2.0.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.
Files changed (2) hide show
  1. package/README.md +119 -113
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Formaze: A Customizable Form Validation package for React
1
+ # Formaze: An Easy Form Builder for React
2
2
 
3
- Formaze is a flexible and customizable form validation package for React built with `React Hook Form`, `Zod`, and `TailwindCSS`. It provides an easy way to define form validation schemas and handles complex form validation logic efficiently with proper type-safety.
3
+ Formaze is a flexible and customizable form builder for React built with `React Hook Form`, `Zod`, and `TailwindCSS`. It provides an easy way to build forms with custom validation schemas and handles complex form validation logic efficiently with proper type-safety.
4
4
 
5
5
  - Supports multiple field types such as `string`, `email`, `password`, `number`, `date`, and `boolean`.
6
6
  - Efficient utilization of Zod's built-in validation like `min`, `max`, `regex`, and `optional`.
@@ -28,55 +28,55 @@ import "formaze/dist/style.css";
28
28
 
29
29
  // create the validation schema
30
30
  const formSchema = makeFormSchema({
31
- email: { type: "email" },
32
- password: { type: "password", minLength: { value: 8 } },
31
+ email: { type: "email" },
32
+ password: { type: "password", minLength: { value: 8 } },
33
33
  });
34
34
 
35
- // create the form
35
+ // create the form component
36
36
  const Form = createFormValidator(formSchema);
37
37
 
38
38
  export function MyForm() {
39
- // for TypeScript
40
- function handleSubmit(data: z.infer<typeof formSchema>) {
41
- const result = formSchema.safeParse(data);
42
-
43
- if (!result.success) throw new Error("Invalid inputs");
44
-
45
- console.log(data);
46
- }
47
-
48
- // for JavaScript
49
- // /**@param {import("zod").infer<typeof formSchema>} data */
50
- // function handleSubmit(data) {
51
- // const result = formSchema.safeParse(data);
52
-
53
- // if (!result.success) throw new Error("Invalid inputs");
54
-
55
- // console.log(data);
56
- // }
57
-
58
- return (
59
- <Form onSubmit={handleSubmit}>
60
- <Form.Input
61
- label="Email"
62
- type="email"
63
- name="email"
64
- placeholder="Enter your email"
65
- />
66
- <Form.Input
67
- label="Password"
68
- type="password"
69
- name="password"
70
- placeholder="Enter your password"
71
- />
72
- <button
73
- className="rounded-md bg-blue-500 py-1 px-3 text-white hover:bg-blue-600"
74
- type="submit"
75
- >
76
- Submit
77
- </button>
78
- </Form>
79
- );
39
+ // for TypeScript
40
+ function handleSubmit(data: z.infer<typeof formSchema>) {
41
+ const result = formSchema.safeParse(data);
42
+
43
+ if (!result.success) throw new Error("Invalid inputs");
44
+
45
+ console.log(data);
46
+ }
47
+
48
+ // for JavaScript
49
+ // /**@param {import("zod").infer<typeof formSchema>} data */
50
+ // function handleSubmit(data) {
51
+ // const result = formSchema.safeParse(data);
52
+
53
+ // if (!result.success) throw new Error("Invalid inputs");
54
+
55
+ // console.log(data);
56
+ // }
57
+
58
+ return (
59
+ <Form onSubmit={handleSubmit}>
60
+ <Form.Input
61
+ label="Email"
62
+ type="email"
63
+ name="email"
64
+ placeholder="Enter your email"
65
+ />
66
+ <Form.Input
67
+ label="Password"
68
+ type="password"
69
+ name="password"
70
+ placeholder="Enter your password"
71
+ />
72
+ <button
73
+ className="rounded-md bg-blue-500 py-1 px-3 text-white hover:bg-blue-600"
74
+ type="submit"
75
+ >
76
+ Submit
77
+ </button>
78
+ </Form>
79
+ );
80
80
  }
81
81
  ```
82
82
 
@@ -86,33 +86,33 @@ The makeFormSchema method allows you to define validation rules for each field i
86
86
 
87
87
  ```js
88
88
  const formSchema = makeFormSchema({
89
- email: {
90
- type: "email",
91
- customMessage: "A valid email is required",
92
- },
93
- password: {
94
- type: "password",
95
- minLength: {
96
- value: 8,
97
- message: "Password must be at least 8 characters",
98
- },
99
- maxLength: {
100
- value: 16,
101
- message: "Password must be less than 16 characters",
102
- },
103
- },
104
- username: {
105
- type: "string",
106
- maxLength: {
107
- value: 12,
108
- message: "Username must be less than 12 characters",
109
- },
110
- customMessage: "Username must not be empty",
111
- },
112
- terms: {
113
- type: "boolean",
114
- customMessage: "You must accept the terms to continue",
115
- },
89
+ email: {
90
+ type: "email",
91
+ customMessage: "A valid email is required",
92
+ },
93
+ password: {
94
+ type: "password",
95
+ minLength: {
96
+ value: 8,
97
+ message: "Password must be at least 8 characters",
98
+ },
99
+ maxLength: {
100
+ value: 16,
101
+ message: "Password must be less than 16 characters",
102
+ },
103
+ },
104
+ username: {
105
+ type: "string",
106
+ regex: {
107
+ value: /^[a-z0-9]{3,}$/ /* only lowercase letters and numbers */,
108
+ message: "Username must contains lowercase letters and numbers",
109
+ },
110
+ customMessage: "Username must not be empty",
111
+ },
112
+ terms: {
113
+ type: "boolean",
114
+ customMessage: "You must accept the terms to continue",
115
+ },
116
116
  });
117
117
  ```
118
118
 
@@ -123,8 +123,8 @@ import { z } from "zod";
123
123
  import { createFormValidator } from "formaze";
124
124
 
125
125
  const formSchema = z.object({
126
- email: z.string().email(),
127
- name: z.string().min(3, { message: "Required" }),
126
+ email: z.string().email(),
127
+ name: z.string().min(3, { message: "Required" }),
128
128
  });
129
129
 
130
130
  const Form = createFormValidator(formSchema);
@@ -157,39 +157,39 @@ import "formaze/dist/style.css";
157
157
 
158
158
  ```css
159
159
  .form-control {
160
- display: flex;
161
- flex-direction: column;
162
- justify-content: center;
163
- gap: 1rem;
160
+ display: flex;
161
+ flex-direction: column;
162
+ justify-content: center;
163
+ gap: 1rem;
164
164
  }
165
165
 
166
166
  .form-control .form-field {
167
- display: flex;
168
- flex-direction: column;
169
- justify-content: center;
167
+ display: flex;
168
+ flex-direction: column;
169
+ justify-content: center;
170
170
  }
171
171
 
172
172
  .form-control .form-field .form-input {
173
- padding: 10px 1rem;
174
- border-radius: 10px;
175
- border-width: 1px;
176
- border-color: rgb(209 213 219);
173
+ padding: 10px 1rem;
174
+ border-radius: 10px;
175
+ border-width: 1px;
176
+ border-color: rgb(209 213 219);
177
177
  }
178
178
 
179
179
  .form-control .form-field .form-input:focus {
180
- border-color: rgb(59 130 246);
181
- outline: none;
180
+ border-color: rgb(59 130 246);
181
+ outline: none;
182
182
  }
183
183
 
184
184
  .form-control .form-field-error-text {
185
- color: rgb(220, 38, 38);
186
- font-size: 0.875rem;
185
+ color: rgb(220, 38, 38);
186
+ font-size: 0.875rem;
187
187
  }
188
188
 
189
189
  /* or */
190
190
 
191
191
  .form-control .form-field-error {
192
- color: rgb(220, 38, 38);
192
+ color: rgb(220, 38, 38);
193
193
  }
194
194
  ```
195
195
 
@@ -227,8 +227,8 @@ The createFormValidator function accepts an argument which is a type of zod sche
227
227
 
228
228
  ```tsx
229
229
  const schema = makeFormSchema({
230
- email: { type: "email" },
231
- password: { type: "string", minLength: { value: 8 } },
230
+ email: { type: "email" },
231
+ password: { type: "string", minLength: { value: 8 } },
232
232
  });
233
233
 
234
234
  const Form = createFormValidator(schema);
@@ -247,14 +247,14 @@ const Form = createFormValidator(schema);
247
247
 
248
248
  ```tsx
249
249
  const schema = makeFormSchema({
250
- email: { type: "email" },
251
- password: { type: "string" },
250
+ email: { type: "email" },
251
+ password: { type: "string" },
252
252
  });
253
253
 
254
254
  const Form = createFormValidator(schema);
255
255
 
256
256
  const handleSubmit = (data: z.infer<typeof schema>) => {
257
- console.log(data); // { email: "test@example.com", password: "securePassword" }
257
+ console.log(data); // { email: "test@example.com", password: "securePassword" }
258
258
  };
259
259
 
260
260
  <Form onSubmit={handleSubmit} />;
@@ -271,15 +271,15 @@ const handleSubmit = (data: z.infer<typeof schema>) => {
271
271
 
272
272
  ```tsx
273
273
  const schema = makeFormSchema({
274
- email: { type: "email" },
275
- password: { type: "string" },
274
+ email: { type: "email" },
275
+ password: { type: "string" },
276
276
  });
277
277
 
278
278
  const Form = createFormValidator(schema);
279
279
 
280
280
  const defaultValues = {
281
- email: "example@example.com",
282
- password: "",
281
+ email: "example@example.com",
282
+ password: "",
283
283
  };
284
284
 
285
285
  <Form defaultValues={defaultValues} />;
@@ -298,9 +298,9 @@ You can include `Form.Input` for each form field, along with any other elements
298
298
 
299
299
  ```tsx
300
300
  <Form onSubmit={onSubmit} defaultValues={defaultValues}>
301
- <Form.Input type="email" name="email" placeholder="Email" />
302
- <Form.Input type="password" name="password" placeholder="Password" />
303
- <button type="submit">Submit</button>
301
+ <Form.Input type="email" name="email" placeholder="Email" />
302
+ <Form.Input type="password" name="password" placeholder="Password" />
303
+ <button type="submit">Submit</button>
304
304
  </Form>
305
305
  ```
306
306
 
@@ -326,14 +326,17 @@ The Form.Input component is a form input field that is connected to a Zod-based
326
326
  **Props:**
327
327
 
328
328
  name: `keyof z.infer<T>`
329
+
329
330
  - Description:
330
331
  The name of the form field, which should correspond to one of the keys in the Zod schema used in the form. This connects the input to the form's validation logic.
331
332
 
332
333
  label: `string` (optional)
334
+
333
335
  - Description:
334
336
  A label for the form field. This is used for displaying a descriptive text for the input field.
335
337
 
336
338
  ...props: `React.InputHTMLAttributes<HTMLInputElement>`
339
+
337
340
  - Description:
338
341
  Any additional props that can be passed to an HTML input element. These props allow you to customize the input field, such as adding a placeholder, className, type, etc.
339
342
 
@@ -354,14 +357,14 @@ The makeFormSchema is a function that generates a Zod schema based on the provid
354
357
 
355
358
  ```tsx
356
359
  const schema = makeFormSchema({
357
- email: { type: "email" },
358
- password: {
359
- type: "string",
360
- minLength: {
361
- value: 8,
362
- message: "Password must be at least 8 characters long",
363
- },
364
- },
360
+ email: { type: "email" },
361
+ password: {
362
+ type: "string",
363
+ minLength: {
364
+ value: 8,
365
+ message: "Password must be at least 8 characters long",
366
+ },
367
+ },
365
368
  });
366
369
  ```
367
370
 
@@ -374,11 +377,14 @@ This schema can then be passed to the Form component returned by `createFormVali
374
377
  In order to give proper type support for JavaScript projects and to simplify the defining process this change has been made.
375
378
 
376
379
  **From this**
377
- ```tsx
380
+
381
+ ```tsx
378
382
  const Form = createFormValidator<typeof formSchema>(); ❌
379
383
  ```
384
+
380
385
  **To this**
381
- ```tsx
386
+
387
+ ```tsx
382
388
  const Form = createFormValidator(formSchema); ✔
383
389
  ```
384
390
 
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "formaze",
3
- "description": "An easy form validation package for react, built by using React hook form and zod library",
3
+ "description": "Easily build forms with custom validation logic for React",
4
4
  "main": "dist/formaze.js",
5
5
  "types": "dist/index.d.ts",
6
- "version": "2.0.0",
6
+ "version": "2.0.2",
7
7
  "type": "module",
8
8
  "keywords": [
9
9
  "react",