formaze 2.2.0 → 2.3.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/dist/index.d.ts +113 -16
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import {
|
|
2
|
+
import { DefaultValues, SubmitHandler } from "react-hook-form";
|
|
3
3
|
|
|
4
4
|
export interface FormProps<T extends z.ZodSchema>
|
|
5
5
|
extends React.FormHTMLAttributes<HTMLFormElement> {
|
|
@@ -12,58 +12,58 @@ export interface FormProps<T extends z.ZodSchema>
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
// @ts-ignore
|
|
15
|
-
|
|
15
|
+
interface InputProps<T extends z.ZodSchema>
|
|
16
16
|
extends React.InputHTMLAttributes<HTMLInputElement> {
|
|
17
17
|
name: keyof z.infer<T>;
|
|
18
|
-
label
|
|
18
|
+
label?: string;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
interface FormFieldProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
22
22
|
name: string;
|
|
23
23
|
children: React.ReactNode;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
type OptionalAndCustomMessageConfig = {
|
|
27
27
|
optional?: boolean;
|
|
28
28
|
customMessage?: string;
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
type StringFieldConfig = {
|
|
32
32
|
type: "string";
|
|
33
33
|
minLength?: { value: number; message?: string };
|
|
34
34
|
maxLength?: { value: number; message?: string };
|
|
35
35
|
regex?: { value: RegExp; message?: string };
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
type EmailFieldConfig = {
|
|
39
39
|
type: "email";
|
|
40
40
|
regex?: { value: RegExp; message?: string };
|
|
41
41
|
};
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
type PasswordFieldConfig = {
|
|
44
44
|
type: "password";
|
|
45
45
|
minLength?: { value: number; message?: string };
|
|
46
46
|
maxLength?: { value: number; message?: string };
|
|
47
47
|
regex?: { value: RegExp; message?: string };
|
|
48
48
|
};
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
type NumberFieldConfig = {
|
|
51
51
|
type: "number";
|
|
52
52
|
min?: { value: number | string | Date; message?: string };
|
|
53
53
|
max?: { value: number | string | Date; message?: string };
|
|
54
54
|
};
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
type DateFieldConfig = {
|
|
57
57
|
type: "date";
|
|
58
58
|
min?: { value: string; message?: string };
|
|
59
59
|
max?: { value: string; message?: string };
|
|
60
60
|
};
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
type BooleanFieldConfig = {
|
|
63
63
|
type: "boolean";
|
|
64
64
|
};
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
type FieldConfig = OptionalAndCustomMessageConfig &
|
|
67
67
|
(
|
|
68
68
|
| StringFieldConfig
|
|
69
69
|
| EmailFieldConfig
|
|
@@ -73,13 +73,11 @@ export type FieldConfig = OptionalAndCustomMessageConfig &
|
|
|
73
73
|
| BooleanFieldConfig
|
|
74
74
|
);
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
type SchemaConfig<T extends Record<string, FieldConfig>> = {
|
|
77
77
|
[K in keyof T]: T[K];
|
|
78
78
|
};
|
|
79
79
|
|
|
80
|
-
|
|
81
|
-
T extends SchemaConfig<Record<string, FieldConfig>>
|
|
82
|
-
> = {
|
|
80
|
+
type SchemaKeyValuePair<T extends SchemaConfig<Record<string, FieldConfig>>> = {
|
|
83
81
|
[K in keyof T]: T[K]["optional"] extends true
|
|
84
82
|
? z.ZodOptional<
|
|
85
83
|
T[K]["type"] extends "email" | "password" | "string"
|
|
@@ -102,3 +100,102 @@ export type SchemaKeyValuePair<
|
|
|
102
100
|
? z.ZodDate
|
|
103
101
|
: z.ZodTypeAny;
|
|
104
102
|
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Generates a Form component with built-in validation using Zod and React Hook Form.
|
|
106
|
+
* The returned Form component also includes an Input component for form fields with automatic schema-based validation.
|
|
107
|
+
*
|
|
108
|
+
* @template T - The Zod schema used to define the structure and validation rules for the form.
|
|
109
|
+
* @param {T} formSchema - Zod schema generated through makeFormSchema or directly from Zod.
|
|
110
|
+
* @returns {{
|
|
111
|
+
* ({schema, children, className, onSubmit, mode, ...props}: FormProps<T>): JSX.Element;
|
|
112
|
+
* Input: ({label, name, className, ...props}: InputProps<T>) => JSX.Element;
|
|
113
|
+
* }} - The generated Form component that supports schema-based form handling and an Input component for each form field.
|
|
114
|
+
*
|
|
115
|
+
* @component Form
|
|
116
|
+
* @param {T} schema - Optional Zod schema defining the form fields and validation rules.
|
|
117
|
+
* @param {SubmitHandler<z.infer<T>>} onSubmit - Function to handle form submission after successful validation.
|
|
118
|
+
* @param {DefaultValues<z.infer<T>>} [defaultValues] - Optional default values to pre-populate the form fields.
|
|
119
|
+
* @param {React.ReactNode} children - Form fields and other JSX components, including Form.Input components.
|
|
120
|
+
* @param {string} [className] - Optional class for the form element.
|
|
121
|
+
* @param {string} mode - Optional mode for when to trigger validation error. Default is onSubmit.
|
|
122
|
+
*
|
|
123
|
+
* @component Input
|
|
124
|
+
* @param {keyof z.infer<T>} name - The name of the form field, matching the key of the schema.
|
|
125
|
+
* @param {string} label - Label for the form input.
|
|
126
|
+
* @param {string} [className] - Optional class for the input element.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* // Create a form schema
|
|
130
|
+
* const schema = makeFormSchema({
|
|
131
|
+
* email: { type: "email" },
|
|
132
|
+
* password: { type: "string", minLength: { value: 8 } },
|
|
133
|
+
* });
|
|
134
|
+
*
|
|
135
|
+
* // Generate the Form component
|
|
136
|
+
* const MyForm = createFormValidator(schema);
|
|
137
|
+
*
|
|
138
|
+
* <MyForm onSubmit={(data) => {}}>
|
|
139
|
+
* <MyForm.Input name="email" label="Email" type="email" />
|
|
140
|
+
* <MyForm.Input name="password" label="Password" type="password" />
|
|
141
|
+
* <button type="submit">Submit</button>
|
|
142
|
+
* </MyForm>;
|
|
143
|
+
*/
|
|
144
|
+
|
|
145
|
+
declare const createFormValidator: <T extends z.ZodSchema>(
|
|
146
|
+
formSchema: T
|
|
147
|
+
) => {
|
|
148
|
+
({
|
|
149
|
+
schema,
|
|
150
|
+
onSubmit,
|
|
151
|
+
children,
|
|
152
|
+
className,
|
|
153
|
+
...props
|
|
154
|
+
}: FormProps<T>): JSX.Element;
|
|
155
|
+
Input({ name, label, className, ...props }: InputProps<T>): JSX.Element;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Generates a Zod schema based on the provided configuration.
|
|
160
|
+
* This utility function is useful for creating dynamic form schemas with validation rules.
|
|
161
|
+
*
|
|
162
|
+
* @template T - The shape of the schema config, where T extends a record of field configurations.
|
|
163
|
+
*
|
|
164
|
+
* @param {SchemaConfig<T>} schemaConfig - An object that defines the fields and validation rules for the form.
|
|
165
|
+
* Each key in the object represents a form field, and its value specifies the field's type, optional status, and any additional validation logic.
|
|
166
|
+
*
|
|
167
|
+
* @returns {z.ZodObject<T>} - Returns a Zod schema that can be used for form validation.
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* const schema = makeFormSchema({
|
|
171
|
+
* email: { type: "email" },
|
|
172
|
+
* password: {
|
|
173
|
+
* type: "string",
|
|
174
|
+
* minLength: { value: 8 },
|
|
175
|
+
* },
|
|
176
|
+
* });
|
|
177
|
+
*/
|
|
178
|
+
|
|
179
|
+
declare const makeFormSchema: <
|
|
180
|
+
T extends SchemaConfig<Record<string, FieldConfig>>
|
|
181
|
+
>(
|
|
182
|
+
schemaConfig: SchemaConfig<T>
|
|
183
|
+
) => z.ZodObject<
|
|
184
|
+
SchemaKeyValuePair<T>,
|
|
185
|
+
"strip",
|
|
186
|
+
z.ZodTypeAny,
|
|
187
|
+
{
|
|
188
|
+
[k in keyof z.objectUtil.addQuestionMarks<
|
|
189
|
+
z.baseObjectOutputType<SchemaKeyValuePair<T>>,
|
|
190
|
+
any
|
|
191
|
+
>]: z.objectUtil.addQuestionMarks<
|
|
192
|
+
z.baseObjectOutputType<SchemaKeyValuePair<T>>,
|
|
193
|
+
any
|
|
194
|
+
>[k];
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
[k_1 in keyof z.baseObjectInputType<
|
|
198
|
+
SchemaKeyValuePair<T>
|
|
199
|
+
>]: z.baseObjectInputType<SchemaKeyValuePair<T>>[k_1];
|
|
200
|
+
}
|
|
201
|
+
>;
|