formaze 1.0.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Samir Kotal
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,195 @@
1
+ # Formaze: A Customizable Form Validation package for React
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 handle complex form validation logic efficiently with proper type-safety.
4
+
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`.
7
+ - Custom error messages.
8
+
9
+ ## Installation
10
+
11
+ You can install the package via npm.
12
+
13
+ ```bash
14
+ npm install formaze
15
+ ```
16
+
17
+ ## Quickstart
18
+
19
+ ```tsx
20
+ // if you are using next.js (app router) then activate the below line "use client" directive otherwise remove it
21
+
22
+ // "use client"
23
+ import { z } from "zod";
24
+ import { useFormSchema, createFormValidator } from "formaze";
25
+ /* for pre-styled css (check the styling guide below) */
26
+ import "formaze/dist/style.css";
27
+
28
+ // create the validation schema
29
+ const formSchema = useFormSchema({
30
+ email: {
31
+ type: "email",
32
+ },
33
+ password: {
34
+ type: "password",
35
+ minLength: {
36
+ value: 8, // by default 6
37
+ },
38
+ },
39
+ });
40
+
41
+ // create the form
42
+ const Form = createFormValidator<typeof formSchema>();
43
+
44
+ export function RegistrationForm() {
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
+ }
74
+ ```
75
+
76
+ ## Define Validation Schema
77
+
78
+ The useFormSchema hook 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`.
79
+
80
+ ```js
81
+ const formSchema = useFormSchema({
82
+ email: {
83
+ type: "email",
84
+ customMessage: "A valid email is required",
85
+ },
86
+ password: {
87
+ type: "password",
88
+ minLength: {
89
+ value: 8,
90
+ message: "Password must be at least 8 characters",
91
+ },
92
+ maxLength: {
93
+ value: 16,
94
+ message: "Password must be less than 16 characters",
95
+ },
96
+ },
97
+ username: {
98
+ type: "string",
99
+ maxLength: {
100
+ value: 12,
101
+ message: "Username must be less than 12 characters",
102
+ },
103
+ customMessage: "Username must not be empty",
104
+ },
105
+ terms: {
106
+ type: "boolean",
107
+ customMessage: "You must accept the terms to continue",
108
+ },
109
+ });
110
+ ```
111
+
112
+ Though, you can directly use `zod` to define schema as well and pass it to the Form props created through `createFormValidator` method.
113
+
114
+ ```tsx
115
+ import { z } from "zod";
116
+
117
+ const formSchema = z.object({
118
+ email: z.string().email(),
119
+ name: z.string().min(3, {message: "Required"})
120
+ });
121
+ ```
122
+
123
+ ### Validation options
124
+
125
+ You can specify the following validation options for each field:
126
+
127
+ - **minLength**: Specifies the minimum length for `string` and `password` fields.
128
+ - **maxLength**: Specifies the maximum length for `string` and `password` fields.
129
+ - **min**: Defines the minimum value for `number` and `date` fields.
130
+ - **max**: Defines the maximum value for `number` and `date` fields.
131
+ - **regex**: Allows defining a regular expression pattern for string validation.
132
+ - **optional**: Marks a field as optional.
133
+
134
+ ## Styling
135
+
136
+ - For Tailwind (pre-styled)
137
+
138
+ Follow the [Official Tailwind Docs](https://tailwindcss.com/docs/installation/framework-guides) for initializing project with vite or next.js
139
+
140
+ 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
141
+
142
+ ```js
143
+ import "formaze/dist/style.css";
144
+ ```
145
+
146
+ - For Pure CSS
147
+ Here are the css classes with default styles, add it into main css file
148
+ ```css
149
+ .form-control {
150
+ display: flex;
151
+ flex-direction: column;
152
+ justify-content: center;
153
+ gap: 1rem;
154
+ }
155
+
156
+ .form-control .form-field {
157
+ display: flex;
158
+ flex-direction: column;
159
+ justify-content: center;
160
+ }
161
+
162
+ .form-control .form-field .form-input {
163
+ padding: 10px 1rem;
164
+ border-radius: 10px;
165
+ border-width: 1px;
166
+ border-color: rgb(209 213 219);
167
+ }
168
+
169
+ .form-control .form-field .form-input:focus {
170
+ border-color: rgb(59 130 246);
171
+ outline: none;
172
+ }
173
+
174
+ .form-control .form-field-error-text {
175
+ color: rgb(220, 38, 38);
176
+ font-size: 0.875rem;
177
+ }
178
+
179
+ /* or */
180
+
181
+ .form-control .form-field-error {
182
+ color: rgb(220, 38, 38);
183
+ }
184
+ ```
185
+
186
+ ## For Next.js (app router)
187
+
188
+ You just have to add `"use client"` directive at the top of your file where you are using this form and its related methods
189
+
190
+ ```tsx
191
+ "use client"
192
+ // your code
193
+ ```
194
+
195
+ ## (That's it!)