remix-validated-form 0.0.4 → 1.1.1-beta.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.
- package/.eslintcache +1 -1
- package/.prettierignore +2 -0
- package/README.md +76 -19
- package/browser/ValidatedForm.d.ts +22 -0
- package/browser/ValidatedForm.js +5 -2
- package/browser/flatten.d.ts +4 -0
- package/browser/flatten.js +35 -0
- package/browser/hooks.d.ts +28 -3
- package/browser/hooks.js +17 -2
- package/browser/index.d.ts +2 -0
- package/browser/index.js +2 -0
- package/browser/internal/flatten.d.ts +4 -0
- package/browser/internal/flatten.js +35 -0
- package/browser/internal/formContext.d.ts +18 -0
- package/browser/server.d.ts +5 -0
- package/browser/server.js +5 -0
- package/browser/test-data/testFormData.d.ts +15 -0
- package/browser/test-data/testFormData.js +46 -0
- package/browser/validation/createValidator.d.ts +7 -0
- package/browser/validation/createValidator.js +19 -0
- package/browser/validation/types.d.ts +14 -2
- package/browser/validation/validation.test.js +221 -8
- package/browser/validation/withYup.d.ts +3 -0
- package/browser/validation/withYup.js +31 -25
- package/browser/validation/withZod.d.ts +6 -0
- package/browser/validation/withZod.js +50 -0
- package/build/ValidatedForm.d.ts +22 -0
- package/build/ValidatedForm.js +5 -2
- package/build/flatten.d.ts +4 -0
- package/build/flatten.js +43 -0
- package/build/hooks.d.ts +28 -3
- package/build/hooks.js +20 -2
- package/build/index.d.ts +2 -0
- package/build/index.js +2 -0
- package/build/internal/flatten.d.ts +4 -0
- package/build/internal/flatten.js +43 -0
- package/build/internal/formContext.d.ts +18 -0
- package/build/server.d.ts +5 -0
- package/build/server.js +5 -0
- package/build/test-data/testFormData.d.ts +15 -0
- package/build/test-data/testFormData.js +50 -0
- package/build/validation/createValidator.d.ts +7 -0
- package/build/validation/createValidator.js +23 -0
- package/build/validation/types.d.ts +14 -2
- package/build/validation/validation.test.js +221 -8
- package/build/validation/withYup.d.ts +3 -0
- package/build/validation/withYup.js +31 -25
- package/build/validation/withZod.d.ts +6 -0
- package/build/validation/withZod.js +57 -0
- package/package.json +20 -14
- package/sample-app/.env +7 -0
- package/sample-app/README.md +53 -0
- package/sample-app/app/components/ErrorBox.tsx +34 -0
- package/sample-app/app/components/FormInput.tsx +40 -0
- package/sample-app/app/components/FormSelect.tsx +37 -0
- package/sample-app/app/components/SubjectForm.tsx +150 -0
- package/sample-app/app/entry.client.tsx +4 -0
- package/sample-app/app/entry.server.tsx +21 -0
- package/sample-app/app/root.tsx +92 -0
- package/sample-app/app/routes/index.tsx +5 -0
- package/sample-app/app/routes/subjects/$id.edit.tsx +98 -0
- package/sample-app/app/routes/subjects/index.tsx +112 -0
- package/sample-app/app/routes/subjects/new.tsx +46 -0
- package/sample-app/app/services/db.server.ts +23 -0
- package/sample-app/app/types.ts +6 -0
- package/sample-app/package-lock.json +10890 -0
- package/sample-app/package.json +36 -0
- package/sample-app/prisma/dev.db +0 -0
- package/sample-app/prisma/schema.prisma +34 -0
- package/sample-app/public/favicon.ico +0 -0
- package/sample-app/remix.config.js +10 -0
- package/sample-app/remix.env.d.ts +2 -0
@@ -1,5 +1,8 @@
|
|
1
1
|
import * as yup from "yup";
|
2
|
+
import { z } from "zod";
|
2
3
|
import { withYup } from "..";
|
4
|
+
import { TestFormData } from "../test-data/testFormData";
|
5
|
+
import { withZod } from "./withZod";
|
3
6
|
const validationTestCases = [
|
4
7
|
{
|
5
8
|
name: "yup",
|
@@ -7,6 +10,37 @@ const validationTestCases = [
|
|
7
10
|
firstName: yup.string().required(),
|
8
11
|
lastName: yup.string().required(),
|
9
12
|
age: yup.number(),
|
13
|
+
address: yup
|
14
|
+
.object({
|
15
|
+
streetAddress: yup.string().required(),
|
16
|
+
city: yup.string().required(),
|
17
|
+
country: yup.string().required(),
|
18
|
+
})
|
19
|
+
.required(),
|
20
|
+
pets: yup.array().of(yup.object({
|
21
|
+
animal: yup.string().required(),
|
22
|
+
name: yup.string().required(),
|
23
|
+
})),
|
24
|
+
})),
|
25
|
+
},
|
26
|
+
{
|
27
|
+
name: "zod",
|
28
|
+
validator: withZod(z.object({
|
29
|
+
firstName: z.string().nonempty(),
|
30
|
+
lastName: z.string().nonempty(),
|
31
|
+
age: z.optional(z.number()),
|
32
|
+
address: z.preprocess((value) => (value == null ? {} : value), z.object({
|
33
|
+
streetAddress: z.string().nonempty(),
|
34
|
+
city: z.string().nonempty(),
|
35
|
+
country: z.string().nonempty(),
|
36
|
+
})),
|
37
|
+
pets: z
|
38
|
+
.object({
|
39
|
+
animal: z.string().nonempty(),
|
40
|
+
name: z.string().nonempty(),
|
41
|
+
})
|
42
|
+
.array()
|
43
|
+
.optional(),
|
10
44
|
})),
|
11
45
|
},
|
12
46
|
];
|
@@ -16,41 +50,220 @@ describe("Validation", () => {
|
|
16
50
|
describe.each(validationTestCases)("Adapter for $name", ({ validator }) => {
|
17
51
|
describe("validate", () => {
|
18
52
|
it("should return the data when valid", () => {
|
19
|
-
const
|
53
|
+
const person = {
|
20
54
|
firstName: "John",
|
21
55
|
lastName: "Doe",
|
22
56
|
age: 30,
|
57
|
+
address: {
|
58
|
+
streetAddress: "123 Main St",
|
59
|
+
city: "Anytown",
|
60
|
+
country: "USA",
|
61
|
+
},
|
62
|
+
pets: [{ animal: "dog", name: "Fido" }],
|
23
63
|
};
|
24
|
-
expect(validator.validate(
|
25
|
-
data:
|
64
|
+
expect(validator.validate(person)).toEqual({
|
65
|
+
data: person,
|
26
66
|
error: undefined,
|
27
67
|
});
|
28
68
|
});
|
29
69
|
it("should return field errors when invalid", () => {
|
30
|
-
const obj = { age: "hi!" };
|
70
|
+
const obj = { age: "hi!", pets: [{ animal: "dog" }] };
|
31
71
|
expect(validator.validate(obj)).toEqual({
|
32
72
|
data: undefined,
|
33
73
|
error: {
|
34
74
|
firstName: anyString,
|
35
75
|
lastName: anyString,
|
36
76
|
age: anyString,
|
77
|
+
"address.city": anyString,
|
78
|
+
"address.country": anyString,
|
79
|
+
"address.streetAddress": anyString,
|
80
|
+
"pets[0].name": anyString,
|
37
81
|
},
|
38
82
|
});
|
39
83
|
});
|
84
|
+
it("should unflatten data when validating", () => {
|
85
|
+
const data = {
|
86
|
+
firstName: "John",
|
87
|
+
lastName: "Doe",
|
88
|
+
age: 30,
|
89
|
+
"address.streetAddress": "123 Main St",
|
90
|
+
"address.city": "Anytown",
|
91
|
+
"address.country": "USA",
|
92
|
+
"pets[0].animal": "dog",
|
93
|
+
"pets[0].name": "Fido",
|
94
|
+
};
|
95
|
+
expect(validator.validate(data)).toEqual({
|
96
|
+
data: {
|
97
|
+
firstName: "John",
|
98
|
+
lastName: "Doe",
|
99
|
+
age: 30,
|
100
|
+
address: {
|
101
|
+
streetAddress: "123 Main St",
|
102
|
+
city: "Anytown",
|
103
|
+
country: "USA",
|
104
|
+
},
|
105
|
+
pets: [{ animal: "dog", name: "Fido" }],
|
106
|
+
},
|
107
|
+
error: undefined,
|
108
|
+
});
|
109
|
+
});
|
110
|
+
it("should accept FormData directly and return errors", () => {
|
111
|
+
const formData = new TestFormData();
|
112
|
+
formData.set("firstName", "John");
|
113
|
+
formData.set("lastName", "Doe");
|
114
|
+
formData.set("address.streetAddress", "123 Main St");
|
115
|
+
formData.set("address.country", "USA");
|
116
|
+
formData.set("pets[0].animal", "dog");
|
117
|
+
expect(validator.validate(formData)).toEqual({
|
118
|
+
data: undefined,
|
119
|
+
error: {
|
120
|
+
"address.city": anyString,
|
121
|
+
"pets[0].name": anyString,
|
122
|
+
},
|
123
|
+
});
|
124
|
+
});
|
125
|
+
it("should accept FormData directly and return valid data", () => {
|
126
|
+
const formData = new TestFormData();
|
127
|
+
formData.set("firstName", "John");
|
128
|
+
formData.set("lastName", "Doe");
|
129
|
+
formData.set("address.streetAddress", "123 Main St");
|
130
|
+
formData.set("address.country", "USA");
|
131
|
+
formData.set("address.city", "Anytown");
|
132
|
+
formData.set("pets[0].animal", "dog");
|
133
|
+
formData.set("pets[0].name", "Fido");
|
134
|
+
expect(validator.validate(formData)).toEqual({
|
135
|
+
data: {
|
136
|
+
firstName: "John",
|
137
|
+
lastName: "Doe",
|
138
|
+
address: {
|
139
|
+
streetAddress: "123 Main St",
|
140
|
+
country: "USA",
|
141
|
+
city: "Anytown",
|
142
|
+
},
|
143
|
+
pets: [{ animal: "dog", name: "Fido" }],
|
144
|
+
},
|
145
|
+
error: undefined,
|
146
|
+
});
|
147
|
+
});
|
40
148
|
});
|
41
149
|
describe("validateField", () => {
|
42
150
|
it("should not return an error if field is valid", () => {
|
43
|
-
const
|
44
|
-
|
151
|
+
const person = {
|
152
|
+
firstName: "John",
|
153
|
+
lastName: {}, // invalid, but we should only be validating firstName
|
154
|
+
};
|
155
|
+
expect(validator.validateField(person, "firstName")).toEqual({
|
156
|
+
error: undefined,
|
157
|
+
});
|
158
|
+
});
|
159
|
+
it("should not return an error if a nested field is valid", () => {
|
160
|
+
const person = {
|
161
|
+
firstName: "John",
|
162
|
+
lastName: {},
|
163
|
+
address: {
|
164
|
+
streetAddress: "123 Main St",
|
165
|
+
city: "Anytown",
|
166
|
+
country: "USA",
|
167
|
+
},
|
168
|
+
pets: [{ animal: "dog", name: "Fido" }],
|
169
|
+
};
|
170
|
+
expect(validator.validateField(person, "address.streetAddress")).toEqual({
|
171
|
+
error: undefined,
|
172
|
+
});
|
173
|
+
expect(validator.validateField(person, "address.city")).toEqual({
|
174
|
+
error: undefined,
|
175
|
+
});
|
176
|
+
expect(validator.validateField(person, "address.country")).toEqual({
|
177
|
+
error: undefined,
|
178
|
+
});
|
179
|
+
expect(validator.validateField(person, "pets[0].animal")).toEqual({
|
180
|
+
error: undefined,
|
181
|
+
});
|
182
|
+
expect(validator.validateField(person, "pets[0].name")).toEqual({
|
45
183
|
error: undefined,
|
46
184
|
});
|
47
185
|
});
|
48
186
|
it("should return an error if field is invalid", () => {
|
49
|
-
const
|
50
|
-
|
187
|
+
const person = {
|
188
|
+
firstName: "John",
|
189
|
+
lastName: {},
|
190
|
+
address: {
|
191
|
+
streetAddress: "123 Main St",
|
192
|
+
city: 1234,
|
193
|
+
},
|
194
|
+
};
|
195
|
+
expect(validator.validateField(person, "lastName")).toEqual({
|
196
|
+
error: anyString,
|
197
|
+
});
|
198
|
+
});
|
199
|
+
it("should return an error if a nested field is invalid", () => {
|
200
|
+
const person = {
|
201
|
+
firstName: "John",
|
202
|
+
lastName: {},
|
203
|
+
address: {
|
204
|
+
streetAddress: "123 Main St",
|
205
|
+
city: 1234,
|
206
|
+
},
|
207
|
+
pets: [{ animal: "dog" }],
|
208
|
+
};
|
209
|
+
expect(validator.validateField(person, "address.country")).toEqual({
|
210
|
+
error: anyString,
|
211
|
+
});
|
212
|
+
expect(validator.validateField(person, "pets[0].name")).toEqual({
|
51
213
|
error: anyString,
|
52
214
|
});
|
53
215
|
});
|
54
216
|
});
|
55
217
|
});
|
56
218
|
});
|
219
|
+
describe("withZod", () => {
|
220
|
+
it("returns coherent errors for complex schemas", () => {
|
221
|
+
const schema = z.union([
|
222
|
+
z.object({
|
223
|
+
type: z.literal("foo"),
|
224
|
+
foo: z.string(),
|
225
|
+
}),
|
226
|
+
z.object({
|
227
|
+
type: z.literal("bar"),
|
228
|
+
bar: z.string(),
|
229
|
+
}),
|
230
|
+
]);
|
231
|
+
const obj = {
|
232
|
+
type: "foo",
|
233
|
+
bar: 123,
|
234
|
+
foo: 123,
|
235
|
+
};
|
236
|
+
expect(withZod(schema).validate(obj)).toEqual({
|
237
|
+
data: undefined,
|
238
|
+
error: {
|
239
|
+
type: anyString,
|
240
|
+
bar: anyString,
|
241
|
+
foo: anyString,
|
242
|
+
},
|
243
|
+
});
|
244
|
+
});
|
245
|
+
it("returns errors for fields that are unions", () => {
|
246
|
+
const schema = z.object({
|
247
|
+
field1: z.union([z.literal("foo"), z.literal("bar")]),
|
248
|
+
field2: z.union([z.literal("foo"), z.literal("bar")]),
|
249
|
+
});
|
250
|
+
const obj = {
|
251
|
+
field1: "a value",
|
252
|
+
// field2 missing
|
253
|
+
};
|
254
|
+
const validator = withZod(schema);
|
255
|
+
expect(validator.validate(obj)).toEqual({
|
256
|
+
data: undefined,
|
257
|
+
error: {
|
258
|
+
field1: anyString,
|
259
|
+
field2: anyString,
|
260
|
+
},
|
261
|
+
});
|
262
|
+
expect(validator.validateField(obj, "field1")).toEqual({
|
263
|
+
error: anyString,
|
264
|
+
});
|
265
|
+
expect(validator.validateField(obj, "field2")).toEqual({
|
266
|
+
error: anyString,
|
267
|
+
});
|
268
|
+
});
|
269
|
+
});
|
@@ -1,3 +1,6 @@
|
|
1
1
|
import type { AnyObjectSchema, InferType } from "yup";
|
2
2
|
import { Validator } from "./types";
|
3
|
+
/**
|
4
|
+
* Create a `Validator` using a `yup` schema.
|
5
|
+
*/
|
3
6
|
export declare const withYup: <Schema extends AnyObjectSchema>(validationSchema: Schema) => Validator<InferType<Schema>>;
|
@@ -1,3 +1,4 @@
|
|
1
|
+
import { createValidator } from "./createValidator";
|
1
2
|
const validationErrorToFieldErrors = (error) => {
|
2
3
|
const fieldErrors = {};
|
3
4
|
error.inner.forEach((innerError) => {
|
@@ -7,28 +8,33 @@ const validationErrorToFieldErrors = (error) => {
|
|
7
8
|
});
|
8
9
|
return fieldErrors;
|
9
10
|
};
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
error:
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
11
|
+
/**
|
12
|
+
* Create a `Validator` using a `yup` schema.
|
13
|
+
*/
|
14
|
+
export const withYup = (validationSchema) => {
|
15
|
+
return createValidator({
|
16
|
+
validate: (data) => {
|
17
|
+
try {
|
18
|
+
const validated = validationSchema.validateSync(data, {
|
19
|
+
abortEarly: false,
|
20
|
+
});
|
21
|
+
return { data: validated, error: undefined };
|
22
|
+
}
|
23
|
+
catch (err) {
|
24
|
+
return {
|
25
|
+
error: validationErrorToFieldErrors(err),
|
26
|
+
data: undefined,
|
27
|
+
};
|
28
|
+
}
|
29
|
+
},
|
30
|
+
validateField: (data, field) => {
|
31
|
+
try {
|
32
|
+
validationSchema.validateSyncAt(field, data);
|
33
|
+
return {};
|
34
|
+
}
|
35
|
+
catch (err) {
|
36
|
+
return { error: err.message };
|
37
|
+
}
|
38
|
+
},
|
39
|
+
});
|
40
|
+
};
|
@@ -0,0 +1,50 @@
|
|
1
|
+
import isEqual from "lodash/isEqual";
|
2
|
+
import toPath from "lodash/toPath";
|
3
|
+
import { createValidator } from "./createValidator";
|
4
|
+
const getIssuesForError = (err) => {
|
5
|
+
return err.issues.flatMap((issue) => {
|
6
|
+
if ("unionErrors" in issue) {
|
7
|
+
return issue.unionErrors.flatMap((err) => getIssuesForError(err));
|
8
|
+
}
|
9
|
+
else {
|
10
|
+
return [issue];
|
11
|
+
}
|
12
|
+
});
|
13
|
+
};
|
14
|
+
function pathToString(array) {
|
15
|
+
return array.reduce(function (string, item) {
|
16
|
+
var prefix = string === "" ? "" : ".";
|
17
|
+
return string + (isNaN(Number(item)) ? prefix + item : "[" + item + "]");
|
18
|
+
}, "");
|
19
|
+
}
|
20
|
+
/**
|
21
|
+
* Create a validator using a `zod` schema.
|
22
|
+
*/
|
23
|
+
export function withZod(zodSchema) {
|
24
|
+
return createValidator({
|
25
|
+
validate: (value) => {
|
26
|
+
const result = zodSchema.safeParse(value);
|
27
|
+
if (result.success)
|
28
|
+
return { data: result.data, error: undefined };
|
29
|
+
const fieldErrors = {};
|
30
|
+
getIssuesForError(result.error).forEach((issue) => {
|
31
|
+
const path = pathToString(issue.path);
|
32
|
+
if (!fieldErrors[path])
|
33
|
+
fieldErrors[path] = issue.message;
|
34
|
+
});
|
35
|
+
return { error: fieldErrors, data: undefined };
|
36
|
+
},
|
37
|
+
validateField: (data, field) => {
|
38
|
+
var _a;
|
39
|
+
const result = zodSchema.safeParse(data);
|
40
|
+
if (result.success)
|
41
|
+
return { error: undefined };
|
42
|
+
return {
|
43
|
+
error: (_a = getIssuesForError(result.error).find((issue) => {
|
44
|
+
const allPathsAsString = issue.path.map((p) => `${p}`);
|
45
|
+
return isEqual(allPathsAsString, toPath(field));
|
46
|
+
})) === null || _a === void 0 ? void 0 : _a.message,
|
47
|
+
};
|
48
|
+
},
|
49
|
+
});
|
50
|
+
}
|
package/build/ValidatedForm.d.ts
CHANGED
@@ -2,10 +2,32 @@ import { Form as RemixForm, useFetcher } from "@remix-run/react";
|
|
2
2
|
import React, { ComponentProps } from "react";
|
3
3
|
import { Validator } from "./validation/types";
|
4
4
|
export declare type FormProps<DataType> = {
|
5
|
+
/**
|
6
|
+
* A `Validator` object that describes how to validate the form.
|
7
|
+
*/
|
5
8
|
validator: Validator<DataType>;
|
9
|
+
/**
|
10
|
+
* A submit callback that gets called when the form is submitted
|
11
|
+
* after all validations have been run.
|
12
|
+
*/
|
6
13
|
onSubmit?: (data: DataType, event: React.FormEvent<HTMLFormElement>) => void;
|
14
|
+
/**
|
15
|
+
* Allows you to provide a `fetcher` from remix's `useFetcher` hook.
|
16
|
+
* The form will use the fetcher for loading states, action data, etc
|
17
|
+
* instead of the default form action.
|
18
|
+
*/
|
7
19
|
fetcher?: ReturnType<typeof useFetcher>;
|
20
|
+
/**
|
21
|
+
* Accepts an object of default values for the form
|
22
|
+
* that will automatically be propagated to the form fields via `useField`.
|
23
|
+
*/
|
8
24
|
defaultValues?: Partial<DataType>;
|
25
|
+
/**
|
26
|
+
* A ref to the form element.
|
27
|
+
*/
|
9
28
|
formRef?: React.RefObject<HTMLFormElement>;
|
10
29
|
} & Omit<ComponentProps<typeof RemixForm>, "onSubmit">;
|
30
|
+
/**
|
31
|
+
* The primary form component of `remix-validated-form`.
|
32
|
+
*/
|
11
33
|
export declare function ValidatedForm<DataType>({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, ...rest }: FormProps<DataType>): JSX.Element;
|
package/build/ValidatedForm.js
CHANGED
@@ -27,9 +27,12 @@ const useIsSubmitting = (action, fetcher) => {
|
|
27
27
|
return fetcher
|
28
28
|
? fetcher.state === "submitting"
|
29
29
|
: pendingFormSubmit &&
|
30
|
-
pendingFormSubmit.action
|
30
|
+
pendingFormSubmit.action === (action !== null && action !== void 0 ? action : actionForCurrentPage);
|
31
31
|
};
|
32
|
-
const getDataFromForm = (el) =>
|
32
|
+
const getDataFromForm = (el) => new FormData(el);
|
33
|
+
/**
|
34
|
+
* The primary form component of `remix-validated-form`.
|
35
|
+
*/
|
33
36
|
function ValidatedForm({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, ...rest }) {
|
34
37
|
var _a;
|
35
38
|
const [fieldErrors, setFieldErrors] = useFieldErrors(fetcher);
|
@@ -0,0 +1,4 @@
|
|
1
|
+
import { GenericObject } from ".";
|
2
|
+
export declare const objectFromPathEntries: (entries: [string, any][]) => {};
|
3
|
+
/** Flatten an object so there are no nested objects or arrays */
|
4
|
+
export declare function flatten(obj: GenericObject, preserveEmpty?: boolean): GenericObject;
|
package/build/flatten.js
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.flatten = exports.objectFromPathEntries = void 0;
|
7
|
+
// `flatten` is taken from https://github.com/richie5um/FlattenJS. Decided to implement them here instead of using that package because this is a core functionality of the library and this will add more flexibility in case we need to change the implementation.
|
8
|
+
const assign_1 = __importDefault(require("lodash/assign"));
|
9
|
+
const isArray_1 = __importDefault(require("lodash/isArray"));
|
10
|
+
const isObject_1 = __importDefault(require("lodash/isObject"));
|
11
|
+
const keys_1 = __importDefault(require("lodash/keys"));
|
12
|
+
const mapKeys_1 = __importDefault(require("lodash/mapKeys"));
|
13
|
+
const set_1 = __importDefault(require("lodash/set"));
|
14
|
+
const transform_1 = __importDefault(require("lodash/transform"));
|
15
|
+
const objectFromPathEntries = (entries) => entries.reduce((acc, [key, value]) => (0, set_1.default)(acc, key, value), {});
|
16
|
+
exports.objectFromPathEntries = objectFromPathEntries;
|
17
|
+
/** Flatten an object so there are no nested objects or arrays */
|
18
|
+
function flatten(obj, preserveEmpty = false) {
|
19
|
+
return (0, transform_1.default)(obj, function (result, value, key) {
|
20
|
+
if ((0, isObject_1.default)(value)) {
|
21
|
+
let flatMap = (0, mapKeys_1.default)(flatten(value, preserveEmpty), function (_mvalue, mkey) {
|
22
|
+
if ((0, isArray_1.default)(value)) {
|
23
|
+
let index = mkey.indexOf(".");
|
24
|
+
if (-1 !== index) {
|
25
|
+
return `${key}[${mkey.slice(0, index)}]${mkey.slice(index)}`;
|
26
|
+
}
|
27
|
+
return `${key}[${mkey}]`;
|
28
|
+
}
|
29
|
+
return `${key}.${mkey}`;
|
30
|
+
});
|
31
|
+
(0, assign_1.default)(result, flatMap);
|
32
|
+
// Preverve Empty arrays and objects
|
33
|
+
if (preserveEmpty && (0, keys_1.default)(flatMap).length === 0) {
|
34
|
+
result[key] = value;
|
35
|
+
}
|
36
|
+
}
|
37
|
+
else {
|
38
|
+
result[key] = value;
|
39
|
+
}
|
40
|
+
return result;
|
41
|
+
}, {});
|
42
|
+
}
|
43
|
+
exports.flatten = flatten;
|
package/build/hooks.d.ts
CHANGED
@@ -1,8 +1,33 @@
|
|
1
|
-
export declare
|
2
|
-
|
1
|
+
export declare type FieldProps = {
|
2
|
+
/**
|
3
|
+
* The validation error message if there is one.
|
4
|
+
*/
|
5
|
+
error?: string;
|
6
|
+
/**
|
7
|
+
* Clears the error message.
|
8
|
+
*/
|
3
9
|
clearError: () => void;
|
10
|
+
/**
|
11
|
+
* Validates the field.
|
12
|
+
*/
|
4
13
|
validate: () => void;
|
5
|
-
|
14
|
+
/**
|
15
|
+
* The default value of the field, if there is one.
|
16
|
+
*/
|
17
|
+
defaultValue?: any;
|
6
18
|
};
|
19
|
+
/**
|
20
|
+
* Provides the data and helpers necessary to set up a field.
|
21
|
+
*/
|
22
|
+
export declare const useField: (name: string) => FieldProps;
|
23
|
+
/**
|
24
|
+
* Provides access to the entire form context.
|
25
|
+
* This is not usually necessary, but can be useful for advanced use cases.
|
26
|
+
*/
|
7
27
|
export declare const useFormContext: () => import("./internal/formContext").FormContextValue;
|
28
|
+
/**
|
29
|
+
* Returns whether or not the parent form is currently being submitted.
|
30
|
+
* This is different from remix's `useTransition().submission` in that it
|
31
|
+
* is aware of what form it's in and when _that_ form is being submitted.
|
32
|
+
*/
|
8
33
|
export declare const useIsSubmitting: () => boolean;
|
package/build/hooks.js
CHANGED
@@ -1,8 +1,16 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
6
|
exports.useIsSubmitting = exports.useFormContext = exports.useField = void 0;
|
7
|
+
const get_1 = __importDefault(require("lodash/get"));
|
8
|
+
const toPath_1 = __importDefault(require("lodash/toPath"));
|
4
9
|
const react_1 = require("react");
|
5
10
|
const formContext_1 = require("./internal/formContext");
|
11
|
+
/**
|
12
|
+
* Provides the data and helpers necessary to set up a field.
|
13
|
+
*/
|
6
14
|
const useField = (name) => {
|
7
15
|
const { fieldErrors, clearError, validateField, defaultValues } = (0, react_1.useContext)(formContext_1.FormContext);
|
8
16
|
const field = (0, react_1.useMemo)(() => ({
|
@@ -11,13 +19,23 @@ const useField = (name) => {
|
|
11
19
|
clearError(name);
|
12
20
|
},
|
13
21
|
validate: () => validateField(name),
|
14
|
-
defaultValue: defaultValues
|
22
|
+
defaultValue: defaultValues
|
23
|
+
? (0, get_1.default)(defaultValues, (0, toPath_1.default)(name), undefined)
|
24
|
+
: undefined,
|
15
25
|
}), [clearError, defaultValues, fieldErrors, name, validateField]);
|
16
26
|
return field;
|
17
27
|
};
|
18
28
|
exports.useField = useField;
|
19
|
-
|
29
|
+
/**
|
30
|
+
* Provides access to the entire form context.
|
31
|
+
* This is not usually necessary, but can be useful for advanced use cases.
|
32
|
+
*/
|
20
33
|
const useFormContext = () => (0, react_1.useContext)(formContext_1.FormContext);
|
21
34
|
exports.useFormContext = useFormContext;
|
35
|
+
/**
|
36
|
+
* Returns whether or not the parent form is currently being submitted.
|
37
|
+
* This is different from remix's `useTransition().submission` in that it
|
38
|
+
* is aware of what form it's in and when _that_ form is being submitted.
|
39
|
+
*/
|
22
40
|
const useIsSubmitting = () => (0, exports.useFormContext)().isSubmitting;
|
23
41
|
exports.useIsSubmitting = useIsSubmitting;
|
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
@@ -15,3 +15,5 @@ __exportStar(require("./server"), exports);
|
|
15
15
|
__exportStar(require("./ValidatedForm"), exports);
|
16
16
|
__exportStar(require("./validation/types"), exports);
|
17
17
|
__exportStar(require("./validation/withYup"), exports);
|
18
|
+
__exportStar(require("./validation/withZod"), exports);
|
19
|
+
__exportStar(require("./validation/createValidator"), exports);
|
@@ -0,0 +1,4 @@
|
|
1
|
+
import { GenericObject } from "..";
|
2
|
+
export declare const objectFromPathEntries: (entries: [string, any][]) => {};
|
3
|
+
/** Flatten an object so there are no nested objects or arrays */
|
4
|
+
export declare function flatten(obj: GenericObject, preserveEmpty?: boolean): GenericObject;
|
@@ -0,0 +1,43 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.flatten = exports.objectFromPathEntries = void 0;
|
7
|
+
// `flatten` is taken from https://github.com/richie5um/FlattenJS. Decided to implement them here instead of using that package because this is a core functionality of the library and this will add more flexibility in case we need to change the implementation.
|
8
|
+
const assign_1 = __importDefault(require("lodash/assign"));
|
9
|
+
const isArray_1 = __importDefault(require("lodash/isArray"));
|
10
|
+
const isObject_1 = __importDefault(require("lodash/isObject"));
|
11
|
+
const keys_1 = __importDefault(require("lodash/keys"));
|
12
|
+
const mapKeys_1 = __importDefault(require("lodash/mapKeys"));
|
13
|
+
const set_1 = __importDefault(require("lodash/set"));
|
14
|
+
const transform_1 = __importDefault(require("lodash/transform"));
|
15
|
+
const objectFromPathEntries = (entries) => entries.reduce((acc, [key, value]) => (0, set_1.default)(acc, key, value), {});
|
16
|
+
exports.objectFromPathEntries = objectFromPathEntries;
|
17
|
+
/** Flatten an object so there are no nested objects or arrays */
|
18
|
+
function flatten(obj, preserveEmpty = false) {
|
19
|
+
return (0, transform_1.default)(obj, function (result, value, key) {
|
20
|
+
if ((0, isObject_1.default)(value)) {
|
21
|
+
let flatMap = (0, mapKeys_1.default)(flatten(value, preserveEmpty), function (_mvalue, mkey) {
|
22
|
+
if ((0, isArray_1.default)(value)) {
|
23
|
+
let index = mkey.indexOf(".");
|
24
|
+
if (-1 !== index) {
|
25
|
+
return `${key}[${mkey.slice(0, index)}]${mkey.slice(index)}`;
|
26
|
+
}
|
27
|
+
return `${key}[${mkey}]`;
|
28
|
+
}
|
29
|
+
return `${key}.${mkey}`;
|
30
|
+
});
|
31
|
+
(0, assign_1.default)(result, flatMap);
|
32
|
+
// Preverve Empty arrays and objects
|
33
|
+
if (preserveEmpty && (0, keys_1.default)(flatMap).length === 0) {
|
34
|
+
result[key] = value;
|
35
|
+
}
|
36
|
+
}
|
37
|
+
else {
|
38
|
+
result[key] = value;
|
39
|
+
}
|
40
|
+
return result;
|
41
|
+
}, {});
|
42
|
+
}
|
43
|
+
exports.flatten = flatten;
|