formaze 1.0.3 → 1.0.4
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/README.md +180 -65
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -27,49 +27,49 @@ import "formaze/dist/style.css";
|
|
|
27
27
|
|
|
28
28
|
// create the validation schema
|
|
29
29
|
const formSchema = useFormSchema({
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
},
|
|
30
|
+
email: {
|
|
31
|
+
type: "email",
|
|
32
|
+
},
|
|
33
|
+
password: {
|
|
34
|
+
type: "password",
|
|
35
|
+
minLength: {
|
|
36
|
+
value: 8, // by default 6
|
|
38
37
|
},
|
|
38
|
+
},
|
|
39
39
|
});
|
|
40
40
|
|
|
41
41
|
// create the form
|
|
42
42
|
const Form = createFormValidator<typeof formSchema>();
|
|
43
43
|
|
|
44
44
|
export function RegistrationForm() {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
45
|
+
function handleSubmit(data: z.infer<typeof formSchema>) {
|
|
46
|
+
const result = formSchema.safeParse(data);
|
|
47
|
+
|
|
48
|
+
if (!result.success) throw new Error("Invalid inputs");
|
|
49
|
+
|
|
50
|
+
console.log(data);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<Form schema={formSchema} onSubmit={handleSubmit}>
|
|
55
|
+
<Form.Input
|
|
56
|
+
type="email"
|
|
57
|
+
name="email"
|
|
58
|
+
placeholder="Enter your email"
|
|
59
|
+
/>
|
|
60
|
+
<Form.Input
|
|
61
|
+
type="password"
|
|
62
|
+
name="password"
|
|
63
|
+
placeholder="Enter your password"
|
|
64
|
+
/>
|
|
65
|
+
<button
|
|
66
|
+
className="rounded-md bg-blue-500 py-1 px-3 text-white hover:bg-blue-600"
|
|
67
|
+
type="submit"
|
|
68
|
+
>
|
|
69
|
+
Submit
|
|
70
|
+
</button>
|
|
71
|
+
</Form>
|
|
72
|
+
);
|
|
73
73
|
}
|
|
74
74
|
```
|
|
75
75
|
|
|
@@ -79,33 +79,32 @@ The useFormSchema hook allows you to define validation rules for each field in t
|
|
|
79
79
|
|
|
80
80
|
```js
|
|
81
81
|
const formSchema = useFormSchema({
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
},
|
|
82
|
+
email: {
|
|
83
|
+
type: "email", customMessage: "A valid email is required",
|
|
84
|
+
},
|
|
85
|
+
password: {
|
|
86
|
+
type: "password",
|
|
87
|
+
minLength: {
|
|
88
|
+
value: 8,
|
|
89
|
+
message: "Password must be at least 8 characters",
|
|
90
|
+
},
|
|
91
|
+
maxLength: {
|
|
92
|
+
value: 16,
|
|
93
|
+
message: "Password must be less than 16 characters",
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
username: {
|
|
97
|
+
type: "string",
|
|
98
|
+
maxLength: {
|
|
99
|
+
value: 12,
|
|
100
|
+
message: "Username must be less than 12 characters",
|
|
101
|
+
},
|
|
102
|
+
customMessage: "Username must not be empty",
|
|
103
|
+
},
|
|
104
|
+
terms: {
|
|
105
|
+
type: "boolean",
|
|
106
|
+
customMessage: "You must accept the terms to continue",
|
|
107
|
+
},
|
|
109
108
|
});
|
|
110
109
|
```
|
|
111
110
|
|
|
@@ -115,8 +114,8 @@ Though, you can directly use `zod` to define schema as well and pass it to the F
|
|
|
115
114
|
import { z } from "zod";
|
|
116
115
|
|
|
117
116
|
const formSchema = z.object({
|
|
118
|
-
|
|
119
|
-
|
|
117
|
+
email: z.string().email(),
|
|
118
|
+
name: z.string().min(3, {message: "Required"})
|
|
120
119
|
});
|
|
121
120
|
```
|
|
122
121
|
|
|
@@ -192,4 +191,120 @@ You just have to add `"use client"` directive at the top of your file where you
|
|
|
192
191
|
// your code
|
|
193
192
|
```
|
|
194
193
|
|
|
194
|
+
## Components and Hooks
|
|
195
|
+
|
|
196
|
+
### `createFormValidator`
|
|
197
|
+
|
|
198
|
+
The createFormValidator function returns a Form component with built-in form validation using Zod and React Hook Form. It simplifies the creation of forms that adhere to a Zod schema for type-safe validation.
|
|
199
|
+
|
|
200
|
+
**Returned Form Component Props**:
|
|
201
|
+
|
|
202
|
+
**schema: `T`**
|
|
203
|
+
|
|
204
|
+
- Type: `T` (A Zod schema)
|
|
205
|
+
|
|
206
|
+
- Description:
|
|
207
|
+
The 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.
|
|
208
|
+
|
|
209
|
+
- Example:
|
|
210
|
+
|
|
211
|
+
```tsx
|
|
212
|
+
const schema = useFormSchema({
|
|
213
|
+
email: { type: "email" },
|
|
214
|
+
password: { type: "string", minLength: { value: 8 } },
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
const Form = createFormValidator<typeof schema>();
|
|
218
|
+
|
|
219
|
+
<Form schema={schema} onSubmit={handleSubmit} />;
|
|
220
|
+
```
|
|
221
|
+
**onSubmit: `SubmitHandler<z.infer<T>>`**
|
|
222
|
+
|
|
223
|
+
- Type: `SubmitHandler<z.infer<T>>`
|
|
224
|
+
|
|
225
|
+
- Description:
|
|
226
|
+
The onSubmit prop accepts a function that is called when the form is successfully submitted. The function receives the form's validated data as its argument, which is inferred from the useFormSchema (using Zod) schema (z.infer<T>).
|
|
227
|
+
|
|
228
|
+
- Example:
|
|
229
|
+
|
|
230
|
+
```tsx
|
|
231
|
+
const schema = useFormSchema({
|
|
232
|
+
email: { type: "email" },
|
|
233
|
+
password: { type: "string" },
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
const onSubmit = (data: z.infer<typeof schema>) => {
|
|
237
|
+
console.log(data); // { email: "test@example.com", password: "securePassword" }
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
<Form schema={schema} onSubmit={onSubmit} />;
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
**defaultValues?: `DefaultValues<z.infer<T>>`**
|
|
244
|
+
- Type: `DefaultValues<z.infer<T>>`
|
|
245
|
+
|
|
246
|
+
- Description:
|
|
247
|
+
This optional prop allows you to specify default values for the form fields. The keys should exactly match the fields defined in the useFormSchema/Zod schema, and the values should be of the appropriate type.
|
|
248
|
+
|
|
249
|
+
- Example:
|
|
250
|
+
|
|
251
|
+
```tsx
|
|
252
|
+
const schema = useFormSchema({
|
|
253
|
+
email: { type: "email" },
|
|
254
|
+
password: { type: "string" },
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
const defaultValues = {
|
|
258
|
+
email: "example@example.com",
|
|
259
|
+
password: "",
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
<Form schema={schema} defaultValues={defaultValues} onSubmit={onSubmit} />;
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
**children: React.ReactNode**
|
|
266
|
+
- Type: `React.ReactNode`
|
|
267
|
+
|
|
268
|
+
- Description:
|
|
269
|
+
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.
|
|
270
|
+
|
|
271
|
+
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.
|
|
272
|
+
|
|
273
|
+
- Example:
|
|
274
|
+
|
|
275
|
+
```tsx
|
|
276
|
+
<Form schema={schema} onSubmit={onSubmit} defaultValues={defaultValues}>
|
|
277
|
+
<Form.Input name="email" placeholder="Email" />
|
|
278
|
+
<Form.Input type="password" name="password" placeholder="Password" />
|
|
279
|
+
<button type="submit">Submit</button>
|
|
280
|
+
</Form>
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### `useFormSchema`
|
|
284
|
+
|
|
285
|
+
useFormSchema is a utility function that generates a Zod schema based on the provided configuration. It supports various field types such as string, email, password, number, date, and boolean, along with validation rules like minLength, maxLength, regex, etc.
|
|
286
|
+
|
|
287
|
+
#### Arguments
|
|
288
|
+
|
|
289
|
+
**schemaConfig: `SchemaConfig<T>`**
|
|
290
|
+
|
|
291
|
+
- Type: `SchemaConfig<T>`
|
|
292
|
+
|
|
293
|
+
- Description:
|
|
294
|
+
The schemaConfig defines the fields and validation rules for the schema. Each field in the config should specify its type (string, email, password, etc.), whether it is optional, and any additional validation logic, such as minLength, maxLength, or regex.
|
|
295
|
+
|
|
296
|
+
- Example:
|
|
297
|
+
|
|
298
|
+
```tsx
|
|
299
|
+
const schema = useFormSchema({
|
|
300
|
+
email: { type: "email" },
|
|
301
|
+
password: {
|
|
302
|
+
type: "string",
|
|
303
|
+
minLength: { value: 8, message: "Password must be at least 8 characters long" },
|
|
304
|
+
},
|
|
305
|
+
});
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
This schema can then be passed to the Form component returned by `createFormValidator`.
|
|
309
|
+
|
|
195
310
|
## (That's it!)
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "An easy form validation package for react, built by using React hook form and zod library",
|
|
4
4
|
"main": "dist/formaze.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
|
-
"version": "1.0.
|
|
6
|
+
"version": "1.0.4",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"keywords": [
|
|
9
9
|
"react",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
],
|
|
15
15
|
"repository": {
|
|
16
16
|
"type": "git",
|
|
17
|
-
"url": "https://github.com/Its-Samir/formaze"
|
|
17
|
+
"url": "git+https://github.com/Its-Samir/formaze.git"
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"scripts": {
|