remix-validated-form 0.0.3 → 1.1.1-beta.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/.eslintcache +1 -0
- package/.prettierignore +2 -0
- package/README.md +228 -1
- package/browser/ValidatedForm.js +1 -1
- package/browser/flatten.d.ts +5 -0
- package/browser/flatten.js +41 -0
- package/browser/hooks.js +5 -1
- package/browser/index.d.ts +1 -0
- package/browser/index.js +1 -0
- package/browser/server.d.ts +1 -1
- package/browser/server.js +1 -1
- package/browser/validation/createValidator.d.ts +3 -0
- package/browser/validation/createValidator.js +8 -0
- package/browser/validation/types.d.ts +5 -2
- package/browser/validation/validation.test.js +156 -8
- package/browser/validation/withYup.js +28 -25
- package/browser/validation/withZod.d.ts +3 -0
- package/browser/validation/withZod.js +47 -0
- package/build/ValidatedForm.js +1 -1
- package/build/flatten.d.ts +5 -0
- package/build/flatten.js +49 -0
- package/build/hooks.js +8 -1
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/server.d.ts +1 -1
- package/build/server.js +3 -3
- package/build/validation/createValidator.d.ts +3 -0
- package/build/validation/createValidator.js +12 -0
- package/build/validation/types.d.ts +5 -2
- package/build/validation/validation.test.js +156 -8
- package/build/validation/withYup.js +28 -25
- package/build/validation/withZod.d.ts +3 -0
- package/build/validation/withZod.js +54 -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 +100 -0
- package/sample-app/app/routes/subjects/index.tsx +112 -0
- package/sample-app/app/routes/subjects/new.tsx +48 -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 +4617 -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
@@ -0,0 +1,47 @@
|
|
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
|
+
export function withZod(zodSchema) {
|
21
|
+
return createValidator({
|
22
|
+
validate: (value) => {
|
23
|
+
const result = zodSchema.safeParse(value);
|
24
|
+
if (result.success)
|
25
|
+
return { data: result.data, error: undefined };
|
26
|
+
const fieldErrors = {};
|
27
|
+
getIssuesForError(result.error).forEach((issue) => {
|
28
|
+
const path = pathToString(issue.path);
|
29
|
+
if (!fieldErrors[path])
|
30
|
+
fieldErrors[path] = issue.message;
|
31
|
+
});
|
32
|
+
return { error: fieldErrors, data: undefined };
|
33
|
+
},
|
34
|
+
validateField: (data, field) => {
|
35
|
+
var _a;
|
36
|
+
const result = zodSchema.safeParse(data);
|
37
|
+
if (result.success)
|
38
|
+
return { error: undefined };
|
39
|
+
return {
|
40
|
+
error: (_a = getIssuesForError(result.error).find((issue) => {
|
41
|
+
const allPathsAsString = issue.path.map((p) => `${p}`);
|
42
|
+
return isEqual(allPathsAsString, toPath(field));
|
43
|
+
})) === null || _a === void 0 ? void 0 : _a.message,
|
44
|
+
};
|
45
|
+
},
|
46
|
+
});
|
47
|
+
}
|
package/build/ValidatedForm.js
CHANGED
@@ -27,7 +27,7 @@ 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
32
|
const getDataFromForm = (el) => Object.fromEntries(new FormData(el));
|
33
33
|
function ValidatedForm({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, ...rest }) {
|
@@ -0,0 +1,5 @@
|
|
1
|
+
import { GenericObject } from ".";
|
2
|
+
/** Unflatten a previously flatten object */
|
3
|
+
export declare function unflatten(params: GenericObject): {};
|
4
|
+
/** Flatten an object so there are no nested objects or arrays */
|
5
|
+
export declare function flatten(obj: GenericObject, preserveEmpty?: boolean): GenericObject;
|
package/build/flatten.js
ADDED
@@ -0,0 +1,49 @@
|
|
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.unflatten = void 0;
|
7
|
+
// `flatten` and `unflatten` are 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 reduce_1 = __importDefault(require("lodash/reduce"));
|
14
|
+
const set_1 = __importDefault(require("lodash/set"));
|
15
|
+
const transform_1 = __importDefault(require("lodash/transform"));
|
16
|
+
/** Unflatten a previously flatten object */
|
17
|
+
function unflatten(params) {
|
18
|
+
return (0, reduce_1.default)(params, function (result, value, key) {
|
19
|
+
return (0, set_1.default)(result, key, value);
|
20
|
+
}, {});
|
21
|
+
}
|
22
|
+
exports.unflatten = unflatten;
|
23
|
+
/** Flatten an object so there are no nested objects or arrays */
|
24
|
+
function flatten(obj, preserveEmpty = false) {
|
25
|
+
return (0, transform_1.default)(obj, function (result, value, key) {
|
26
|
+
if ((0, isObject_1.default)(value)) {
|
27
|
+
let flatMap = (0, mapKeys_1.default)(flatten(value, preserveEmpty), function (_mvalue, mkey) {
|
28
|
+
if ((0, isArray_1.default)(value)) {
|
29
|
+
let index = mkey.indexOf(".");
|
30
|
+
if (-1 !== index) {
|
31
|
+
return `${key}[${mkey.slice(0, index)}]${mkey.slice(index)}`;
|
32
|
+
}
|
33
|
+
return `${key}[${mkey}]`;
|
34
|
+
}
|
35
|
+
return `${key}.${mkey}`;
|
36
|
+
});
|
37
|
+
(0, assign_1.default)(result, flatMap);
|
38
|
+
// Preverve Empty arrays and objects
|
39
|
+
if (preserveEmpty && (0, keys_1.default)(flatMap).length === 0) {
|
40
|
+
result[key] = value;
|
41
|
+
}
|
42
|
+
}
|
43
|
+
else {
|
44
|
+
result[key] = value;
|
45
|
+
}
|
46
|
+
return result;
|
47
|
+
}, {});
|
48
|
+
}
|
49
|
+
exports.flatten = flatten;
|
package/build/hooks.js
CHANGED
@@ -1,6 +1,11 @@
|
|
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");
|
6
11
|
const useField = (name) => {
|
@@ -11,7 +16,9 @@ const useField = (name) => {
|
|
11
16
|
clearError(name);
|
12
17
|
},
|
13
18
|
validate: () => validateField(name),
|
14
|
-
defaultValue: defaultValues
|
19
|
+
defaultValue: defaultValues
|
20
|
+
? (0, get_1.default)(defaultValues, (0, toPath_1.default)(name), undefined)
|
21
|
+
: undefined,
|
15
22
|
}), [clearError, defaultValues, fieldErrors, name, validateField]);
|
16
23
|
return field;
|
17
24
|
};
|
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
@@ -15,3 +15,4 @@ __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);
|
package/build/server.d.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
import { FieldErrors } from "./validation/types";
|
2
|
-
export declare const
|
2
|
+
export declare const validationError: (errors: FieldErrors) => Response;
|
package/build/server.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.validationError = void 0;
|
4
4
|
const server_runtime_1 = require("@remix-run/server-runtime");
|
5
|
-
const
|
6
|
-
exports.
|
5
|
+
const validationError = (errors) => (0, server_runtime_1.json)({ fieldErrors: errors }, { status: 422 });
|
6
|
+
exports.validationError = validationError;
|
@@ -0,0 +1,12 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.createValidator = void 0;
|
4
|
+
const flatten_1 = require("../flatten");
|
5
|
+
/** Handles data manipulation such us flattening the data to send to the validator */
|
6
|
+
function createValidator(validator) {
|
7
|
+
return {
|
8
|
+
validate: (value) => validator.validate((0, flatten_1.unflatten)(value)),
|
9
|
+
validateField: (data, field) => validator.validateField((0, flatten_1.unflatten)(data), field),
|
10
|
+
};
|
11
|
+
}
|
12
|
+
exports.createValidator = createValidator;
|
@@ -1,4 +1,7 @@
|
|
1
1
|
export declare type FieldErrors = Record<string, string>;
|
2
|
+
export declare type GenericObject = {
|
3
|
+
[key: string]: any;
|
4
|
+
};
|
2
5
|
export declare type ValidationResult<DataType> = {
|
3
6
|
data: DataType;
|
4
7
|
error: undefined;
|
@@ -10,6 +13,6 @@ export declare type ValidateFieldResult = {
|
|
10
13
|
error?: string;
|
11
14
|
};
|
12
15
|
export declare type Validator<DataType> = {
|
13
|
-
validate: (
|
14
|
-
validateField: (
|
16
|
+
validate: (unvalidatedData: GenericObject) => ValidationResult<DataType>;
|
17
|
+
validateField: (unvalidatedData: GenericObject, field: string) => ValidateFieldResult;
|
15
18
|
};
|
@@ -20,7 +20,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
20
20
|
};
|
21
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
22
22
|
const yup = __importStar(require("yup"));
|
23
|
+
const zod_1 = require("zod");
|
23
24
|
const __1 = require("..");
|
25
|
+
const withZod_1 = require("./withZod");
|
24
26
|
const validationTestCases = [
|
25
27
|
{
|
26
28
|
name: "yup",
|
@@ -28,6 +30,37 @@ const validationTestCases = [
|
|
28
30
|
firstName: yup.string().required(),
|
29
31
|
lastName: yup.string().required(),
|
30
32
|
age: yup.number(),
|
33
|
+
address: yup
|
34
|
+
.object({
|
35
|
+
streetAddress: yup.string().required(),
|
36
|
+
city: yup.string().required(),
|
37
|
+
country: yup.string().required(),
|
38
|
+
})
|
39
|
+
.required(),
|
40
|
+
pets: yup.array().of(yup.object({
|
41
|
+
animal: yup.string().required(),
|
42
|
+
name: yup.string().required(),
|
43
|
+
})),
|
44
|
+
})),
|
45
|
+
},
|
46
|
+
{
|
47
|
+
name: "zod",
|
48
|
+
validator: (0, withZod_1.withZod)(zod_1.z.object({
|
49
|
+
firstName: zod_1.z.string().nonempty(),
|
50
|
+
lastName: zod_1.z.string().nonempty(),
|
51
|
+
age: zod_1.z.optional(zod_1.z.number()),
|
52
|
+
address: zod_1.z.preprocess((value) => (value == null ? {} : value), zod_1.z.object({
|
53
|
+
streetAddress: zod_1.z.string().nonempty(),
|
54
|
+
city: zod_1.z.string().nonempty(),
|
55
|
+
country: zod_1.z.string().nonempty(),
|
56
|
+
})),
|
57
|
+
pets: zod_1.z
|
58
|
+
.object({
|
59
|
+
animal: zod_1.z.string().nonempty(),
|
60
|
+
name: zod_1.z.string().nonempty(),
|
61
|
+
})
|
62
|
+
.array()
|
63
|
+
.optional(),
|
31
64
|
})),
|
32
65
|
},
|
33
66
|
];
|
@@ -37,41 +70,156 @@ describe("Validation", () => {
|
|
37
70
|
describe.each(validationTestCases)("Adapter for $name", ({ validator }) => {
|
38
71
|
describe("validate", () => {
|
39
72
|
it("should return the data when valid", () => {
|
40
|
-
const
|
73
|
+
const person = {
|
41
74
|
firstName: "John",
|
42
75
|
lastName: "Doe",
|
43
76
|
age: 30,
|
77
|
+
address: {
|
78
|
+
streetAddress: "123 Main St",
|
79
|
+
city: "Anytown",
|
80
|
+
country: "USA",
|
81
|
+
},
|
82
|
+
pets: [{ animal: "dog", name: "Fido" }],
|
44
83
|
};
|
45
|
-
expect(validator.validate(
|
46
|
-
data:
|
84
|
+
expect(validator.validate(person)).toEqual({
|
85
|
+
data: person,
|
47
86
|
error: undefined,
|
48
87
|
});
|
49
88
|
});
|
50
89
|
it("should return field errors when invalid", () => {
|
51
|
-
const obj = { age: "hi!" };
|
90
|
+
const obj = { age: "hi!", pets: [{ animal: "dog" }] };
|
52
91
|
expect(validator.validate(obj)).toEqual({
|
53
92
|
data: undefined,
|
54
93
|
error: {
|
55
94
|
firstName: anyString,
|
56
95
|
lastName: anyString,
|
57
96
|
age: anyString,
|
97
|
+
"address.city": anyString,
|
98
|
+
"address.country": anyString,
|
99
|
+
"address.streetAddress": anyString,
|
100
|
+
"pets[0].name": anyString,
|
58
101
|
},
|
59
102
|
});
|
60
103
|
});
|
61
104
|
});
|
62
105
|
describe("validateField", () => {
|
63
106
|
it("should not return an error if field is valid", () => {
|
64
|
-
const
|
65
|
-
|
107
|
+
const person = {
|
108
|
+
firstName: "John",
|
109
|
+
lastName: {}, // invalid, but we should only be validating firstName
|
110
|
+
};
|
111
|
+
expect(validator.validateField(person, "firstName")).toEqual({
|
112
|
+
error: undefined,
|
113
|
+
});
|
114
|
+
});
|
115
|
+
it("should not return an error if a nested field is valid", () => {
|
116
|
+
const person = {
|
117
|
+
firstName: "John",
|
118
|
+
lastName: {},
|
119
|
+
address: {
|
120
|
+
streetAddress: "123 Main St",
|
121
|
+
city: "Anytown",
|
122
|
+
country: "USA",
|
123
|
+
},
|
124
|
+
pets: [{ animal: "dog", name: "Fido" }],
|
125
|
+
};
|
126
|
+
expect(validator.validateField(person, "address.streetAddress")).toEqual({
|
127
|
+
error: undefined,
|
128
|
+
});
|
129
|
+
expect(validator.validateField(person, "address.city")).toEqual({
|
130
|
+
error: undefined,
|
131
|
+
});
|
132
|
+
expect(validator.validateField(person, "address.country")).toEqual({
|
133
|
+
error: undefined,
|
134
|
+
});
|
135
|
+
expect(validator.validateField(person, "pets[0].animal")).toEqual({
|
136
|
+
error: undefined,
|
137
|
+
});
|
138
|
+
expect(validator.validateField(person, "pets[0].name")).toEqual({
|
66
139
|
error: undefined,
|
67
140
|
});
|
68
141
|
});
|
69
142
|
it("should return an error if field is invalid", () => {
|
70
|
-
const
|
71
|
-
|
143
|
+
const person = {
|
144
|
+
firstName: "John",
|
145
|
+
lastName: {},
|
146
|
+
address: {
|
147
|
+
streetAddress: "123 Main St",
|
148
|
+
city: 1234,
|
149
|
+
},
|
150
|
+
};
|
151
|
+
expect(validator.validateField(person, "lastName")).toEqual({
|
152
|
+
error: anyString,
|
153
|
+
});
|
154
|
+
});
|
155
|
+
it("should return an error if a nested field is invalid", () => {
|
156
|
+
const person = {
|
157
|
+
firstName: "John",
|
158
|
+
lastName: {},
|
159
|
+
address: {
|
160
|
+
streetAddress: "123 Main St",
|
161
|
+
city: 1234,
|
162
|
+
},
|
163
|
+
pets: [{ animal: "dog" }],
|
164
|
+
};
|
165
|
+
expect(validator.validateField(person, "address.country")).toEqual({
|
166
|
+
error: anyString,
|
167
|
+
});
|
168
|
+
expect(validator.validateField(person, "pets[0].name")).toEqual({
|
72
169
|
error: anyString,
|
73
170
|
});
|
74
171
|
});
|
75
172
|
});
|
76
173
|
});
|
77
174
|
});
|
175
|
+
describe("withZod", () => {
|
176
|
+
it("returns coherent errors for complex schemas", () => {
|
177
|
+
const schema = zod_1.z.union([
|
178
|
+
zod_1.z.object({
|
179
|
+
type: zod_1.z.literal("foo"),
|
180
|
+
foo: zod_1.z.string(),
|
181
|
+
}),
|
182
|
+
zod_1.z.object({
|
183
|
+
type: zod_1.z.literal("bar"),
|
184
|
+
bar: zod_1.z.string(),
|
185
|
+
}),
|
186
|
+
]);
|
187
|
+
const obj = {
|
188
|
+
type: "foo",
|
189
|
+
bar: 123,
|
190
|
+
foo: 123,
|
191
|
+
};
|
192
|
+
expect((0, withZod_1.withZod)(schema).validate(obj)).toEqual({
|
193
|
+
data: undefined,
|
194
|
+
error: {
|
195
|
+
type: anyString,
|
196
|
+
bar: anyString,
|
197
|
+
foo: anyString,
|
198
|
+
},
|
199
|
+
});
|
200
|
+
});
|
201
|
+
it("returns errors for fields that are unions", () => {
|
202
|
+
const schema = zod_1.z.object({
|
203
|
+
field1: zod_1.z.union([zod_1.z.literal("foo"), zod_1.z.literal("bar")]),
|
204
|
+
field2: zod_1.z.union([zod_1.z.literal("foo"), zod_1.z.literal("bar")]),
|
205
|
+
});
|
206
|
+
const obj = {
|
207
|
+
field1: "a value",
|
208
|
+
// field2 missing
|
209
|
+
};
|
210
|
+
const validator = (0, withZod_1.withZod)(schema);
|
211
|
+
expect(validator.validate(obj)).toEqual({
|
212
|
+
data: undefined,
|
213
|
+
error: {
|
214
|
+
field1: anyString,
|
215
|
+
field2: anyString,
|
216
|
+
},
|
217
|
+
});
|
218
|
+
expect(validator.validateField(obj, "field1")).toEqual({
|
219
|
+
error: anyString,
|
220
|
+
});
|
221
|
+
expect(validator.validateField(obj, "field2")).toEqual({
|
222
|
+
error: anyString,
|
223
|
+
});
|
224
|
+
});
|
225
|
+
});
|
@@ -1,6 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.withYup = void 0;
|
4
|
+
const createValidator_1 = require("./createValidator");
|
4
5
|
const validationErrorToFieldErrors = (error) => {
|
5
6
|
const fieldErrors = {};
|
6
7
|
error.inner.forEach((innerError) => {
|
@@ -10,29 +11,31 @@ const validationErrorToFieldErrors = (error) => {
|
|
10
11
|
});
|
11
12
|
return fieldErrors;
|
12
13
|
};
|
13
|
-
const withYup = (validationSchema) =>
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
}
|
14
|
+
const withYup = (validationSchema) => {
|
15
|
+
return (0, createValidator_1.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
|
+
};
|
38
41
|
exports.withYup = withYup;
|
@@ -0,0 +1,54 @@
|
|
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.withZod = void 0;
|
7
|
+
const isEqual_1 = __importDefault(require("lodash/isEqual"));
|
8
|
+
const toPath_1 = __importDefault(require("lodash/toPath"));
|
9
|
+
const createValidator_1 = require("./createValidator");
|
10
|
+
const getIssuesForError = (err) => {
|
11
|
+
return err.issues.flatMap((issue) => {
|
12
|
+
if ("unionErrors" in issue) {
|
13
|
+
return issue.unionErrors.flatMap((err) => getIssuesForError(err));
|
14
|
+
}
|
15
|
+
else {
|
16
|
+
return [issue];
|
17
|
+
}
|
18
|
+
});
|
19
|
+
};
|
20
|
+
function pathToString(array) {
|
21
|
+
return array.reduce(function (string, item) {
|
22
|
+
var prefix = string === "" ? "" : ".";
|
23
|
+
return string + (isNaN(Number(item)) ? prefix + item : "[" + item + "]");
|
24
|
+
}, "");
|
25
|
+
}
|
26
|
+
function withZod(zodSchema) {
|
27
|
+
return (0, createValidator_1.createValidator)({
|
28
|
+
validate: (value) => {
|
29
|
+
const result = zodSchema.safeParse(value);
|
30
|
+
if (result.success)
|
31
|
+
return { data: result.data, error: undefined };
|
32
|
+
const fieldErrors = {};
|
33
|
+
getIssuesForError(result.error).forEach((issue) => {
|
34
|
+
const path = pathToString(issue.path);
|
35
|
+
if (!fieldErrors[path])
|
36
|
+
fieldErrors[path] = issue.message;
|
37
|
+
});
|
38
|
+
return { error: fieldErrors, data: undefined };
|
39
|
+
},
|
40
|
+
validateField: (data, field) => {
|
41
|
+
var _a;
|
42
|
+
const result = zodSchema.safeParse(data);
|
43
|
+
if (result.success)
|
44
|
+
return { error: undefined };
|
45
|
+
return {
|
46
|
+
error: (_a = getIssuesForError(result.error).find((issue) => {
|
47
|
+
const allPathsAsString = issue.path.map((p) => `${p}`);
|
48
|
+
return (0, isEqual_1.default)(allPathsAsString, (0, toPath_1.default)(field));
|
49
|
+
})) === null || _a === void 0 ? void 0 : _a.message,
|
50
|
+
};
|
51
|
+
},
|
52
|
+
});
|
53
|
+
}
|
54
|
+
exports.withZod = withZod;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "remix-validated-form",
|
3
|
-
"version": "
|
3
|
+
"version": "1.1.1-beta.0",
|
4
4
|
"description": "Form component and utils for easy form validation in remix",
|
5
5
|
"browser": "./browser/index.js",
|
6
6
|
"main": "./build/index.js",
|
@@ -14,7 +14,10 @@
|
|
14
14
|
"build:browser": "tsc --project tsconfig.json --module ESNext --outDir ./browser",
|
15
15
|
"build:main": "tsc --project tsconfig.json --module CommonJS --outDir ./build",
|
16
16
|
"build:tests": "tsc --project tsconfig.json --module CommonJS --outDir ./test-app/remix-validated-form",
|
17
|
+
"build:sample": "tsc --project tsconfig.json --module CommonJS --outDir ./sample-app/remix-validated-form",
|
18
|
+
"sample-app": "npm run build:sample && cd sample-app && npm run dev",
|
17
19
|
"test": "jest src",
|
20
|
+
"test:watch": "jest src --watch",
|
18
21
|
"lint": "eslint .",
|
19
22
|
"prettier": "prettier . --write",
|
20
23
|
"prepare": "husky install",
|
@@ -35,22 +38,23 @@
|
|
35
38
|
"validation"
|
36
39
|
],
|
37
40
|
"peerDependencies": {
|
38
|
-
"@remix-run/react": "^1.0.
|
39
|
-
"@remix-run/server-runtime": "^1.0.
|
41
|
+
"@remix-run/react": "^1.0.6",
|
42
|
+
"@remix-run/server-runtime": "^1.0.6",
|
40
43
|
"react": "^17.0.2"
|
41
44
|
},
|
42
45
|
"devDependencies": {
|
43
|
-
"@remix-run/react": "^1.0.
|
44
|
-
"@remix-run/server-runtime": "^1.0.
|
46
|
+
"@remix-run/react": "^1.0.6",
|
47
|
+
"@remix-run/server-runtime": "^1.0.6",
|
45
48
|
"@types/jest": "^27.0.3",
|
46
|
-
"@types/
|
47
|
-
"@
|
48
|
-
"@typescript-eslint/
|
49
|
+
"@types/lodash": "^4.14.178",
|
50
|
+
"@types/react": "^17.0.37",
|
51
|
+
"@typescript-eslint/eslint-plugin": "^5.6.0",
|
52
|
+
"@typescript-eslint/parser": "^5.6.0",
|
49
53
|
"babel-eslint": "^10.1.0",
|
50
54
|
"eslint": "^7.32.0",
|
51
55
|
"eslint-config-react-app": "^6.0.0",
|
52
56
|
"eslint-plugin-cypress": "^2.12.1",
|
53
|
-
"eslint-plugin-flowtype": "^
|
57
|
+
"eslint-plugin-flowtype": "^8.0.3",
|
54
58
|
"eslint-plugin-import": "^2.25.3",
|
55
59
|
"eslint-plugin-jsx-a11y": "^6.5.1",
|
56
60
|
"eslint-plugin-prettier": "^4.0.0",
|
@@ -58,15 +62,17 @@
|
|
58
62
|
"eslint-plugin-react-hooks": "^4.3.0",
|
59
63
|
"fetch-blob": "^3.1.3",
|
60
64
|
"husky": "^7.0.4",
|
61
|
-
"jest": "^27.
|
65
|
+
"jest": "^27.4.4",
|
62
66
|
"lint-staged": "^12.1.2",
|
63
|
-
"prettier": "^2.5.
|
67
|
+
"prettier": "^2.5.1",
|
64
68
|
"react": "^17.0.2",
|
65
|
-
"ts-jest": "^27.
|
66
|
-
"typescript": "^4.5.
|
67
|
-
"yup": "^0.32.11"
|
69
|
+
"ts-jest": "^27.1.1",
|
70
|
+
"typescript": "^4.5.3",
|
71
|
+
"yup": "^0.32.11",
|
72
|
+
"zod": "^3.11.6"
|
68
73
|
},
|
69
74
|
"dependencies": {
|
75
|
+
"lodash": "^4.17.21",
|
70
76
|
"tiny-invariant": "^1.2.0"
|
71
77
|
},
|
72
78
|
"lint-staged": {
|
package/sample-app/.env
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
# Environment variables declared in this file are automatically made available to Prisma.
|
2
|
+
# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables
|
3
|
+
|
4
|
+
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server and MongoDB (Preview).
|
5
|
+
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
|
6
|
+
|
7
|
+
DATABASE_URL="file:./dev.db"
|