formaze 1.0.9 → 2.0.1

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 +133 -128
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,9 +1,9 @@
1
- # Formaze: A stomizable 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
- - Efficient utilization of zod's built-in validation like `min`, `max`, `regex`, and `optional`.
6
+ - Efficient utilization of Zod's built-in validation like `min`, `max`, `regex`, and `optional`.
7
7
  - Custom error messages.
8
8
 
9
9
  ## Installation
@@ -23,108 +23,108 @@ npm install formaze
23
23
  // "use client"
24
24
  import { z } from "zod";
25
25
  import { makeFormSchema, createFormValidator } from "formaze";
26
- /* pre-styled css (you can check the styling guide below) */
26
+ /* pre-styled CSS (you can check the styling guide below) */
27
27
  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
35
  // create the form
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
 
83
83
  ## Define Validation Schema
84
84
 
85
- The makeFormSchema method allows you to define validation rules for each field in the form. You can specify field types (`string`, `email`, `password`, `number`, `date`, `boolean`) along with their specific validation constraints like `minLength`, `maxLength`, `min`, `max`, `regex`, and `optional`.
85
+ The makeFormSchema method allows you to define validation rules for each field in the form. You can specify field types (`string`, `email`, `password`, `number`, `date`, `boolean`) along with specific validation constraints like `minLength`, `maxLength`, `min`, `max`, `regex`, and `optional`.
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
+ 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
+ },
116
116
  });
117
117
  ```
118
118
 
119
- Though, you can directly use `zod` to define schema as well and pass it to the Form props created through `createFormValidator` method.
119
+ You can also directly use `Zod` to define schema as well and pass it to the Form props created through `createFormValidator` method.
120
120
 
121
121
  ```tsx
122
122
  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);
@@ -145,58 +145,57 @@ You can specify the following validation options for each field:
145
145
 
146
146
  - For Tailwind (pre-styled)
147
147
 
148
- Follow the [Official Tailwind Docs](https://tailwindcss.com/docs/installation/framework-guides) for initializing project with vite or next.js
148
+ Follow the [Official Tailwind Docs](https://tailwindcss.com/docs/installation/framework-guides) for initializing project with vite or next.js.
149
149
 
150
- You can add your own styles or import the below css (styled with tailwind css) file into the main or App component or the component where the form is being used
150
+ You can add your own styles or import the CSS below (styled with TailwindCSS) file into the main or App component or the component where the form is being used.
151
151
 
152
152
  ```js
153
153
  import "formaze/dist/style.css";
154
154
  ```
155
155
 
156
- - For Pure CSS
157
- Here are the css classes with default styles, add it into main css file
156
+ - For Pure CSS, here are the CSS classes with default styles, add them to your main CSS file
158
157
 
159
158
  ```css
160
159
  .form-control {
161
- display: flex;
162
- flex-direction: column;
163
- justify-content: center;
164
- gap: 1rem;
160
+ display: flex;
161
+ flex-direction: column;
162
+ justify-content: center;
163
+ gap: 1rem;
165
164
  }
166
165
 
167
166
  .form-control .form-field {
168
- display: flex;
169
- flex-direction: column;
170
- justify-content: center;
167
+ display: flex;
168
+ flex-direction: column;
169
+ justify-content: center;
171
170
  }
172
171
 
173
172
  .form-control .form-field .form-input {
174
- padding: 10px 1rem;
175
- border-radius: 10px;
176
- border-width: 1px;
177
- 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);
178
177
  }
179
178
 
180
179
  .form-control .form-field .form-input:focus {
181
- border-color: rgb(59 130 246);
182
- outline: none;
180
+ border-color: rgb(59 130 246);
181
+ outline: none;
183
182
  }
184
183
 
185
184
  .form-control .form-field-error-text {
186
- color: rgb(220, 38, 38);
187
- font-size: 0.875rem;
185
+ color: rgb(220, 38, 38);
186
+ font-size: 0.875rem;
188
187
  }
189
188
 
190
189
  /* or */
191
190
 
192
191
  .form-control .form-field-error {
193
- color: rgb(220, 38, 38);
192
+ color: rgb(220, 38, 38);
194
193
  }
195
194
  ```
196
195
 
197
196
  ## For Next.js (app router)
198
197
 
199
- You just have to add `"use client"` directive at the top of your file where you are using this form and its related methods
198
+ You just need to add `"use client"` directive at the top of your file where you are using this form and its related methods
200
199
 
201
200
  ```tsx
202
201
  "use client";
@@ -222,14 +221,14 @@ The createFormValidator function accepts an argument which is a type of zod sche
222
221
  - Type: `T` (A Zod schema) (optional)
223
222
 
224
223
  - Description:
225
- This optional schema prop is a schema used to define the structure and validation rules for the form. This schema determines the expected fields, their types, and any validation logic, such as required fields, minimum/maximum values, regex patterns, etc. **NOTE: In future version this prop will be deprecated. As of now this prop is no longer needed.**
224
+ This optional schema prop defines the structure and validation rules for the form. This schema determines the expected fields, their types, and any validation logic, such as required fields, minimum/maximum values, regex etc. **NOTE: In future version this prop will be deprecated. As of now this prop is no longer needed.**
226
225
 
227
226
  - Example:
228
227
 
229
228
  ```tsx
230
229
  const schema = makeFormSchema({
231
- email: { type: "email" },
232
- password: { type: "string", minLength: { value: 8 } },
230
+ email: { type: "email" },
231
+ password: { type: "string", minLength: { value: 8 } },
233
232
  });
234
233
 
235
234
  const Form = createFormValidator(schema);
@@ -248,14 +247,14 @@ const Form = createFormValidator(schema);
248
247
 
249
248
  ```tsx
250
249
  const schema = makeFormSchema({
251
- email: { type: "email" },
252
- password: { type: "string" },
250
+ email: { type: "email" },
251
+ password: { type: "string" },
253
252
  });
254
253
 
255
254
  const Form = createFormValidator(schema);
256
255
 
257
256
  const handleSubmit = (data: z.infer<typeof schema>) => {
258
- console.log(data); // { email: "test@example.com", password: "securePassword" }
257
+ console.log(data); // { email: "test@example.com", password: "securePassword" }
259
258
  };
260
259
 
261
260
  <Form onSubmit={handleSubmit} />;
@@ -272,15 +271,15 @@ const handleSubmit = (data: z.infer<typeof schema>) => {
272
271
 
273
272
  ```tsx
274
273
  const schema = makeFormSchema({
275
- email: { type: "email" },
276
- password: { type: "string" },
274
+ email: { type: "email" },
275
+ password: { type: "string" },
277
276
  });
278
277
 
279
278
  const Form = createFormValidator(schema);
280
279
 
281
280
  const defaultValues = {
282
- email: "example@example.com",
283
- password: "",
281
+ email: "example@example.com",
282
+ password: "",
284
283
  };
285
284
 
286
285
  <Form defaultValues={defaultValues} />;
@@ -291,17 +290,17 @@ const defaultValues = {
291
290
  - Type: `React.ReactNode`
292
291
 
293
292
  - Description:
294
- The children prop accepts form inputs and other JSX elements to be rendered inside the Form component. It specifically expects components like Form.Input, which is designed to integrate with the form's validation logic. The Form.Input component handles binding form fields to the Zod schema, ensuring that validation rules are properly applied.
293
+ The children prop accepts form inputs and other JSX elements to be rendered inside the Form component. It specifically expects components like `Form.Input`, which is designed to integrate with the form's validation logic. The `Form.Input` component handles binding form fields to the Zod schema, ensuring that validation rules are properly applied.
295
294
 
296
- You can include Form.Input for each form field, along with any other elements like buttons or additional form elements. The Form.Input automatically manages validation states based on the schema.
295
+ You can include `Form.Input` for each form field, along with any other elements like buttons or additional form elements. The `Form.Input` automatically manages validation states based on the schema.
297
296
 
298
297
  - Example:
299
298
 
300
299
  ```tsx
301
300
  <Form onSubmit={onSubmit} defaultValues={defaultValues}>
302
- <Form.Input type="email" name="email" placeholder="Email" />
303
- <Form.Input type="password" name="password" placeholder="Password" />
304
- <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>
305
304
  </Form>
306
305
  ```
307
306
 
@@ -326,15 +325,18 @@ The Form.Input component is a form input field that is connected to a Zod-based
326
325
 
327
326
  **Props:**
328
327
 
329
- - name: `keyof z.infer<T>`
328
+ name: `keyof z.infer<T>`
329
+
330
330
  - Description:
331
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.
332
332
 
333
- - label: `string` (optional)
333
+ label: `string` (optional)
334
+
334
335
  - Description:
335
336
  A label for the form field. This is used for displaying a descriptive text for the input field.
336
337
 
337
- - ...props: `React.HTMLAttributes<HTMLInputElement>`
338
+ ...props: `React.InputHTMLAttributes<HTMLInputElement>`
339
+
338
340
  - Description:
339
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.
340
342
 
@@ -355,14 +357,14 @@ The makeFormSchema is a function that generates a Zod schema based on the provid
355
357
 
356
358
  ```tsx
357
359
  const schema = makeFormSchema({
358
- email: { type: "email" },
359
- password: {
360
- type: "string",
361
- minLength: {
362
- value: 8,
363
- message: "Password must be at least 8 characters long",
364
- },
365
- },
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
+ },
366
368
  });
367
369
  ```
368
370
 
@@ -372,14 +374,17 @@ This schema can then be passed to the Form component returned by `createFormVali
372
374
 
373
375
  1. The `createFormValidator` has been changed and now it is accepting an argument which is a type of zod schema generated through `makeFormSchema` or directly from `Zod` and the rest of the logic will be the same as before.
374
376
 
375
- In order to give proper type support for JavaScript project and to simplify the defining process this change has been made.
377
+ In order to give proper type support for JavaScript projects and to simplify the defining process this change has been made.
376
378
 
377
379
  **From this**
378
- ```tsx
380
+
381
+ ```tsx
379
382
  const Form = createFormValidator<typeof formSchema>(); ❌
380
383
  ```
384
+
381
385
  **To this**
382
- ```tsx
386
+
387
+ ```tsx
383
388
  const Form = createFormValidator(formSchema); ✔
384
389
  ```
385
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": "1.0.9",
6
+ "version": "2.0.1",
7
7
  "type": "module",
8
8
  "keywords": [
9
9
  "react",